WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to show certain categories on category.php if it has a body class of x (11 posts)

  1. vikkineal
    Member
    Posted 1 year ago #

    Hiya

    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">
        <h1>
          <?php single_cat_title() ?>
        </h1>
        <?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(); ?>">
            <?php
    // Must be inside a loop.
    
    if ( has_post_thumbnail() ) {
        the_post_thumbnail('size-2');
    }
    else {
        echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/shared/placeholder.jpg" />';
    }
    ?>
            </a></div>
          <h2><a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
            </a></h2>
          <h3>
            <?php the_time('F jS, Y') ?>
          </h3>
          <?php the_excerpt(); ?>
          <p class="readMore"><a href="<?php the_permalink(); ?>">Read more</a></p>
          <div class="clearBoth"></div>
        </div>
        <?php } ?>
        <div class="clearBoth"></div>
        <div id="nav-below" class="navigation">
          <p class="floatRight">
            <?php posts_nav_link('','< Recent entries','Older entries >'); ?>
          </p>
          <div class="clearBoth"></div>
        </div>
      </div>
  2. tomontoast
    Member
    Posted 1 year ago #

    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?

  3. curlybracket
    Member
    Posted 1 year ago #

    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; ?>
  4. vikkineal
    Member
    Posted 1 year ago #

    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.

  5. vikkineal
    Member
    Posted 1 year ago #

    @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?

  6. vikkineal
    Member
    Posted 1 year ago #

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

  7. tomontoast
    Member
    Posted 1 year ago #

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

  8. vikkineal
    Member
    Posted 1 year ago #

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

  9. esmi
    Forum Moderator
    Posted 1 year ago #

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

  10. vikkineal
    Member
    Posted 1 year ago #

    Thanks Esmi - that was what I was beginning to think...

  11. esmi
    Forum Moderator
    Posted 1 year ago #

Topic Closed

This topic has been closed to new replies.

About this Topic