Support » Fixing WordPress » Display latest post from category

Viewing 10 replies - 1 through 10 (of 10 total)
  • Michael

    (@alchymyth)

    Forum Moderator

    where?

    in what context?

    what theme are you using?

    Thanks for the reply.

    I just need to display the latest post in full from a certain category of posts, the title, text etc.

    Im using the 2010 theme.

    Michael

    (@alchymyth)

    Forum Moderator

    http://codex.wordpress.org/Class_Reference/WP_Query

    example:

    <?php $cat_id = 27; //the certain category ID
    $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id));
    if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();
    //here whatever you need to display; possibly copy parts from loop-single.php//
    the_title();
    the_content();
    //etc.
    endwhile; endif; ?>

    Thanks for your reply.

    I’ve used your code, but just get a blank whit screen

    Can you please help?

    Here is my code:

    <?php $cat_id = 7; //the certain category ID
    $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id));
    if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>
    
    <h1><?php the_title(); ?></h1>	
    
    <?php the_content(); ?>
    
    <?php endwhile; endif; ?>
    Michael

    (@alchymyth)

    Forum Moderator

    my mistake – I missed to close a bracket;

    try:

    <?php $cat_id = 7; //the certain category ID
    $latest_cat_post = new WP_Query( array('posts_per_page' => 1, 'category__in' => array($cat_id)));
    if( $latest_cat_post->have_posts() ) : while( $latest_cat_post->have_posts() ) : $latest_cat_post->the_post();  ?>
    
    <h1><?php the_title(); ?></h1>	
    
    <?php the_content(); ?>
    
    <?php endwhile; endif; ?>

    Thats great many thanks.

    However, the page does not break this time, but the latest post does not display.

    I am using pages not posts, does that matter?

    Thanks again, really appreciate your help.

    Michael

    (@alchymyth)

    Forum Moderator

    am using pages not posts, does that matter?

    what are you trying to show?

    a static page (the latest published page)?
    -static pages do not have categories.

    or the latest post of a certain category?

    but the latest post does not display

    do you have a category in your site with the category ID 7 and at least one published post in it?

    Thanks for your reply.

    I have a parent page with subpages. I’d like to show the latest published subpage on the parent page.

    Sorry if thats confused matters.

    Any help much appreciated. Thanks again

    Michael

    (@alchymyth)

    Forum Moderator

    Sorry if thats confused matters.

    not at all – it is just a totally different question.

    you can use an adapted query:

    <?php if( is_page() ) :
    $latest_child_page = new WP_Query( array('posts_per_page' => 1, 'post_type' => 'page', 'post_parent' => $post->ID));
    if( $latest_child_page->have_posts() ) : while( $latest_child_page->have_posts() ) : $latest_child_page->the_post();  ?>
    
    <h1><?php the_title(); ?></h1>	
    
    <?php the_content(); ?>
    
    <?php endwhile; endif;
    endif; //ends 'if( is_page() )'// ?>

    basically, when you are on a static page (any static page), the code will try to get the latest child page of that page.

    -alternatively, try work with: http://codex.wordpress.org/Function_Reference/get_pages

    That works great!!!

    Thanks for all your help!!!! 🙂

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Display latest post from category’ is closed to new replies.