WordPress.org

Ready to get started?Download WordPress

Forums

get_posts returns all posts (1 post)

  1. candybrie
    Member
    Posted 1 year ago #

    I'm trying to display an image as a link for a category, and I'd prefer that image to be one that is attached to a post in that category. Currently it just displays the same image for all of them.

    This is what I have right now

    <?php $args = array( 'post_type' => 'post' );
    $categories=get_categories($args);
    $thumbURL;
    $posts;
    $id;?>
    
    <?php foreach($categories as $category) : ?>
    
    	<?php
    	$args = array(
    		'suppress_filters' => false,
    		'post_type' => 'attachment',
    		'numberposts' => -1,
    		'category' =>$category->term_ID,
    		'order'    => 'ASC',
    		);
    	$posts=get_posts( $args );
    
    	if ($posts){
    		foreach($posts as $post){
    			if ( wp_attachment_is_image() ){
    				if( $thumbURL=wp_get_attachment_thumb_url() ){
    					break;
    				}
    			}
    		}
    	}
    	?>
    		<a class="categoryNav" href="<?php echo get_category_link($category->term_id); ?>" title="<?php echo $category->name; ?>">
    		<img class="categoryNav" src="<?php echo $thumbURL; ?>" alt="<?php echo $category->name; ?> : <?php echo $category->description; ?>" />
    	</a>
    
    <?php endforeach ?>

    I'm pretty new to PHP and wordpress, so any feedback would probably be helpful to me.

Topic Closed

This topic has been closed to new replies.

About this Topic