WordPress.org

Ready to get started?Download WordPress

Forums

Permalink to latest post in category? (5 posts)

  1. fireduck
    Member
    Posted 8 years ago #

    My reason for this is to create a photoblog within my existing blog. I have done this through creating a 'photo' category. I want to be able to put a link off my main page that will take you to the newest individual page in the 'photo' category. I don't want to use the category archive page displaying the latest post because I would still like to use it to display a listing of the photoblog entries. Is there any plugin/hack I can use to get something like ?cat=3&p=latest or similar functionality?

  2. fireduck
    Member
    Posted 8 years ago #

    Hmm, still no luck any hacks/plugins? I'm probably going to look into writing a hack/plugin for this soon. If anyone has any pointers or helpful hints for this I would greatly appreciate it.

  3. IsaacSchlueter
    Member
    Posted 8 years ago #

    Try this. Define this function somewhere (in a plugin, in your template somewhere, whatever you prefer.)

    Then, call latest_in_category_link('photos'); to display the link, or $link = latest_in_category_link('photos',false); to just return the value (if you want to do something with it.)

    <?php
    function latest_in_category_link($catname, $disp = true)
    {
    $blah = '';
    $my_query =& new WP_Query('category_name=photos&showposts=1');
    while( $my_query->have_posts() )
    {
    $my_query->the_post();
    $blah = '<a href="' . apply_filters('the_permalink', get_permalink($my_query->post->id)) .
    '" title="Latest post in the ' . htmlentities($catname) . ' category">' .
    apply_filters('the_title', $my_query->post->post_title, $my_query->post) . '</a>';
    }
    if( $disp ) echo $blah;
    return $blah;
    }
    ?>

    DISCLAIMER: This is untested and may cause big errors. If you have problems, just respond to this post, and I'll try to help.

  4. fireduck
    Member
    Posted 8 years ago #

    Thanks for the input. This could work for the short term. I'm hoping to set something up with a more permanent link so that people can bookmark the photoblog. I'm going to have to figure something out that processes before loading the page.

  5. IsaacSchlueter
    Member
    Posted 8 years ago #

    Aha.

    Well, you could create a custom template file for that category. Get the category ID number, and then go into your main template directory. Open up category.php (if it's there), or archive.php (if it's not) and save it as category-6.php, but replace "6" with the ID of the photo category.

    Then, change the code of that file to not loop through posts, but instead just show one and break out of The Loop. Since I don't have access to your template to tell you exactly how to do this, the super-quick and super-dirty way that will almost certainly work is to just put a break; right before the endwhile;.

    It's probably designed to loop through a bunch of posts, and usually that loop is wrapped in while( have_posts() ): the_post; and endwhile; Right before the endwhile;, you put a break; which says, "Break out of this loop, and don't continue." Since it already finished the first run through, it'll just show one post, and then quit.

    Then, the link to give is just http://your.site.com/category/photos/

Topic Closed

This topic has been closed to new replies.

About this Topic