Forums

[resolved] Display recent posts excluding certain categories (revised; disredgard previous) (8 posts)

  1. octonaut
    Member
    Posted 6 months ago #

    Hello!

    I have a static main page where I also display the last five recent posts. I want to exclude specific categories from this list.

    This is the code I'm currently using:

    <?php
    			global $post;
    			$cat=get_cat_ID('top-menu');
    			$count=0;
    			$myposts2=get_posts(array('post__not_in'=>get_option('sticky_posts'),'cat'=>-$cat,'showposts'=>5));
    			foreach($myposts2 as $post) :
    			$count++; ?>
    			<li type="circle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    			<?php endforeach;
    			if ($count >=10)
    			?>

    It might be pretty messy, I don't know. Where should I change/add the information to exclude a specific category from appearing?

    Or if someone is bored/kind enough to clean up what is probably an egregious block of code, I would be most appreciative. Thank you, WordPress community.

    I love you all.

  2. ChristiNi
    Member
    Posted 6 months ago #

  3. octonaut
    Member
    Posted 6 months ago #

    Thanks for the help, but I still can't get it to work!

    This is the code I'm currently using. I'm trying to exclude the category "journal", but it's not functioning properly.

    Here is the code I'm currently using:

    <?php query_posts('showposts=5&category_name=-journal'); ?>
    <?php while (have_posts()) : the_post(); ?>
            <li type="circle"><a href="<?php the_permalink(); ?>">
              <?php the_title(); ?>
              </a>  </li>         <?php endwhile; ?>

    Any ideas? Minus-ing out "journal" doesn't seem to exclude it from showing up . . . "journal" being the slug of that category.

  4. ChristiNi
    Member
    Posted 6 months ago #

    Hi octonaut,

    Two things to point out in the code example:

    In PHP, != means not equal (not -)

    Also, from the WordPress Codex, to exclude a category, use the exclude parameter:

    This parameter takes a comma-separated list of categories by unique ID, in ascending order.

    So, you will want to use the exclude parameter with the category ID (not the name).

    Hope this helps!

  5. wspencer
    Member
    Posted 6 months ago #

    If you use just the '-', I believe you have to use the category ID...not the slug.

    In the dashboard, go to your categories page. Hover over a category and look at the link in the status bar. It will have something like category_ID=12 or category&tag_ID=12....or something like that.

    Give that a whirl and see if it works for you.

  6. octonaut
    Member
    Posted 6 months ago #

    It worked. Thanks, wspencer & ChristiNi!

  7. wspencer
    Member
    Posted 6 months ago #

    You're very welcome!

  8. ChristiNi
    Member
    Posted 6 months ago #

    You're welcome octonaut! If you have time, please feel free to mark this topic resolved.

Reply

You must log in to post.

About this Topic