• Hi there, I hope someone can help me…
    I would like to show category list in a was that each category item displays first image from the first post in that category.
    I hope I explained this well …

    I’m using wp_list_categories and trying to “hack” it, tried to find a solution here and all over the internet, but no luck …

    Please help,
    thanks

Viewing 12 replies - 1 through 12 (of 12 total)
  • That’s simply not possible with wp_list_categories. You’d need to use something like get_categories and then run a custom query to grab the image from the first post in that category.

    Thread Starter Micemade

    (@anydog)

    Oh, thanks for reply …
    I will check what can be done with get_categories and if I find a solution I will share it here .

    If you, or anyone else is willing to find a solution, feel free …. ??

    This is cobbled together from bits in the Codex and a similar bit of my code. And untested but try:

    <ul class="category-list">
    <?php
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC'
      );
    $categories=get_categories($args);
    foreach($categories as $category) {
    	$featured_image = array();
    	$args2 = array(
    		'post_type' => 'attachment',
    		'post_mime_type' => 'image',
    		'numberposts' => 1,
    		'post_status' => null,
    		'orderby' => ID,
    		'post_parent' => $post->ID
    		);
    	$attachments = get_posts($args2);
    	if ($attachments) {
    		foreach ($attachments as $attachment) {
    			$featured_image = wp_get_attachment_image_src($attachment->ID, $size='thumbnail', $icon = false);
    		}
    	}
    	?>
    	<li><a href="<?php echo get_category_link( $category->term_id );?>" title="View all posts in <?php echo $category->name;?> ">
    	<?php if( count( $featured_image ) != 0 ) echo '<img src="' . $featured_image[0] . '" width="' . $featured_image[1] . '" height="' . $featured_image[2] . '" alt="'" /> ';?>
    	<?php echo $category->name;?></a></li>
    }
    </ul>

    Thread Starter Micemade

    (@anydog)

    Hmm,there’s error:
    Parse error: syntax error, unexpected ‘”‘, expecting ‘,’ or ‘;’

    line:
    <?php if( count( $featured_image ) != 0 ) echo '<img src="' . $featured_image[0] . '" width="' . $featured_image[1] . '" height="' . $featured_image[2] . '" alt="'" /> ';?>

    I changed it to:
    <?php if( count( $featured_image ) != 0 ) echo '<img src="' . $featured_image[0] . '" width="' . $featured_image[1] . '" height="' . $featured_image[2] . '" alt="" />' ;?>
    ,but it produced this error:
    Parse error: syntax error, unexpected $end on line 155.
    Line is the last line, after<?php get_sidebar(); ?><?php get_footer(); ?>

    Any idea?

    Oh – how annoying!
    <?php if( count( $featured_image ) != 0 ) echo '<img src="' . $featured_image[0] . '" width="' . $featured_image[1] . '" height="' . $featured_image[2] . '" alt="'" /> ';?> has a spurious single quote in the alt attribute (alt="'")

    It should be:
    <?php if( count( $featured_image ) != 0 ) echo '<img src="' . $featured_image[0] . '" width="' . $featured_image[1] . '" height="' . $featured_image[2] . '" alt="" /> ';?>

    Thread Starter Micemade

    (@anydog)

    Yeah, I changed that too (see end of my last post) …
    I got this error:
    Parse error: syntax error, unexpected $end on line 155.
    Line 155 is the last line, after<?php get_sidebar(); ?><?php get_footer(); ?>

    I can’t see any more errors in that piece of code.

    Thread Starter Micemade

    (@anydog)

    OK, nevermind …
    It must be that I made some ugly code that messes up with yours …

    Thanx for you help anyway … It’s nice to see there are people who are willing to help others !

    Best luck !

    Thread Starter Micemade

    (@anydog)

    <ul class="category-list">
    <?php
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC'
      );
    $categories=get_categories($args);
    foreach($categories as $category) {
    	$featured_image = array();
    	$args2 = array(
    		'post_type' => 'attachment',
    		'post_mime_type' => 'image',
    		'numberposts' => 1,
    		'post_status' => null,
    		'orderby' => ID,
    		'post_parent' => $post->ID
    		);
    	$attachments = get_posts($args2);
    	if ($attachments) {
    		foreach ($attachments as $attachment) {
    			$featured_image = wp_get_attachment_image_src($attachment->ID, $size='thumbnail', $icon = false);
    		}
    	}
    	?>
    	<li><a href="<?php echo get_category_link( $category->term_id );?>" title="View all posts in <?php echo $category->name;?> ">
    	<?php if( count( $featured_image ) != 0 ) echo '<img src="' . $featured_image[0] . '" width="' . $featured_image[1] . '" height="' . $featured_image[2] . '" alt="" /> ';?>
    	<?php echo $category->name;?></a></li>
    <?php };?>
    </ul>

    This is the de-bugged code … Still, it doesn’t show images just a list of categories …

    Hello, did you ever get this to work, anydog? I’m looking for this functionality.

    In the foreach satement looping through $attachments, $featured_image is no longer an array, its being changed to a string. It should be

    if ($attachments) {
    		foreach ($attachments as $attachment) {
    			$featured_image[] = wp_get_attachment_image_src($attachment->ID, $size='thumbnail', $icon = false);
    		}
    	}

    OK, so I tried this: It lists only the categories, no images.

    <ul class="category-list">
    <?php
    $args=array(
      'orderby' => 'name',
      'order' => 'ASC'
      );
    $categories=get_categories($args);
    foreach($categories as $category) {
    	$featured_image = array();
    	$args2 = array(
    		'post_type' => 'attachment',
    		'post_mime_type' => 'image',
    		'numberposts' => 1,
    		'post_status' => null,
    		'orderby' => ID,
    		'post_parent' => $post->ID
    		);
    	$attachments = get_posts($args2);
    if ($attachments) {
    		foreach ($attachments as $attachment) {
    			$featured_image[] = wp_get_attachment_image_src($attachment->ID, $size='thumbnail', $icon = false);
    		}
    	}
    	?>
    	<li><a href="<?php echo get_category_link( $category->term_id );?>" title="View all posts in <?php echo $category->name;?> ">
    	<?php if( count( $featured_image ) != 0 ) echo '<img src="' . $featured_image[0] . '" width="' . $featured_image[1] . '" height="' . $featured_image[2] . '" alt="" /> ';?>
    	<?php echo $category->name;?></a></li>
    <?php };?>
    </ul>

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Category list – items with first post pic’ is closed to new replies.