6

I want to display in page the latest post content from a category.

For example, the category foo has the following posts:

    1. Hello World
    1. Hello Mars
    1. Foo bar

Considering Foo Bar the latest article from foo category, its content should be rendered in a page:

<title>
<content>

Where <title> is Foo bar and <content> is the post content.

How can I do this?


I'm struggling to implement the @Pieter's answer. I added these lines in functions.php:

function latest_post() {  
    $args = array(
       'posts_per_page' => 1, // we need only the latest post, so get that post only
       'cat' => '4' // Use the category id, can also replace with category_name which uses category slug
    );

    $str = "";
    $posts = get_posts($args);

    foreach($posts as $post):
       $str = $str."<h2>".$post->title."</h2>";
       $str = $str."<p class='post-content-custom'>".$post->content."</p>";
    endforeach;

    return $str;
}

add_shortcode('latest_post', 'latest_post');

In the page I do:

[latest_post]

However, no error appears, but the post content doesn't appear.

2 Answers 2

9

You can make use of WP_Query to call the lastest post from the category and display it. Have a look at the category parameters. By default, WP_Query uses post as the post type and orders post by post date, so we can exclude that from the query. If you need something else, you can just define them in you arguments

You can basically try something like this

$args = array(
    'posts_per_page' => 1, // we need only the latest post, so get that post only
    'cat' => 'ID OF THE CATEGORY', // Use the category id, can also replace with category_name which uses category slug
    //'category_name' => 'SLUG OF FOO CATEGORY,
);
$q = new WP_Query( $args);

if ( $q->have_posts() ) {
    while ( $q->have_posts() ) {
    $q->the_post();        
        //Your template tags and markup like:
        the_title();
    }
    wp_reset_postdata();
}

This should provide you with a base, you can modify, customize and use it as you like. If you are unsure of the parameters and usage, check out the WP_Query codex page for assistance

EDIT

I'm really not sure why you decided to reinvent the wheel and to go with get_posts where as I have showed you a working example of how to use WP_Query. Your use of get_posts in conjuction with the WP_Post properties is completely wrong

  • The WP_Post properties is unfiltered, so the output from this is totally unfiltered and will not look the same as the output from the template tags like the_title() or the_content(). You have to use the appropriate filters on those properties

  • title and content is invalid properties of WP_POST. The other answer is completely wrong. It is post_title and post_content

  • You can make use of the template tags as normal by just using setup_postdata( $post ); and then just using wp_reset_postdata() afterwards

You can try the following

function latest_post() {  
    $args = array(
       'posts_per_page' => 1, // we need only the latest post, so get that post only
       'cat' => '4' // Use the category id, can also replace with category_name which uses category slug
    );

    $str = "";
    $posts = get_posts($args);

    foreach($posts as $post):
       $str = $str."<h2>". apply_filters( 'the_title', $post->post_title) ."</h2>";
       $str = $str."<p class='post-content-custom'>". apply_filters( 'the_content', $post->post_content ) ."</p>";
    endforeach;

    return $str;
}

add_shortcode('latest_post', 'latest_post');
4
  • Where should I add this code? This is still unclear to me... Commented Mar 9, 2015 at 8:54
  • 1
    You need to add it in your template where you need to display this post. Not to be rude or ugly, but we expect you to at least know the basics. :-) Commented Mar 9, 2015 at 8:59
  • See my edit. From what I see the size of the array is 1 which is expected, however I don't why the function doesn't return the expected result. Can you check the code (and maybe adding a working version in your answer)? Thanks! Commented Mar 11, 2015 at 19:56
  • Please see my update Commented Mar 12, 2015 at 8:09
-1

You can do something like this:::

 $args = array(
        'post_type' => '__post_type__',
        'posts_per_page' => 1,
        'orderby' => 'date',
        'order' => 'ASC'

        );

$posts = get_posts($args);
foreach($posts as $post):
 echo $post->ID;
 echo $post->title;
 echo $post->content;
endforeach;
3
  • Where shuold I add this? In the page content (editing in the text mode)? Commented Mar 9, 2015 at 8:44
  • in the template, template part or functions file if you are using shortcuts Commented Mar 9, 2015 at 8:49
  • Can you edit your post with the complete steps how to integrate this? I'm not the best in PHP, but following the steps should work. Commented Mar 9, 2015 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.