Tag Archives: category

WordPress Tricks: Get the titles of recent blog posts from a certain category using wp_query

Here is a simple block of code that you can use to get the most recent post titles and permalinks to a certain number of the most recent posts in a certain category:

 

<ul>
<?php
    global $cat;
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5&cat=389');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?></ul>

Simply change the number of posts and the category id in the code above to get the number of posts and the category of posts that you want.

 

You can also check out my post on how to display a list of the titles of your most recent blog posts from several categories if you are looking for a way to get the most recent posts from several different categories.

WordPress Tricks: How to exclude a certain category of posts from the loop

Use this line of code to control which categories of posts do and don’t show up in the loop for particular pages on your WordPress site:

<?php
if (is_page('recipes')) {
 $catID=4;
}
elseif (is_home()) {
 $catID=-4;
}
if ($catID) {
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 query_posts("cat=$catID&paged=$paged");
} ?>

Copy and paste that code into your index.php or page.php template file, just before the loop is called.

This line of code will display all posts in category 4 on the recipe page, and not on the main blogroll page.

Simply change the page IDs (or slugs, either one will work) and the category IDs to control which pages show which category of posts.

To exclude a category of posts from the loop on a certain page, simply put a negative sign ( – ) in front of the category ID you want to exclude from that page, and the page will show posts from all categories except for that one.

If you want to specify which page each category of posts show up on, you would use this:

<?php
$catID = 0;
if (is_page('Books')) {
 $catID=1;
}
elseif (is_page('Videos')) {
 $catID=4;
}
elseif (is_page('CDs')) {
 $catID=5;
}
if ($catID) {
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 query_posts("cat=$catID&paged=$paged");
} ?>

 

Let me know if you have any questions, need more help,  or run into any problems.

Interspire Shopping Carts: Add a Flyout or Dropdown Menu to Sidebar Panel

Here is the easiest way I have found to add flyout or drop down menus to a particular panel in an Interspire shopping cart’s template.

First, sign  into the website’s admin panel.

Go to Settings -> Store Settings -> Display.

In the box next to Category List Depth, change the number to the number of levels you want to display in the flyout or drop down menu.

Now, go to Store Design and then click Browse Template files.

In your website template’s style.css file, add this code:

.Left #SideCategoryList li:hover, .CategoryList li:hover, .CategoryList li.Over {
background: #746755;
}
.Left #SideCategoryList li:hover a, .CategoryList li:hover a {
color: #fff;
}
.Left #SideCategoryList li ul, .CategoryList li ul {
display: none;
padding: 0 0 9px 0;
-moz-border-radius-bottomleft: 17px;
-moz-border-radius-topright: 17px;
-webkit-border-top-right-radius: 17px;
-webkit-border-bottom-left-radius: 17px;
width: 160px;
position: absolute;
}
.Left #SideCategoryList li:hover ul, .CategoryList li:hover ul {
display: block;
position:absolute;
margin-top:-15px;
left:160px;
width:160px;
}
.Left #SideCategoryList li:hover li, .CategoryList li:hover li {
float: none;
background: #f0eee8;
}
.Left #SideCategoryList li:hover li a, .CategoryList li:hover li a {
color: #444;
font-size: 14px;
text-transform: none;
font-style: normal;
border-top: 1px solid #f0eee8;
border-bottom: 1px solid #f0eee8;
}
.Left #SideCategoryList li li a:hover, .CategoryList li li a:hover {
background: #746755;
color: #fff;
border-color: #fefaf3;
}
.Left #SideCategoryList li:hover, .CategoryList li:hover {
background: #746755;
}
.Left #SideCategoryList li:hover a, .CategoryList li:hover a {color: #fff;}

.Left #SideCategoryList li ul, .CategoryList li ul {display: none;padding: 0 0 9px 0;-moz-border-radius-bottomleft: 17px;-moz-border-radius-topright: 17px;-webkit-border-top-right-radius: 17px;-webkit-border-bottom-left-radius: 17px;width: 160px;position: absolute;
}
.Left #SideCategoryList li:hover ul, .CategoryList li:hover ul {display: block;position:absolute;margin-top:-15px;left:160px;width:160px;
}
.Left #SideCategoryList li:hover li, .CategoryList li:hover li {float: none;background: #f0eee8;}
.Left #SideCategoryList li:hover li a, .CategoryList li:hover li a {color: #444;font-size: 14px;text-transform: none;font-style: normal;border-top: 1px solid #f0eee8;border-bottom: 1px solid #f0eee8;}
.Left #SideCategoryList li li a:hover, .CategoryList li li a:hover {background: #746755;color: #fff;border-color: #fefaf3;}

This code will add a flyout menu to the Category panel in the left sidebar of your website. You can change the name of the panel in the codes if you want to apply the flyout menu to another panel.

Next, you will want to edit the styles above to match the design of your own site.

And that’s all you need to create flyout or dropdown menus for the sidebar of your Interspire shopping cart template!

Let me know if you run into any issues or have any questions.

You can also learn how to create your own custom panels for Interspire shopping carts in another post I wrote.

WordPress: How to show a different number of posts on different pages

Here is the line of code that I use to control the number of posts on particular pages in a WordPress site.

Note: the website that this code was written for is set up to display different categories of posts on different pages.

Copy this line of code:

<?php if(is_category('3')) { query_posts('cat=3&posts_per_page=3&paged='.$paged);
} else if(is_category('5')) { query_posts('cat=5&posts_per_page=8&paged='.$paged);
} else if(is_category('6')) { query_posts('cat=6&posts_per_page=10&paged='.$paged);
} else if(is_category('7')) { query_posts('cat=7&posts_per_page=5&paged='.$paged);
} else if(is_category('4')) { query_posts('cat=4&posts_per_page=13&paged='.$paged);
} else if(is_category('1')) { query_posts('cat=1&posts_per_page=1&paged='.$paged);
}
$paged = ( get_query_var('paged') && get_query_var('paged') > 1 ) ? get_query_var('paged') : 1;
?>

And paste it into your page.php template, just before the loop is called.

Then just edit the category IDs and the number of posts you want to display on each page.

This code worked perfectly for me but, as always, just let me know if you run into any issues!

WordPress: How to control which category of posts displays on particular pages

Here are the codes I use to control which category of posts are displayed on which particular pages in WordPress. This is a great way to allow for multiple blogs on a single site, or to organize your blog posts according to topic, etc.

Open the page.php file in your WordPress admin panel. Insert this block of code just before the loop is called:

<?php
$catID = 0;
if (is_page('videos')) {
 $catID=3;
} elseif (is_page('music')) {
 $catID=4;
}
 elseif (is_page('poetry')) {
 $catID=5;
}

if ($catID) {
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 query_posts("cat=$catID&paged=$paged");
} ?>

Of course, you will want to change the category IDs and the page names to reflect those on your own site.

Note: the page names in this block of code are the slugs of each page, not the page titles.

This has worked perfectly for me in the past, but let me know if you run into any issues or have any questions.