【问题标题】:PHP, How to fetch information from two different $post_id's within one loopPHP,如何在一个循环中从两个不同的 $post_id 获取信息
【发布时间】: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


【解决方案1】:

问题是因为您在循环中间覆盖了循环数据,这打破了循环。

你需要改变两件事 -

  1. 您正在用作者页面的帖子 ID 覆盖 $post_id 值。只需要使用另一个变量,这样您的主帖子 ID 就不会受到影响。
  2. 删除循环内的多个wp_reset_postdata(我不确定这是什么意思?)

请参阅下面的更新代码(请注意,这未经测试,但主要思想在那里):

<?php 
$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 endif; ?>
            </div>

            <div class="articleabout">
                <?php the_title(); ?>
                <br> 
                <?php 
                 // DON'T USE YOUR POST_ID VARIABLE FOR THE AUTHOR PAGE!!
                 // Save it into a new variable
                 $author_post_id = get_field( 'author_link', false, false ); 
                 if( $author_post_id ): 
                      echo get_the_title( $author_post_id ); 
                 endif; ?>
                <br>
                Text about article.
            </div>

        </a>

    <?php endwhile; ?>
    
<?php wp_reset_postdata(); endif; ?> 

【讨论】:

  • @Berglind 太好了,很高兴当我无法测试它时它工作了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 2019-07-17
  • 1970-01-01
  • 2019-06-15
  • 2013-12-28
相关资源
最近更新 更多