treehouse : what would you like to learn today?
Web Design Web Development iOS Development

[Solved] Wordpress - Remove Category from Post List

  • In Wordpress, on the Single Post page, there is "Older Post" and "Newer Post" navigation links. It goes to the previous and next post. Is there a way to make sure those links skips over posts from a specific category?
  • No problem! Before the loop in your single.php, you'll want to add in something like this:
    <?php query_posts('cat=-3'); ?>

    Changing the number to the ID of the category you'd like to exclude.
  • @TheDoc, normally that would be the simple answer. But the Single Post page isn't a loop. And the "Older Post" and "Newer Post" are just straight Links. Here's the code to the page.

    <?php
    /**
    * The Template for displaying all single posts.
    *
    * @package WordPress
    * @subpackage Twenty_Eleven
    * @since Twenty Eleven 1.0
    */

    get_header(); ?>
    <div id="content" role="main">

    <?php while ( have_posts() ) : the_post(); ?>

    <?php get_template_part( 'content', 'single' ); ?>

    <nav id="nav-single">
    <span class="nav-previous"><?php previous_post_link( '%link', __( 'Previous Post', 'twentyeleven' ) ); ?></span>
    <span class="nav-next"><?php next_post_link( '%link', __( 'Next Post', 'twentyeleven' ) ); ?></span>
    </nav><!-- #nav-single -->

    <?php //comments_template( '', true ); ?>

    <?php endwhile; // end of the loop. ?>

    </div><!-- #content -->

    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    The "get_template_part" line doesn't matter, it just fetches the Single Post html. What matters is that navigation that goes to the previous and next post. It takes all posts into account and I want it to ignore posts from a specific category
  • It certainly looks like a loop to me! Just put the query above your 'while' statement.
  • I tried putting it in and it started displaying the 5 most current posts that don't include that category. So I added "&showposts=1" to the remove category and so its just showing the most current post.
  • Thanks for the Help TheDoc.

    I did a bit of reading the WP Codex (not sure why I didn't before) and apparently both "previous_post_link()" and "next_post_link()" have an option inside to exclude categories and to even keep the links limited to 1 category.

    Man, wordpress has a ton of options :)
  • I didn't know that one either, sweet!