• Resolved lozy

    (@lozy)


    Hi

    I was wondering if anyone knew how to show the recent posts that ONLY belongs to a category.

    So, let’s say for instance I had a post about “Home Loans” And I posted it into the category of “Loans”, when the post I made about “Home Loans” is clicked on, in the sidebar I wanted to have all the posts (or a fixed number of them) to only show up that were previously posted in the category of “Loans”.

    At present if I use this code (*[] are really <>):

    [?php get_archives(‘postbypost’, 50); ?]

    in my sidebar.php

    Then I get all the posts whether or not they were posted in say categories of “Loans” “Insurance” “Lawyers” etc. When ideally I only wanted to have the posts that show up in the category the “Home Loans” post was posted in.

    I’ve spent the last few hours trying to look for the php function that can allow this but I can’t seem to find any reference to how to do this.

    Can anyone help?

    Thanks in advance.

Viewing 6 replies - 16 through 21 (of 21 total)
  • MichaelH

    (@michaelh)

    And what if the post2 under category about has more than one category?

    Len

    (@lenk)

    <?php global $post;
    $categories = get_the_category();
    foreach ($categories as $category) :
    ?>
    <h3>More News From This Category</h3>
    <ul>
    <?php
    $posts = get_posts('numberposts=20&category='. $category->term_id);
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    <li><strong><a href="<?php echo get_category_link($category->term_id);?>" title="View all posts filed under <?php echo $category->name; ?>">ARCHIVE FOR '<?php echo $category->name; ?>' CATEGORY ?</a></strong></li>
    <?php endforeach; ?>
    </ul>

    This will spit out a list of the 20 most recent (configurable) entries of the particular category the post being viewed on single.php belongs to.

    Edit: of course if the post belongs to more than one category you will get multiple instances of the list.

    knightkato

    (@knightkato)

    The code fetches the right posts from the category, but the permalinks aren’t updating the single. It’s pretty strange, when I’m on post1 and click the link for post2, the url changes, but not the content.

    <?php global $post;
    $categories = get_the_category();
    foreach ($categories as $category) :
    ?>
    <h3>More News From This Category</h3>
    <ul>
    <?php
    $posts = get_posts('numberposts=20&category='. $category->term_id);
    foreach($posts as $post) :
    ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    <li><strong><a href="<?php echo get_category_link($category->term_id);?>" title="View all posts filed under <?php echo $category->name; ?>">ARCHIVE FOR '<?php echo $category->name; ?>' CATEGORY ?</a></strong></li>
    <?php endforeach; ?>
    </ul>
    </div>
    
    		<div id="content">
    
    <?php the_post() ?>
    			<div id="post-<?php the_ID() ?>" class="<?php sandbox_post_class() ?>">
    				<h2 class="entry-title"><?php the_title() ?></h2>
    				<div class="entry-content">
    <?php the_content() ?>
    
    <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'sandbox' ) . '&after=</div>') ?>
    				</div>

    Duuuh, needs to be in the sidebar of course. Works nicely!

    KnightKato – I’m running into the same issue you did but I don’t want the list to be in my sidebar. Any ideas? Anybody?

    https://www.ashtrayreviews.com/reviews2/

    Thanks in advance

    Using this method, is there a way to hide the current post from showing up in the list? So only the other related posts in the category are displayed. Not the current one?

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘[How To] Show Posts Related To Category Only?’ is closed to new replies.