Skip to content
Advertisement

Attempting something new in WP. Issue resetting WP loop

Im working in WP and basically what I’ve got set up is some logic to create a separate carousel for each category of my blog.

For each carousel im displaying the top viewed posts for each category. Each Item in the carousel has a post thumbnail, author name and author avatar.

My problem(if you scroll down to the bottom of the home screen pst”meet the community”) the posts are showing the right post names and thumbnails but something funky is going on with the author names and avatars. The first post in the shelf looks correct but after that each post is showing the wrong author.I really dont know how to handle this. Ive been mashing they keys for a week already. Any thoughts?

<!-- A shelf for each category by most posts and posts by highest view count--> 

<?php
$cat_args = array(
'number' => 5,
'orderby' => 'count',
'post_type' => 'post',
'order' => 'DESC',
'child_of' => 0
 );

$categories =   get_categories($cat_args); 

foreach($categories as $category) { 

echo '<div class="shelf_holder">';
echo '<div class="shelf_title"> <h1><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></h1><h5 class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" )) . '" ' . '>  View All</a></h5></div>';

echo '<div class="shelf_prev"><div class="backwards_icon"></div></div>';
echo '<div class="shelf_next"><div class="forward_icon"></div></div>';
echo '<div class="display-posts-listing grid">';
    
$post_args = array(
'numberposts' => 8,
'category' => $category->term_id,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);

$posts = get_posts($post_args);

foreach($posts as $post) {
    ?>
<div class="listing-item">

    <?php
$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium');
list($url, $width, $height, $is_intermediate) = $thumbnail;
?>

    <div class="media-wrapper">
            <?php $post_views = get_post_meta( get_the_ID(), 'post_views_count', true );?>
            <div class="counter"><div class="views_icon"></div><?php echo $post_views; ?></div>
        <div class="save_card"> <?php the_favorites_button($post_id);?></div>
        <div class="media-gradient" style="height:196px;"></div>
        <a href="<?php the_permalink(); ?>"><div class="media" style="height:196px;width:<?php echo $width; ?>px;background-image:url(<?php echo $url; ?>);background-size:cover;overflow: hidden;background-position: center;width: 100%;left:0px;"></div></a>
    </div>
    <div class="tiny_head"><?php echo get_avatar( get_the_author_meta( 'ID' ), 32 );?> </div>
    <div class="card_content">
    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 
    <div class="entry-meta">

        <?php
global $post;
$author_id=$post->display_name;
?>

        <?php
the_author_meta( 'display_name', $author_id );
?>              

        <?php wp_reset_query(); ?>  

    </div>  
    </div>  

</div> <!-- listing item -->

<?php 
} 

echo '</div class="display-posts-listing grid">';
echo '</div class="shelf_holder">';

} ?>

Advertisement

Answer

Replace your foreach with this:

foreach($posts as $post) : ?>

<div class="listing-item">
<?php 

$thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'medium'); 
list($url, $width, $height, $is_intermediate) = $thumbnail;
$author_id=$post->post_author;

?>

    <div class="media-wrapper">
            <?php $post_views = get_post_meta( $post->ID, 'post_views_count', true );?>
            <div class="counter"><div class="views_icon"></div><?= $post_views; ?></div>
        <div class="save_card"> <?php the_favorites_button($post_id);?></div>
        <div class="media-gradient" style="height:196px;"></div>
        <a href="<?= get_the_permalink($post); ?>"><div class="media" style="height:196px;width:<?= $width; ?>px;background-image:url(<?= $url; ?>);background-size:cover;overflow: hidden;background-position: center;width: 100%;left:0px;"></div></a>
    </div>
    <div class="tiny_head"><?= get_avatar( get_the_author_meta( 'ID' ), $author_id );?> </div>
    <div class="card_content">
    <h3><a href="<?= get_the_permalink($post); ?>"><?= get_the_title($post); ?></a></h3> 
    <div class="entry-meta">
<?= get_the_author_meta( 'display_name', $author_id ); ?>
    </div>  
    </div>  

</div> <!-- listing item -->

<?php 
endforeach;

You had some errors in the way you were using get_the_author_meta. It needs the author ID and you tried getting it using display_name. The one you used for the avatar is hardcoded. (That’s only going to return the meta that belongs to ID 32).

There are a lot of inconsistencies in your code. Try to understand what is happening. Look up the functions you’re using and what they do exactly. The PHP documentation and WordPress documentation both are excellent resources.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement