Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Wordpress: Get/display latest post for each category

Posted on 2013-06-01
12
Medium Priority
?
475 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
Industry Leaders: 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!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

610 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