[resolved] Make a link point to the most recently published post of a certain category (4 posts)

  1. markellison
    Posted 5 years ago #

    My work-in-progress site: http://bit.ly/al4Jg3

    This site is for literary magazine. Each physical issue is presented on the website as a WP post, with one post per issue.

    The current issue is promoted on the righthand sidebar with its title and featured image. This is achieved by assigning to each issue post the category of "issues" and using the plugin Category Thumbnail List (a sidebar widget) to display this category's most recently published post. Hooray!

    My problem: I'd like the navigation menu link "Current Issue" to point to the exact same thing as the sidebar widget, eg. to the most recently published post under the category "issues". I'd like this to update automatically, just like the sidebar widget does.

    Can someone please help? Is there a code snippet that would do the trick?

    Many thanks!

  2. esmi
    Forum Moderator
    Posted 5 years ago #

    Something like:

    global $post;
    $myposts = get_posts('numberposts=1&category_name=Issues');
    foreach($myposts as $post) :
        $issue_link = '<a href="' . get_permalink() . '">Current Issue</a>';
    endforeach; ?>

    should provide you with the current issues link in a variable called $issue_link that you may then be able to add to your menu.

  3. markellison
    Posted 5 years ago #

    Thanks esmi, I tried that and although it didn't really work, it pointed me in the right direction and I ended up doing this:

    <?php query_posts('category_name=issues&showposts=1'); ?>
    	<?php while (have_posts()) : the_post(); ?>
            	<li id="current"><a href="<?php the_permalink(); ?>"></a></li>
            <?php endwhile; ?>

    Works great!

  4. markellison
    Posted 5 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic