[Support request] Hook latest post

Home Forums Support [Support request] Hook latest post

Home Forums Support Hook latest post

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #935208
    Kle

    Hi,

    Is it possible to put a snippet of the latest post (only the post title is fine) anywhere on the website using a hook?

    #935246
    David
    Staff
    Customer Support

    Hi there,

    you could add this PHP snippet to the a Hook, make sure to Check Execute PHP:

    <?php
    $args = array( 'numberposts' => 1 );
    $lastposts = get_posts( $args );
    ?><div class="latest-post"><?php
        foreach($lastposts as $post) : setup_postdata($post); ?>
    	    <h2><a class="latest-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    	    <?php the_excerpt(); ?>
        <?php endforeach; ?>
    </div>
    #935265
    Kle

    Hi David,

    It didn’t worked for me, displayed the title of the page. And I am actually trying to put it on the header “above header”, maybe with an Element would be better?

    #935519
    David
    Staff
    Customer Support

    You can just add that code to a Hook Element – if you want it inside a Header Element then this code that Tom provides creates a shortcode to display the very latest post:

    https://generatepress.com/forums/topic/latest-post-in-the-header-element/#post-876757

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.