WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Single (latest) post on page, with comments? (10 posts)

  1. Snaggletooth
    Member
    Posted 4 years ago #

    Hello,

    I want to place exactly one post on a page. It would be nice if this post could automatically be the latest one but updating manually would be OK too.
    What I have done so far is
    - creating a new custom page template and assigned that to the page that should display the post.
    - setting that page to be the posts page in Settings->Reading and allow only one post to be displayed there.

    Now what happens is that I do indeed see the latest post there, BUT:
    - it uses index.php as template instead of my custom template.
    - it shows the post without comments and the submit form (comments_template() doesn't work)

    I have also tried to solve the problem by not setting that page as the 'posts page' but by retrieving the lastest post with get_post($id).
    This basically also works but again I am out of luck with the comments because comments_template() gives me the comments for that page and not for the post on it.

    Can someone help me please - I have searched around but just couldn't find any solution here or elsewhere...

    Thank you.

  2. vtxyzzy
    Member
    Posted 4 years ago #

    As you found out, you can't use a template on the Blog page. But, you can create a template for another page that just displays the one most recent post. The exact coding will depend on your theme, but the query should be similar to this:

    $args = array(
       'caller_get_posts' => 1,  // Don't put stickies at top
       'posts_per_page' => 1,
    );
    query_posts($args);

    This should be followed by the rest of the code for the Loop.

  3. Snaggletooth
    Member
    Posted 4 years ago #

    Thank you. That was very helpful. The problem is still not completely solved though.
    Calling comments_template() does not work so I tried get_template_part('comments') that for some reason only returns the submit form. Now calling get_comments() lists the comments but I would need to place all the html from comments.php again (what I don;t feel like doing).

    Another problem is, that the next_posts_link() and previous_posts_link() don't work anymore. They return a url like this: .../?page_id=6&paged=2

    What I did for a workaround now is using the query_posts() function from above to retrieve the permalink of the latest post and using this link in the navigation. Like this the content is displayed through single.php so that I have the full post, the comments and the previous/next post links. Disadvantage of this is that the url in the main navigation is always changing with a new post.

    By the way - the theme I am working with is a customized Empty Canvas

    Does someone have an idea how to tackle the problem of displaying the post on a page using the comments.php template and with working previous/next links?

  4. vtxyzzy
    Member
    Posted 4 years ago #

    To allow pagination to work, use this:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array(
       'caller_get_posts' => 1,  // Don't put stickies at top
       'posts_per_page' => 1,
       'paged' => $paged,
    );
    query_posts($args);

    Calling comments_template() should work if used in the Loop. Did you place it outside the Loop, by chance?

  5. Snaggletooth
    Member
    Posted 4 years ago #

    Thanks again. Pagination works now. And you are right - comments_template() was placed outside the loop. But now that it's inside it still won't work (sigh).

  6. vtxyzzy
    Member
    Posted 4 years ago #

    Can you post a few lines of code containing your loop, or copy the file to the pastebin and post that link here, so the code can be examined?

  7. Snaggletooth
    Member
    Posted 4 years ago #

    Sorry this is all going a bit slow...
    Here is the code:
    http://wordpress.pastebin.com/4EL76b7a

  8. vtxyzzy
    Member
    Posted 4 years ago #

    OK - please try setting $withcomments = 1;, like this

    <?php $withcomments = 1; ?>
    <?php comments_template(); ?>
  9. Snaggletooth
    Member
    Posted 4 years ago #

    YES! It works. Thank you so much. The documentation is not very intuitive. What a luck there are people like you. Have a great weekend.

  10. vtxyzzy
    Member
    Posted 4 years ago #

    You are welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic