1

I am trying to figure this out from similar questions asked, but I am not sure I am finding what I need.

I have a utility menu and a page that should have links to go to the latest post of a specific category, which is our weekly newsletter.

Basically the Utility Menu on the top always says Blog (which links to the list of blog posts) and then Weekly Newsletter (which I would like to link directly to the latest post for Weekly Newsletter category).

Then in a page (under our learning center) I also mention the Weekly Newsletter, and would like to provide a link there to our latest issue.

Is there a way to accomplish both? I know I may need different functions - my issue is linking to something within a page, where I am not technically allowed to enter PHP code.

1

You could go about this in a few ways. Here are two options:

Option 1 - simplest. Since your weekly newsletter is already in its own Category, just link to that category. Your category template should in theory include the most recent post at the top of the page. You can always tweak the category template to show the full post of the most recent newsletter and then just links to past copies.

Option 2 - a little more complicated but fits your requirements more precisely. Create a shortcode.

In either your theme's functions.php or in a custom plugin,

add_shortcode('latest_newsletter', 'get_latest_newsletter');
function get_latest_newsletter($atts, $content = null) {
    $args = array(
        'posts_per_page' => 1,
        'cat' => '4' // replace this number with your category's ID
    );
    $posts = get_posts($args);
    foreach($posts as $post) {
        $latest_post = '<a href="' . get_permalink($post) . '">' . $post->title . '</a>';
    }
    return $latest_post;
}

For the Page, you can put your new shortcode [latest_newsletter] directly into the WP content editor, or if you want it to appear somewhere else, you can create a custom Page Template and use do_shortcode where you want the link to appear, such as in a sidebar or something custom.

For the Menu, use the "Shortcodes in Menus" plugin and then paste the same shortcode [latest_newsletter] into the menu under the Blog menu item.

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.