WTFranklin Posted January 12, 2011 Share Posted January 12, 2011 Hello, As far as I can tell getting the slug from a category should be easy and outside of the loop I can get the category slug or name only once in a foreach loop using variable assigned with wp_get_recent_posts(). I'm making a featured posts section with the five most recent posts and I'm trying to get the slug from these first five posts because I'm trying to make an array out of the three categories I have with a number value of how many posts belong to each category getting pulled, that way the rest of the front page won't display duplicate posts if it's not a part of the first five. My main problem is since I can only seem to get the slug once for only one of the categories I can't populate my array with these values correctly. So what I've been using, unsuccessfully, in my foreach loop is: $cat = get_the_category($main['ID']);//$main['ID'] being the post it's pulling $parent = get_category($cat->category_parent); $parentName = $parent->cat_name; For testing reasons I only tried doing a print_r($mainArray) which won't print anything on the screen and var_dump($mainArray) which prints NULL five times. So, the problem is nothing seems to be assigning to it, but I can't seem to bridge that gap with a WP function. I hope I make sense with that. Any help is greatly appreciated. Thanks! Quote Link to comment Share on other sites More sharing options...
WTFranklin Posted January 13, 2011 Author Share Posted January 13, 2011 I ended up figuring this out. What I was doing was pretty specific, but if anyone needed something like this, getting the slug is pretty easy after all if you're trying to use the wp_get_recent_posts() function. Here's basically how I got this to work... $mainPost = wp_get_recent_posts(5);//if I remember right, this function will default to 5 posts... $catCount = array( 'yourCat1' => 0, 'yourCat2' => 0, 'yourCat3' => 0); foreach($mainPost as $main) { //code formatting your posts... sorry to leave what I wrote out, just wanted to put in the basic idea foreach((get_the_category($main['ID'])) as $cat) { $catCount[$cat->slug]++; } } I ended up using the $catCount array this way to offset the rest of the posts on the front page in the_loop() because my client wanted the first three posts from their three main categories after the first five recent posts were pulled. I used it to offset the posts_per_page so there weren't duplicate posts showing up (which I've heard is bad for SEO purposes, especially with Google). Anyhow, I hope that might help someone else having the same issue. -Frank Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.