Solved

Wordpress: Get/display latest post for each category

Posted on 2013-06-01
12
454 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

27 Experts available now in Live!

Get 1:1 Help Now