• I have featured images for each post listed, but want the image location to vary from post to post. I know that a class can be set to the featured image using
    <?php echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'top')); ?>

    However I need help delegating which post receives the class out of all visible posts.

    Example Image

Viewing 3 replies - 1 through 3 (of 3 total)
  • what is the criteria you want to use to determine which class is assigned to the specific post?

    You can create a conditional dependent upon this criteria:

    if( $criteria == 'something' ) {
    $class = 'top';
    } elseif ( $criterai == 'something-else' ) {
    $class = 'bottom';
    }
    echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => $class ));

    Are you setting the image based on something post-specific or based on the number of the post in the list? Once you determine your criteria you should be able to put something similar to above together.

    Thread Starter tdesens

    (@tdesens)

    Joshua:

    Thanks for the reply. I think this is already helpful.

    Since only 4 posts will show per page, class is only based on its position in the list of posts.

    Is that doable? If not, can I set it so that if the post category is “featured & top” than it shows in the list and is classified as “top”?

    Are you working generating a list of 4 posts per page and wont the first one at the top, while the second, third and forth have different classes as well? If you are just going on the order of the posts in the list, that is ease. You’ll have something like this for your loop:

    <?php
    if ( have_posts() ) {
    	$count = 1;
    	while ( have_posts() ) {
    		the_post();
    	if( $count == 1 ) {
    		$class = 'top';
    	} elseif ( $count == 2 ) {
    		$class = 'other class';
    	} elseif ( $count == 3 ) {
    		$class = 'other class';
    	} elseif ( $count == 4 ) {
    		$class = 'other class';
    	}
    	echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => $class ));
    	} // end while
    $count++;
    } // end if
    ?>

    Alternatively, if you want a specific post in that group of four posts based on a post-specific criteria you may want to go the route of using categories to filter and add classes based on the category.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Featured Image Location specific to individual posts’ is closed to new replies.