Solved

Wordpress: Get/display latest post for each category

Posted on 2013-06-01
12
459 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

696 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