1

I have a menu item that always links to the latest post from a specific category. The link is generated dynamically. So every time I create a new post, the menu item URL changes.

I need a CTA Button that links to this menu item, no matter what the URL is. Something like this:
<a href="link to menu item 'new-deals'">New Deals</a>

Is it possible to do this?

Thanks in advance!

1

You can use the wp_get_recent_posts function in your own custom function.

Something like this should work:

function get_recent_post_link(){
    // Restrict to latest post
    $args = array( 'numberposts' => '1' );
    // Get the post
    $latest_post = wp_get_recent_posts( $args );
    foreach( $latest_post as $the_post ){
        return get_permalink($the_post["ID"]);
    }
}

You can the call the function in your anchor:

<a href="<?php echo get_recent_post_link(); ?>">Latest</a>

Note, I have not tested this, please use as a guideline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.