PHP,如何在一个循环内从两个不同的 $post_id 获取信息

我正在为首页构建一个主题,其中显示我的 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,但我不知道这是否确实是问题或我将如何解决这个问题。


有什么建议吗?


慕神8447489
浏览 84回答 1
1回答

九州编程

问题是因为您在循环中间覆盖了循环数据,这会破坏循环。你需要改变两件事 -您正在$post_id使用作者页面的帖子 ID 覆盖该值。只需使用另一个变量,这样您的主帖子 ID 就不会受到影响。删除循环内的倍数wp_reset_postdata(我不确定这意味着什么?)请参阅下面更新的代码(请注意,这尚未经过测试,但主要思想就在那里):<?php&nbsp;$the_query = new WP_Query( 'posts_per_page=12&offset=1' );if ( $the_query->have_posts() ) : ?>&nbsp; &nbsp; <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>&nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php the_permalink() ?>" class="article">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="articlepic">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php $image_obj = get_field('coverpic', $post_id );&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( $image_obj ) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="<?= $image_obj[ 'sizes' ]['small'] ?>">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="articleabout">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php the_title(); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// DON'T USE YOUR POST_ID VARIABLE FOR THE AUTHOR PAGE!!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Save it into a new variable&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$author_post_id = get_field( 'author_link', false, false );&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if( $author_post_id ):&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo get_the_title( $author_post_id );&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Text about article.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; <?php endwhile; ?>&nbsp; &nbsp;&nbsp;<?php wp_reset_postdata(); endif; ?>&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP