?
Solved

Wordpress: Get/display latest post for each category

Posted on 2013-06-01
12
Medium Priority
?
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
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…
Suggested Courses

800 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