How can I link to the first post of each category in a dynamically created category list in wordpress?
October 21, 2008 6:18 PM   Subscribe

In a Wordpress-powered site, how can I create a list of categories that link to the first post in that category rather than the category archive?

I'm using wordpress as a cms for a portfolio site and I'd like to create a dynamic list of categories with each category as a link to the first post in that category.

I currently use wp_list_cats(); to list the categories I want, but each link goes to the category archive page. I've temporarily gotten the behavior I want by limiting the category template to display only 1 post per page. If posts move around or get changed, however, the permalinks change for particular content that may be bookmarked or linked to elsewhere (i.e., what was at blah.com/?cat=26&paged=2 is now at blah.com/?cat=26&paged=5)

I'm not scared of code, just running into some difficulty determining exactly how to implement what I want; it seems like I could take a list of the category ids in the site, call up the posts for each category, put the first post id from each category into an array associated with the category names, and then print out the list of category names using the first post id as a link.

Is there an easy way to do this? A hard way?

I'm using wordpress 2.5 and higher
posted by msbrauer to Computers & Internet (4 answers total) 1 user marked this as a favorite
 
Response by poster: I should also say that scaling is not an issue. I could imagine my proposed solution would involve an undesirable burden on the server if there were 100 categories involved, but in my usage, 20 categories is the maximum.
posted by msbrauer at 6:21 PM on October 21, 2008


<?php $postslist = get_posts('category=1&numberposts=1&order=DESC&orderby=post_date');
     foreach ($postslist as $post) :
     setup_postdata($post); ?>
     <h1><strong><a href="<?php the_permalink() ?>" rel="bookmark">LINK</a></strong></h1>
<?php endforeach; ?>

You can configure that to do a lot of stuff, depending on the variables in get_post(). Good luck!
posted by daboo at 8:16 PM on October 21, 2008


Response by poster: Thanks for the help daboo. I've done quite a bit of manipulation with get_post() already. What's you show is easy enough to do, but what I'd really like is a list of categories that operates this way without me having to hand-code ( ``get_posts('category=1&numberposts=1&order=DESC&orderby=post_date');'' in your example) the list. I suppose the solution to that would be a similar foreach using each category id from the function: get_all_category_ids(); and then do a conditional inside that for categories that I don't want to include.

While your code wasn't exactly what I needed, it may have helped me see the forest for the trees.
posted by msbrauer at 4:10 PM on October 22, 2008


Response by poster: Finally got something working. I had to do the runaround with wp_list_categories because I'm using a category ordering plugin (My_Category_Order) which only affects the order of categories when called by that function.

Ended up using the following code:

[?php $categories = wp_list_cats('orderby=order&echo=0&style=none&exclude=' .$exclude_categories. '');
$category_list = strip_tags($categories, "[br]");
$cat_array = explode("[br /]", $category_list);

foreach ($cat_array as $category_name) :


$postslist = query_posts('category_name=' .$category_name. '&numberposts=1');
foreach ($postslist as $post) :
setup_postdata($post);

?]

[li][a href="[?php the_permalink() ?]" rel="bookmark"][?php echo $category_name; ?][/a][/li]

[?php

endforeach;

endforeach;?]

(I couldn't figure out how to post html code here, so I replaced all the < and > with [ and ] ; the exclude_categories bit is a string variable that has a list of categories I don't want to display in this list)
posted by msbrauer at 11:33 PM on October 26, 2008


« Older How to search for anything when all else has...   |   Get on up...and tell me where the bass is at. Newer »
This thread is closed to new comments.