Forums

[resolved] get latest post id by category (3 posts)

  1. jay.weeks
    Member
    Posted 2 years ago #

    i want to link to the latest post for four different categories.
    can't seem to get it to work

    <?php
    	$nc = get_cat_id('news');
    	query_posts('cat=$nc');
    	?>
    <?php
    	$latestNewsPost = get_posts('numberposts=1&category=$nc');
    	$latestNewsPostID = $wp_query->$latestNewsPost->ID;
    	?>
    
    <div class="home-header" id="news-head"><a title="news" href="<?php echo get_permalink($latestNewsPostID); ?> ">news</a></div>
  2. songdogtech
    Member
    Posted 2 years ago #

    This is what I use to show the permalink of the latest post in "my_category", and in one instance, I use several of these loops on one page to show the latest post in three different categories. Seems like each new query doesn't conflict with any of the others.

    <?php $my_query = new WP_Query('category_name=my_category&showposts=1'); ?><?php while ($my_query->have_posts()) : $my_query->the_post(); ?><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><?php endwhile; ?>

  3. jay.weeks
    Member
    Posted 2 years ago #

    cool, seems to be working

Topic Closed

This topic has been closed to new replies.

About this Topic