WordPress: Exclude category from single.php without WP_Query

WordPress - exclude category from single.php template

I was working on a WordPress website the other day when a new category was added and those posts required a more customized single.php template, so they had to be excluded from the WordPress loop in the default single.php template. I wanted to achieve this without having to deal with custom post types or using WP_Query(), which is the focus of this tutorial.

In such cases, we would ideally create custom post types, but for a quick fix, we only need to create a customized version of single.php for a specific category and then modify the single.php file itself.

Note: For a tutorial on how to make use of custom post types, check this article.

Modifying single.php template to exclude a category

When a single post is displayed, the single.php template file is used. Inside this template, we must first check whether the current post belongs to that specific category and if true, we include a different single.php template to it.

For example, let's say the category is a portfolio, then we would have something like this at the top of the single.php:

if(in_category('portfolio')) {
	include('single-portfolio.php');
}
else {
...
}

The WordPress in_category() functions checks, if the current post is in the specific category and if so, we include a customized template file, in our case single-portfolio.php file. If not, then the rest of the single.php is wrapped within the else { } block.

Only posts that are not in the portfolio category will reach the WordPress loop inside the else { } block, so we don't need to make use of WP_Query() to exclude the mentioned category. We can simply use the regular WordPress Loop:

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
        ...
<?php endwhile; ?>

But, there is still an issue with the next and previous links if the single.php uses the next_post_link() and the previous_post_link() functions. Those links will still link to the posts of the portfolio category.

Fortunately, the functions for displaying next and previous links have parameters specifically for this.

Excluding category posts from next and previous post links

The next_post_link() and the previous_post_link() functions are used to post the next/previous posts in chronological order to the current post. If we use the default parameters, they will also link to portfolio posts, which is not what we want.

Possible parameters for these two WordPress functions are:

  • format
    This is the link anchor format of what comes before and after the link.
  • link
    This is the permalink format of the content inside <a></a>.
  • in_same_term (boolean)
    Here, we specify true or false if the next/previous posts need to be of the same taxonomy term as the current post. The default taxonomy term for this parameter is "category", but this can be changed using the last parameter.
  • excluded_terms
    And here, we can list the ID of the terms that will be excluded. We can use an array or comma-separated list. This is the parameter we are interested in.
  • taxonomy
    And for the last parameter, we can specify what taxonomy will be used. The default is "category".

We can use the fourth parameter excluded_terms parameter to tell the WordPress which category ID to skip when creating previous/next links. So, if the portfolio category, for example, has the id=10, we could use the link for the next post like this.

<?php next_post_link( '%link &raquo;', '%title', false, '10' ); ?>
Note: In the above example, the first three parameters use the default values.

The previous_post_link() is used the same way.

Conclusion

In this post, we learned a quick way to make WordPress use a customized single.php template file when displaying a single post with a specific category without creating a customized post type. In single.php, we first checked if the post is in a specific category and if so, we included the customized single.php file. And for links to the next/previous posts, we had to modify them a bit to exclude the specific categories from the links.

Write a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.