Tag Archives: loop

WordPress Tricks: Use a WordPress query to exclude a category from your home page without breaking pagination

Here is the code I use in a WordPress site’s index.php file to exclude a specific category from the home page withOUT breaking the pagination:

<?php if ( is_home() ) { query_posts( $query_string.'&cat=-21' ); } ?>

Of course, to include a specific category, you would use this:

<?php if ( is_home() ) { query_posts( $query_string.'&cat=4' ); } ?>

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.

WordPress: Manually control which blog pages show excerpts and which show full posts

Here is how to manually control which blog pages on your WordPress site display posts as excerpts and which show the entire posts.

For the site that I developed this solution for, the pages are set up to display posts from a particular category on each particular page.

First, open the loop-page.php file in your WordPress admin panel.

Between the two tags: <div class=”entry-content”> and <?php wp_link_pages…, paste this code:

<?php if ( is_category(3) ) { ?>
<?php the_content(); ?>

<?php } else { ?>
<?php the_excerpt(); ?>

<?php } ?>

In this example, we are telling WordPress that if the page is displaying posts from category ID 3, show the entire post. Otherwise, on all other pages, show the exerpts of posts.

Of course, you will want to edit <?php if ( is_category(3) ) { ?> to whatever category ID or page ID (using <?php if ( is_page(3) ) { ?>) or post ID (using <?php if ( is_post(3) ) { ?>) that you are trying to control.

If you want to control the excerpts and/or full posts on multiple pages or posts or categories, you would use this line at the beginning of the previous code:

<?php if ( is_category(array(3,5)) ) { ?>

Let me know if you have any questions or trouble with this!

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.