• Hello,

    I am using this code, but the code is returning all the posts even though I have specified showposts=1. I am using the code on index page. What am I doing wrong here.

    <ul>
    <?php $recent = new WP_Query("showposts=1&amp;cat=3"); while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
Viewing 5 replies - 1 through 5 (of 5 total)
  • WHERE are you using that code on the index page? all that code is going to do is create a listing of post titles. The main WP loop is still going to run – if this code is before the loop, the list will be before the posts, if after the loop the list will be after the posts.

    If you want that to run INSTEAD of the regular code you don’t want a wp_query command

    rather
    query_posts(“showposts=1&cat=3”);

    the the regular WP loop

    if (have_posts()): while (have_posts()) :

    Thread Starter ashwinnaidu

    (@ashwinnaidu)

    I’m using the code on the index page before the loop. I don’t want a list of posts. I am trying to pull the permalink of the latest post from a specific category.

    Just try by using WP_Query(“showposts=1&cat=3”) to WP_Query(“cat=3&showposts=1”)

    I have checked it on my temp blog, it works if we change the order of WP_Query parameters.

    Thread Starter ashwinnaidu

    (@ashwinnaidu)

    not working for me..

    Use this. make sure to use a single ampersand and not &amp; a m p ;

    <ul>
    <?php
    $recent = new WP_Query();
    $recent->query('showposts=1&amp;cat=3');
    while($recent->have_posts()) : $recent->the_post();?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>

    if it doesn’t work double check that 3 is the correct category ID code for your category

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘one post from a specific category’ is closed to new replies.