• Resolved joshrodgers

    (@joshrodgers)


    I have some code that display’s the titles of my child category posts on one of my pages:

    https://pastebin.com/nyhH9W68

    Is there a way to also post a random featured image from the child category posts? I’m pretty sure I can use this existing code, just not sure what to change to make it work.

    Thanks,
    Josh

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter joshrodgers

    (@joshrodgers)

    I almost got it working!!

    This is my new code: https://pastebin.com/yPtmxiqZ

    The concept works, the only thing is that my else doesn’t quite work…if I add a category that doesn’t exist It shows all posts, I expected that I’d get a Sorry, no posts matched your criteria error. Any ideas why that doesn’t work?

    Thread Starter joshrodgers

    (@joshrodgers)

    Ok,

    So now my code looks like: https://pastebin.com/tH2hSxMm

    if I remove the category I get the Category doesn’t exist error, but if the category does exist – just has no posts – I still get the category doesn’t exist error. I thought this would fix it for sure, but for some reason I’m still off a little bit somewhere. Getting closer!!

    Anyone have any ideas?

    Thanks,
    Josh

    Try cat_ID instead of term_id.

    query_posts( array (
        'showposts' => 1,
        'orderby'   => 'rand',
        'cat'       => $cat->cat_ID
    ) );
    Thread Starter joshrodgers

    (@joshrodgers)

    wpcanyon,

    I updated the code, but it still doesn’t work ??

    Thanks,
    Josh

    Put this after the get_categories.

    echo '<pre>';
    print_r( $cats );
    echo '</pre>';

    And copy paste the list that shows up (use pastebin, should be a big list).

    Ohh and “showposts” is deprecated for a few years now, not even sure if it still works. Change it to “posts_per_page”.

    Thread Starter joshrodgers

    (@joshrodgers)

    wpcanyon,

    Since I was testing, I only have one image posted, but you can see the returned array at: https://pastebin.com/cHCRzwJf.

    Also, it should be noted that I went back to: https://pastebin.com/yPtmxiqZ because for some reason, even with content, nothing was returned using the code: https://pastebin.com/tH2hSxMm.

    Having said all that, my code now looks like: https://pastebin.com/VLhdZT8C.

    * I also added the posts_per_page parameter to my array (instead of using showposts)

    Thanks,
    Josh

    Thread Starter joshrodgers

    (@joshrodgers)

    wpcanyon,

    Ok, I’m getting close…not sure why the else didn’t work, but essentially I created my own else by adding a check for the child category.

    My code looks like: https://pastebin.com/ZW7Sb3H9.

    The only thing that doesn’t work is…If the category doesn’t exist I should get a Sorry, no posts matched your criteria error.

    Any ideas?
    Josh

    <?php
    
    	$parent = get_cat_ID( 'photos' );
    	$cats = get_categories("child_of=".$parent);
    
    	foreach ($cats as $cat) {
    
    		query_posts( array (
    			'posts_per_page' => 1,
    			'orderby' => 'rand',
    			'cat' => $cat->cat_ID
    		) );
    
    		?>
    
    		<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    			<ul>
    				<?php
    					if (has_post_thumbnail()) {
    						printf( '<li><a href="'.get_category_link($cat->cat_ID).'">'.get_the_post_thumbnail(get_the_ID(), "album-thumb").'</a><a href="'.get_category_link($cat->term_id).'">%s</a></li>', apply_filters('get_term', $cat->name));
    					}
    				?>
    			</ul>
    
    		<?php endwhile; else: ?>
    
    			<p><?php _e("Sorry, no posts matched your criteria."); ?></p>
    
    		<?php endif;
    
    		wp_reset_query();
    
    	}
    
    ?>
    Thread Starter joshrodgers

    (@joshrodgers)

    wpcanyon,

    Unfortunately, that was one of the first variations of the code, and it did not work ??

    I continued to tinker with the code and found something that works ?? Yay!

    My final code looks like: https://pastebin.com/8ixt6z6v.

    Basically, in this version a couple of things changed:

    1. I changed the if to make sure that the parent category exists and that we have a post, if both of those conditions are met then it will display my content…if not, it does nothing.

    2. Since the else wasn’t working I wrote my own…that says…if the parent category or the child category doesn’t exist than display my error message…if so, than do nothing.

    Not sure why the else doesn’t work, but my guess is it’s because of the foreach that’s the only thing that’s different in this loop, that’s not in most other loops.

    Hope this helps someone else ??

    Thanks wpcanyon for all your help!!

    Josh

    You’re welcome, glad you got it working.

    Thread Starter joshrodgers

    (@joshrodgers)

    wpcanyon,

    Me too ??

    Thanks again!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Child Category Image’ is closed to new replies.