Support » Fixing WordPress » Make a link point to the most recently published post of a certain category

  • Resolved markellison


    My work-in-progress site:

    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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi


    Forum Moderator

    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.

    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!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Make a link point to the most recently published post of a certain category’ is closed to new replies.