How can I link to the first post of each category in a dynamically created category list in wordpress?
-
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
-
Answer:
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.
msbrauer at Ask.Metafilter.Com Visit the source
Other answers
<?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 http://codex.wordpress.org/Template_Tags/get_posts. Good luck!
daboo
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.
msbrauer
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)
msbrauer
Related Q & A:
- How can I link to the latest post on Tumblr?Best solution by Stack Overflow
- How can I communicate Raspberry Pi and Arduino (in both ways) using a 10-15m distance wires?Best solution by Arduino
- How can I link Yahoo groups?Best solution by Yahoo! Answers
- How can I print wirelessly from a laptop to a printer without a router?Best solution by answers.yahoo.com
- How can I link my Yahoo mail to my Facebook account?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.