Archived topic
Hook latest post
3 replies · Started by Kle on June 19, 2019
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?
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>
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?
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