Solved

Wordpress: Get/display latest post for each category

Posted on 2013-06-01
12
452 Views
Last Modified: 2013-06-03
Hey Experts!!

I have a test site here: http://goo.gl/BSmCK

I am listing each category on the home page. When you hover over the image I want to display the latest post. No problem there.

The problem is that it's duplicating posts across all categories. For example:
If I'm in category A and it has post A1. I'll have A1 showing up in category B, C etc.

I can't figure out where I'm going wrong. Is it in my foreach loop? Code below.

Thanks in advance

<?php 
					$args = array(
					  'orderby' => 'id',
					  'hide_empty' => 0,
					  'parent' => 0,
					  'exclude' => '1'
					  );
					$categories = get_categories( $args );
					foreach ( $categories as $category ) {  
						$posts=get_posts($args);
      						if ($posts) {
        							foreach($posts as $post) {
          								setup_postdata($post); ?>
									 <li>
										 <a href="<?php echo get_category_link($category->term_id); ?>">
										 	 <img src="<?php echo z_taxonomy_image_url($category->term_id); ?>" />
										 	 <?php echo $category->cat_name; ?>
										
											 <div class="cat-hover">
											 	<h1>Latest Post:</h1>
												<h2><?php the_title(); ?></h2>
												<h4><?php the_date('m-d-Y'); ?></h4>
											 </div>
										</a>	
									 </li>
				 				<?php }; //end post foreach?> 
							 <?php } else{}; //end if posts?>
					<?php }; //end foreach category?>

Open in new window

0
Comment
Question by:LZ1
  • 7
  • 5
12 Comments
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39215171
Too many loops. The inner loop loops through all posts, and displays the given category as many times as there are posts. And that happens for all categories. That's the first issue. (You have a total of 4 posts? hence each category is displayed 4 times.)

The second issue seems to be that the posts are not associated with the category being iterated over. I can't see anywhere in the snippet a check for whether the post belongs to the category.

So you don't need the inner loop. You just need to associate the latest post with the category in the outer loop.

If you need more specific help, let us know...
0
 
LVL 30

Author Comment

by:LZ1
ID: 39215219
Definitely need some more help with the loops then.

Your saying just 1 loop and make sure that the post is associated with the category.  Can you help me out with some code?
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39215223
yes. I'm running around like a headless chicken at the moment, so it might be a little while...
0
 
LVL 30

Author Comment

by:LZ1
ID: 39215253
No problem. It is very much appreciated, thank you.
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39215991
hi LZ1,

I haven't had time to write it up with your actual code, but this will display the latest post title in each category.

I'll try to get a look at it later with your actual code.

<?php 
					$args = array(
					  'orderby' => 'id',
					  'hide_empty' => 0,
					  'exclude' => '1',
					  'child_of' => 0
					  );


					$categories = get_categories( $args );
					
					foreach ( $categories as $mycategory ) {  
						//var_dump($mycategory);
						$catid = $mycategory->cat_ID;

						echo $mycategory->cat_name . '<br>';
						query_posts('cat='. $catid.'&posts_per_page=1');


						 while (have_posts()) : the_post();
							the_title();
						 endwhile;
						echo '<br><br>';
						};
						?>

Open in new window

0
 
LVL 30

Author Comment

by:LZ1
ID: 39216084
That's pretty close. I need it to spit out all categories whether they have a post or not though.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 500 total points
ID: 39216135
Specifying hide_empy = 0 takes care of categories with no posts, so you should be good.

Here's the whole thing again with your HTML structure (not tested, cause I'm at a diferent machine - no wp install to play with)

<?php 
	$args = array(
	  'orderby' => 'id',
	  'hide_empty' => 0,
	  'exclude' => '1',
	  'child_of' => 0
	  );


	$categories = get_categories( $args );
	
	foreach ( $categories as $mycategory ) {  
		//var_dump($mycategory);
		$catid = $mycategory->cat_ID;

		echo	'<li>';
		echo	'<a href="';
		echo	get_category_link($catid);
		echo	'">';
		echo	'<img src="';
		echo	z_taxonomy_image_url($catid);
		echo	'" />';
		echo	$mycategory->cat_name;
		
		query_posts('cat='. $catid.'&posts_per_page=1');

		 while (have_posts()) : the_post();
 				echo	'<div class="cat-hover">';
				echo	'<h1>Latest Post:</h1>';
				echo	'<h2>';
						the_title();
				echo 	'</h2>';
				echo	'<h4>';
						the_date('m-d-Y');
				echo 	'</h4>';
				echo	'/div>';
		 endwhile;

		 echo 	'</a></li>';

		};
?>

Open in new window

0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39216173
FYI - I edited above..
0
 
LVL 30

Author Comment

by:LZ1
ID: 39216229
That's perfect.

Except it's still showing the child categories for each top-level category.  
Same link as above.  I see the hide_empty is 0, so I'm not sure why it's showing the sub categories.

Thoughts?
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39216239
oh I see, that's different question.

in the args array, try instead of:

'child_of' => 0

this:

'parent' => 0
0
 
LVL 30

Author Closing Comment

by:LZ1
ID: 39216253
Thank you very much! Works like a charm!
0
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39216261
sweet :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to dynamically set the form action using jQuery.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now