How can I make it so when the blog link in the nav bar is clicked it redirects to the most recent post?
I know how to display just the latest post on the blog page, but I want it to actually redirect to the post page itself as I’m displaying a full screen background image called in via a custom metabox on each post page.
Is there perhaps a PHP query and redirect function I can apply to the blog page template, or is there a better way?
UPDATE: the following redirect script works great but I just need a way to get the URL of the latest post:
$URL = "http://www.google.com";
wp_redirect( $URL, 301 );
exit;
1 Answer
You can use the get_posts() function:
<?php
/*
Template Name: Redirect
*/
$args = array(
'posts_per_page' => '1',
'post_type' => 'post'
);
$post = get_posts($args);
if($post){
$url = get_permalink($post[0]->ID);
wp_redirect( $url, 301 );
exit;
}