• Hello,
    I’m trying to display some king of related posts. I need the last 10 posts from article’s category. I have this code, but this the lat 10 posts posted, not from my current category. Does anyone have any sugestion for me?

    Thank you.

    <div id="leeft">
    
    <?php
    $categories = get_the_category();
    $catquery = new WP_Query( 'cat=$categories&posts_per_page=10' );
    while($catquery->have_posts()) : $catquery->the_post();
    ?>
    <ul>
    <li><span style="font-size:18px;"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></span>
     <center><?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?></center>
     </li>
    </ul>
    </li>
    </ul>
    <?php endwhile; ?>
    
    </div>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The function get_the_category() returns an array of data. The ‘cat’ parameter needs a single category id. You can get the id of the first category by changing this:

    <?php
    $categories = get_the_category();
    $catquery = new WP_Query( 'cat=$categories&posts_per_page=10' );

    to this:

    <?php
    $categories = get_the_category();
    $cat_id = $categories[0]->term_taxonomy_id;
    $catquery = new WP_Query( "cat=$cat_id&posts_per_page=10" );

    Notice the use of double quotes around the parameter list.

    Thread Starter RedPitbull

    (@redpitbull)

    Hello,
    Thank you for you answer.
    That works just fine but just for posts from main categories.
    In the posts that are posted in a subcateogry, dont’t show anything. What do you think?

    Try changing this:

    <?php
    $categories = get_the_category();
    $cat_id = $categories[0]->term_taxonomy_id;

    to this:

    <?php
    $cat_id = get_query_var('cat');
    Thread Starter RedPitbull

    (@redpitbull)

    Doing that I get what I want only in category page.

    In the posts pages I still get the last titles posted on the blog, not just on their cateogry.

    Sorry, wasn’t thinking clearly. I really don’t think there is an easy way to do what you want when posts have more than one category.

    The only way I can think of right now would be to add a Custom Field to hold the category.

    Thread Starter RedPitbull

    (@redpitbull)

    I see. Then I guess that my best option is to make it display last posts from a specific cateogry. I did this using

    <?php
    $catquery = new WP_Query( 'cat=1749&&posts_per_page=10' );
    while($catquery->have_posts()) : $catquery->the_post();
    ?>

    Thank you for you help!

    LE. Do you think that there is any way to make it random? I mean, if a user see 5 pages from that category, will be boring to see the same related posts..

    You can add the ‘orderby=rand’ parameter to the query:

    <?php
    $catquery = new WP_Query( 'cat=1749&posts_per_page=10&orderby=rand' );
    while($catquery->have_posts()) : $catquery->the_post();
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display posts from category’ is closed to new replies.