Category: WordPress Logic

WordPress Taxonomy Query (Keep it Simple)

Now that a number of people are using more and more custom taxonomies for WordPress – custom taxonomy queries are something becoming necessary.

Example: You have a WordPress site set up to show/sell products (in this example – you sell books and posters). You have a custom taxonomy called “catalog” that you use to organize all the products.

If you want to show all items that are marked “posters” in your custom catalog taxonomy you can simply use this query:

query_posts( array( 'catalog' => 'posters' ) ); 

Of course – you can create more complicated custom taxonomy queries, but this will get you started. By using custom taxonomies (and custom post types) – you can really open the door to creating complex content management systems (and/or really cool sites that need something more than your standard categories/tags/posts/pages).

Further documentation: http://codex.wordpress.org/Function_Reference/query_posts

Remember – keep it simple!

Change the_excerpt Length and Ending

Using WordPress – there are some times when you want to use the_excerpt but you do not want to show the default 55 words and/or show the […] after those 55 words.

Change the Length

Instead of only showing the default 55 words where you want to change it to something longer or shorter. To change this – just add/modify this in your functions.php file:

add_filter('excerpt_length', 'sdac_excerpt_length');
function sdac_excerpt_length( $length ) {
	return 75;
}

In that code example – I am setting the length to be 75 words.

Change the Ending

Instead of showing the default […] after the excerpt – you can omit that or change it. To change this – just add/modify this in your functions.php file:

add_filter('excerpt_more', 'jappler_excerpt_more');
function jappler_excerpt_more( $more ) {
        global $post;
	return '... <a>ID ).'"&gt;Read More &raquo;</a>';
}

In that example – I added a read more which links to the post permalink. (This filter is available with WordPress 2.9+)

Hopefully these examples will help a little bit the next time you want to use the excerpt but wished it was easy to change the output.

HOWTO: Add a Custom Comment Class to WordPress

Ever need to add your own class to your WordPress comment template but not sure how because of the function comment_class that controls the classes? Check out the function comment_class and you will see adding your own custom classes very easily.


Allow this sounds pretty basic – it is a lifesaver for doing a lot of customizations.

Conditionally Show Only On Home Page (Front Page)

There a are two functions that are used quite a bit to conditionally show content on the very front page (home page or page 1 of your blog). First – we had the function is_home(), but in WordPress version 2.5 – a new function was introduced: is_front_page().

You might think both would conditionally only display content on the home page (front page, page 1 of your blog) – but you actually need to use the following code to accomplish showing something ONLY on the front page of your paged blog:


<p>Show this text only on the home page</p>

So – if you want to easily show something on what I would consider the front page – use the code above to successfully achieve that. (This is particularly helpful if you have ads that run speficically on the home page and others that are ROS (run of site).

Further reading:

Get Top Parent Category (WordPress)

There are many times when you need to show or get the top most (root) parent category in WordPress – regardless of how many subcategories you might be deep. I have used this logic for page navigation (highlight the top parent tab) – as well as within some custom loops/sidebar code.

They way to do this:

// get parent category slug
$parentCatList = get_category_parents($cat,false,',');
$parentCatListArray = split(",",$parentCatList);
$topParentName = $parentCatListArray[0];
$sdacReplace = array(" " => "-", "(" => "", ")" => "");
$topParent = strtolower(strtr($topParentName,$sdacReplace));	

To test this you can simply put it in your header and echo out $topParent and you will see the “slug” of the category.
If you want to see the category name and not necessarily the slug – you can simply echo $topParentName.

Show One WordPress Post

There is a great function you might be aware of that allows you to get one WordPress post and display it anywhere you would like (header, sidebar, home page, etc).

To show only one post, use the function get_post.

Example:

post_title;
$sdac_content = $show_post_id-&gt;post_content;
?&gt;
<h2></h2>
 

The code above will show the title and content of the post with the post ID of 10. If you are not sure what your post ID is, go to the manage screen in the WP-Admin, and hover over the post title. In your browser status bar, you will see the URL – and the post ID is the number at the end of the URL.

Show Only Category (Not Subcategory) Content

If you ever wanted to only show the content of the category you were on in the category view of WordPress (instead of the category you are on plus all of it’s subcategories – you can easily do so by adding the code below to the template that is used to show your category content

You would start your loop with this:

 

Put your title, content functions here

and where you normally see the call, put to close the additional (in_category) if statement.

This code gets the current category and then only displays categories that are in that category (not sub category)

Exclude Single Post in WordPress

If you would like to exclude one post from your blog, archive, search results or wherever you need to on your blog, you can do so by putting in a small piece of code within the particular WordPress loop:

The following example will show all posts except for the post with the ID of 179:

<?php if ( $post->ID == '179' ) continue;?>

This is something that I get a lot of requests for and is very useful in a number of situations.

Exclude Single Category in WordPress

If you ever need to exclude a single category from a WordPress page (archives, index, category page, etc) you can easily do so by using a little conditional tag code within the WordPress loop.

The example below will skip over any post that is in the category with the ID of 35:





This can be helpful if you want to not show a particular category in your blog (if you have a category based site setup) – or if you want to hide some categories from your search results.

Remove “Private:” From WordPress Titles

I needed to be able filter out the text “Private:” for posts and pages that were password protected but did not want to edit any core WordPress files. To filter out that text, I added the following code into my functions.php file (within my theme directory):

function remove_private_prefix($title) {
$title = str_replace(
'Private:',
'',
$title);
return $title;
}
add_filter('the_title','remove_private_prefix');

(I also posted this filter in the WordPress Support Forum)