Support » Fixing WordPress » How to show certain categories on category.php if it has a body class of x

  • Resolved vikkineal



    Hoping someone can help.

    I’ve been working on category.php to show posts from 2 categories only. That works fine, however now when i try to see an archive of all posts from a specific category, its showing both.

    I think I need to do a ‘if has this body class, show these posts’ kind of thing… but am unsure how (PHP is not great). The body class of custom models is category-custom-models

    My existing code is…

    <div id="rightColumn">
          <?php single_cat_title() ?>
        <?php $posts = get_posts('category_name=latest-news,custom-models'); foreach($posts as $post) { ?>
        <div class="newsListing">
          <div class="imageContainer"><a href="<?php the_permalink(); ?>">
    // Must be inside a loop.
    if ( has_post_thumbnail() ) {
    else {
        echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/shared/placeholder.jpg" />';
          <h2><a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
            <?php the_time('F jS, Y') ?>
          <?php the_excerpt(); ?>
          <p class="readMore"><a href="<?php the_permalink(); ?>">Read more</a></p>
          <div class="clearBoth"></div>
        <?php } ?>
        <div class="clearBoth"></div>
        <div id="nav-below" class="navigation">
          <p class="floatRight">
            <?php posts_nav_link('','< Recent entries','Older entries >'); ?>
          <div class="clearBoth"></div>
Viewing 10 replies - 1 through 10 (of 10 total)
  • This code will show posts from the lastest-news and custom-models categories regardless of the actual category page the user is on.

    Could you explain exactly what you want to do. Do you want to only allow users to see posts when they are on either the latest-news or custom-models category pages?

    You would need to make a if/else statement.
    So if you are in this particular category, show specific posts,
    else show only the posts of the current category.

    <?php if (have_posts()) : ?>
        $category = get_the_category();
        $current = get_query_var('cat');
         <? if ($current == "THE ID OF YOUR SPECIAL CATEGORY") {
                // add your code here.
          } else {
              // normal category
              while (have_posts()) : the_post(); ?>
                  <? the_title(); ?>
                  <? the_content(); ?>
             <? endwhile;
      <?php else : ?>
        <?php get_template_part('404'); ?>
      <?php endif; ?>

    Yes – my above code is to show posts from either category when landing on the main landing page.

    The main ‘news’ landing page has a class of category-latest-news and shows all posts – which is what I want.

    However, I was wondering how the above code could be changed so that it would show only the posts from custom models if the body class was category-custom-models.

    @curlybracket – there’s a 3rd category that i wouldn’t want to output as it’s in a separate list to the left of the page How would i exclude that category using your code?

    Perhaps an easier way would be to make use of archive.php and separate them all out on to that…

    What do you mean by body class? Do you mean <body class="category1"> ?

    Yes, the body class that WordPress assigns to the body tag.

    You need to filter on the category itself – not the class.

    Thanks Esmi – that was what I was beginning to think…

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How to show certain categories on category.php if it has a body class of x’ is closed to new replies.