Tag Archives: exclude

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: Add a div or other element to one specific page or page type

 

Here is the code I use to add a div or image or other element to just one page or category page of my WordPress sites:

This code will add a div with the id “featured_content” only on the home page:

<?php if ( is_home() ) { ?>
<div id="featured_content">
&nbsp;
</div>
<?php } ?>

 

This code would add a div with the id “featured_recipe” to all posts in the category “recipes.”:

<?php if ( is_category('recipes') ) { ?>
<div id="featured_recipe">
&nbsp;
</div>
<?php } ?>

 

This code would exclude “myimage.jpg” only on the Contact page:

<?php if ( !is_page('contact') ) { ?>
<img src="myimage.jpg">
<?php } ?>

 

As you can see, there are many uses and ways implementations of this code. Let me know if you have any questions or need further assistance.

Concrete5: How to exclude a certain page type from your autonav menu

Here is how I exclude specific page types from an autonav menu in Concrete5.

Navigate to concrete/blocks/autonav and open the view.php file.

Scroll down to this line:

foreach($aBlocks as $ni) {
		$_c = $ni->getCollectionObject();
		if (!$_c->getCollectionAttributeValue('exclude_nav')) {

Add this block of code:

$typeHandle = $_c->getCollectionTypeHandle();
			if($typeHandle!="page_type"){

Change “page_type” to the handle of page type that you want to exclude from your autonav menu (for example: blog_post).

Now scroll down and add “}” after this line:

$lastLevel = $thisLevel;
			$i++;

You can also see or download the edited view.php here (this is a text file, so if you want to use it, make sure you save it as a php file!!).

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.