Forums

[resolved] Recent Post From a Specific Category (7 posts)

  1. nickgarren
    Member
    Posted 1 year ago #

    I'm using the following code to pull one recent post and display it on my page.php file, it works fine but I want to make it pull from a specific category, how can do this?

    <?php $recentpost = new WP_Query("showposts=1"); while($recentpost->have_posts()) : $recentpost->the_post(); ?>

    Thanks

    Nick

  2. t-p
    Member
    Posted 1 year ago #

  3. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try:
    <?php $recentpost = new WP_Query("showposts=1&&category__in=xx");

    where xx = your chosen category id.

  4. nickgarren
    Member
    Posted 1 year ago #

    Try:
    <?php $recentpost = new WP_Query("showposts=1&&category__in=xx");

    where xx = your chosen category id.

    I couldn't get that piece of code to work, maybe I should have posted the entire snip-it of code I'm using on my page.php file

    here is my entire snip-it:

    <div id="content-inner">
    	          <h1 class="post-title">Featured Mountain Hardware Website</h1>
                       <div> &nbsp;</div>
                        <?php $recentpost = new WP_Query("showposts=1"); while($recentpost->have_posts()) : $recentpost->the_post(); ?>
                          <h1><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h1>
                           <div> &nbsp;</div>
                            <?php the_content(); ?>
                              <?php endwhile; ?>
                               </div>
  5. t-p
    Member
    Posted 1 year ago #

    try this:

    <?php
    //display 5 post from category 77
       $args=array(
       'showposts'=>5,
       'category__in' => array(77),
       'caller_get_posts'=>1
       );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><small><?php the_time('m.d.y') ?></small> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
    the_content(); //use the_excerpt() for a summary
    ?>
    <?php
    //this is necessary to show the tags
    global $wp_query;
    $wp_query->in_the_loop = true;
    endwhile;
    }
    ?>
  6. nickgarren
    Member
    Posted 1 year ago #

    Thanks t-p!

    problem solved!!

  7. t-p
    Member
    Posted 1 year ago #

    Glad you got it working. :-)

    Please mark this thread "resolved" using the dropdown in the right panel.

Topic Closed

This topic has been closed to new replies.

About this Topic