Support » Fixing WordPress » [How To] Show Posts Related To Category Only?

  • 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 15 replies - 1 through 15 (of 21 total)
  • 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 } ?>

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

    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.

    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?

    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.

    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

    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?

    knightkato

    (@knightkato)

    Anyone got a solution to this issue?

    MichaelH

    (@michaelh)

    What’s the problem you are having knightkao?

    knightkato

    (@knightkato)

    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?

    MichaelH

    (@michaelh)

    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.

    knightkato

    (@knightkato)

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

    MichaelH

    (@michaelh)

    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.

    knightkato

    (@knightkato)

    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.

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