menu item to display the most recent post
I spent most of the day reviewing past questions and answers and have not found anything that addresses what I am trying to do. Maybe I didn't word my search correctly.
Lets say I have a category of posts for documents that reflect current committee members. I can create a WP menu item for a specific post with Wordpress However, users can submit new posts using the USP Pro plugin. Now the menu item needs to point to the most recently updated post. I want to avoid having to update the menu every time a new post is added.
I created a short code function in my functions.php file to get the most recent post for a category (short code argument). When I attempt to redirect to the permalink for that post, I get a blank page.
//[latestpost]
function get_latest_post( $atts) { extract( shortcode_atts( array( 'cat' => '', ), $atts, 'latestpost' ) ); $args = array( 'posts_per_page' => 1, // we need only the latest post, so get that post $ 'cat' => $cat, // Use the category id, can also replace with category_nam$ //'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(); $link=get_permalink($post); // echo $link; // echo $cat; if ( wp_redirect( $link) ) { exit; } } wp_reset_postdata(); } } add_shortcode('latestpost','get_latest_post')`
I put in a couple of echo statements to confirm that the function was getting the correct category ($cat) and permalink. They appear on the page but the page is not redirected.
Running Wordpress 5.1 with a twenty seventeen child theme.
Thanks for your help.
Topic recent-posts shortcode Wordpress
Category Web