【问题标题】:Attempting something new in WP. Issue resetting WP loop在 WP 中尝试新的东西。问题重置 WP 循环
【发布时间】:2021-04-29 17:58:21
【问题描述】:

我在 WP 中工作,基本上我设置的是一些逻辑,可以为我的博客的每个类别创建一个单独的轮播。

对于每个轮播,即时显示每个类别的热门帖子。轮播中的每个项目都有一个帖子缩略图、作者姓名和作者头像。

我的问题(如果您向下滚动到主屏幕底部 pst“与社区见面”)帖子显示了正确的帖子名称和缩略图,但作者姓名和头像出现了一些奇怪的情况。架子上的第一个帖子看起来是正确的,但之后每个帖子都显示了错误的作者。我真的不知道如何处理这个问题。我已经捣碎他们的钥匙一个星期了。有什么想法吗?

<!-- 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">';

} ?>

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    用这个替换你的foreach:

    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;
    

    您在使用get_the_author_meta 时遇到了一些错误。它需要作者 ID,您尝试使用 display_name 获取它。您用于头像的那个是硬编码的。 (这只会返回属于 ID 32 的元数据。

    您的代码中有很多不一致之处。尝试了解正在发生的事情。查找您正在使用的功能以及它们的确切作用。 PHP 文档和 WordPress 文档都是极好的资源。

    【讨论】:

    • 谢谢巴特。这有帮助,但仍然无法像我在顶层架子上那样获得头像。生病继续玩它。
    猜你喜欢
    • 1970-01-01
    • 2013-01-10
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多