【发布时间】:2020-07-25 16:48:05
【问题描述】:
我正在为显示我的 wordpress 网站最新帖子的首页构建一个主题。
我想在每个帖子中显示一张图片(帖子本身的高级自定义字段)和帖子的作者(通过高级自定义字段链接到帖子的页面标题)。
我的代码是:
<?php
// The Query
$the_query = new WP_Query( 'posts_per_page=12&offset=1' );
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php the_permalink() ?>" class="article">
<div class="articlepic">
<?php $image_obj = get_field('coverpic', $post_id ); if ( $image_obj ) : ?>
<img src="<?= $image_obj[ 'sizes' ]['small'] ?>">
<?php wp_reset_postdata(); endif; ?>
</div>
<div class="articleabout">
<?php the_title(); ?>
<br>
<?php $post_id = get_field( 'author_link', false, false ); if( $post_id ): echo get_the_title( $post_id ); wp_reset_postdata(); endif; ?>
<br>
Text about article.
</div>
</a>
<?php endwhile; ?>
<?php wp_reset_postdata(); endif; ?>
两个 div 独立工作正常,但是当我添加第二个 div (class="articleabout") 时,第一个 div 显示为空。
我怀疑这可能是因为我在第二个 div 中添加了另一个 $post_id,这混淆了第一个 div,但我不知道这是否真的是问题或我将如何解决它。
有什么建议吗?
谢谢!
【问题讨论】:
-
所以基本上我正在获取文章 post_id 以显示图像,然后获取另一个链接到该 post_id 的帖子 _id 以显示作者的姓名...
-
当你删除额外的 wp_reset_postdata() 调用并且只保留最后结束之前的最后一个调用时会发生什么?
-
我之前尝试过,但没有任何改变..
-
@Berglind 你可以从帖子中获取作者ID吗?
-
我在下面添加了一个答案 - 让我知道它是否有效,因为我无法测试,因为我没有像你一样的设置(ACF 字段等):)跨度>
标签: php wordpress advanced-custom-fields