WordPress.org

Ready to get started?Download WordPress

Forums

Display latest post from category (11 posts)

  1. matthisco
    Member
    Posted 2 years ago #

    Can someone tell me how to do this please?

    I need to display the full post.

    Thankyou

  2. alchymyth
    Forum Moderator
    Posted 2 years ago #

    where?

    in what context?

    what theme are you using?

  3. matthisco
    Member
    Posted 2 years ago #

    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.

  4. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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; ?>
  5. matthisco
    Member
    Posted 2 years ago #

    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; ?>
  6. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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; ?>
  7. matthisco
    Member
    Posted 2 years ago #

    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.

  8. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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?

  9. matthisco
    Member
    Posted 2 years ago #

    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

  10. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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

  11. matthisco
    Member
    Posted 2 years ago #

    That works great!!!

    Thanks for all your help!!!! :-)

Topic Closed

This topic has been closed to new replies.

About this Topic