Forums

one post from a specific category (6 posts)

  1. ashwinnaidu
    Member
    Posted 3 years ago #

    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>
  2. stvwlf
    Member
    Posted 3 years ago #

    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()) :

  3. ashwinnaidu
    Member
    Posted 3 years ago #

    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.

  4. kapiljain.in
    Member
    Posted 3 years ago #

    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.

  5. ashwinnaidu
    Member
    Posted 3 years ago #

    not working for me..

  6. stvwlf
    Member
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.