WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [How To] Show Posts Related To Category Only? (22 posts)

  1. lozy
    Member
    Posted 6 years ago #

    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.

  2. Jeffrey Cannon
    Member
    Posted 6 years ago #

    This should work for you...

    <?php if (is_category('Loans')) {
    	query_posts("category_name=Loans&showposts=3"); ?>
    	<h2>Recent Posts</h2>
    	<ul>
    	<?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
    	<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>
    	</ul>
    <?php } ?>
  3. lozy
    Member
    Posted 6 years ago #

    Arh ha, thanks, but is there any way that it can do this on the fly without having to supply the category name in the code above.

    Cos let's say I have like 100 categories, that's a lot of repetitive coding.

    See, since the single.php will display the entire post, and let's assume there was a post about "Getting Cheaper Insurance", placed under the category of "Insurance", then would the code above not display any related posts because it's not in the Loans, so how would I get the posts that were only posted into the "Insurance" category to show up as recent posts there also?

    Can you see where I'm going with this?

    Again, thanks for the help. Much appreciated. :)

  4. lozy
    Member
    Posted 6 years ago #

    This link here was as close to what I mean,
    http://codex.wordpress.org/Template_Tags/query_posts

    But there isn't any way from what I can see, to determine what category that post was posted then show all of it's other posts in that category.

    But if I were to go to another post that was posted in another category, then the posts wont show because some of the code in that link above, and the example one above you kindly show, will not show any posts related to that category, but if I am going to be adding more categories later, it would make it quite cumbersome as the leading poster on that link above mentions to keep modifying the code.

    So I was wondering if there is like, any code that has the logic of like,

    if post = category, show category posts, limit to show number of posts.

    Then no matter what post was made, or category it was posted in, it will only show those related posts, down to a fixed number of related posts.

  5. Jeffrey Cannon
    Member
    Posted 6 years ago #

    Here's the code tweaked with some code found in the query_posts page you provided from the WordPress Codex:

    <?php
    $categoryvariable=$cat; // assign the variable as current category
    $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
    query_posts($query); // run the query
    ?>
    <h2>Recent Posts</h2>
    <ul>
    	<?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
    	<li>
    	<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    	<?php the_title(); ?>
    	</a>
    	</li>
    	<?php endwhile; ?>
    </ul>

    Is this closer to what you're talking about?

  6. Stretsh
    Member
    Posted 6 years ago #

    I have the same problem/issue. Basically, the issue is to find the category id of the category under which the current post is stored, outside the loop.

    $categoryvariable=$cat; // assign the variable as current category
    $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query

    In this case, we are looking for whatever the value of the '$cat' is, depending on the displayed post. Since it's empty here, you get the most recent posts from all the categories.

  7. Stretsh
    Member
    Posted 6 years ago #

    Problem solved for me. I think this should work:

    <?php
    $ID = $wp_query->posts[0]->ID;
    $categoryvariable = get_the_category($ID); // assign the variable as current category
    $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
    query_posts($query); // run the query
    ?>
    <h2>Recent Posts</h2>
    <ul>
    	<?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
    	<li>
    	<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    	<?php the_title(); ?>
    	</a>
    	</li>
    	<?php endwhile; endif; ?>
    </ul>

    I didn't test this particular code, but something similar worked for me

  8. rumblepup
    Member
    Posted 5 years ago #

    Just came up on this post, because I've been going nuts looking for this function.

    Are you sure there isn't a widget for this?

  9. Dunkkan
    Member
    Posted 5 years ago #

  10. knightkato
    Member
    Posted 5 years ago #

    Anyone got a solution to this issue?

  11. MichaelH
    Member
    Posted 5 years ago #

    What's the problem you are having knightkao?

  12. knightkato
    Member
    Posted 5 years ago #

    The problem, as I probably should have mentioned in my previous post, is that the solution Stretsh supplied is returning all the posts, not just for the active category.

    I've been looking around for quite some time now to find a simple way of listing all posts in the single.php and category.php from the Active category without any luck..,

    so if I have 3 categories with 5 posts each and then when i click post-1 in category-1, i want a list of all the other posts in category-1

    any ideas how I would go about doing that?

  13. MichaelH
    Member
    Posted 5 years ago #

    So to recap:
    When viewing a post (presented with single.php), I want to see all the posts that are in the FIRST category assigned that single post.

  14. knightkato
    Member
    Posted 5 years ago #

    Yes, a list of all the posts of the current Category. Not a specific category, but the current one. Clear? :)

  15. MichaelH
    Member
    Posted 5 years ago #

    I'm confused byt the terminology 'current Category'. If you are talking about a category archive, aren't you already just seeing the posts in that category?

    Review Category Templates and Template Hierarchy.

  16. knightkato
    Member
    Posted 5 years ago #

    current category, let's call it the active category instead.

    i'm on a single, i want to generate a list of all the post that are in the same category as the post i'm viewing and put it as a list in the sidebar.

    so if i go to post2 under category about, it shows me all the posts under category about in the sidebar, nothing else, and if i go to post4 that's in category work, it shows only the posts from work in the sidebar.

    that's about as clear as i can get.

  17. MichaelH
    Member
    Posted 5 years ago #

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

  18. Len
    Member
    Posted 5 years ago #

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

  19. knightkato
    Member
    Posted 5 years ago #

    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>
  20. knightkato
    Member
    Posted 5 years ago #

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

  21. thebadone411
    Member
    Posted 5 years ago #

    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?

    http://www.ashtrayreviews.com/reviews2/

    Thanks in advance

  22. pablo631
    Member
    Posted 5 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic