首页 文章

摘录未显示在“最新帖子”区域 . 为什么?

提问于
浏览
0

我正在制作一个wordpress主题,在主页上的主要内容下面有一个“最新帖子”的区域 . 自定义主题基于Adamos主题 . 到目前为止,我在打印主要内容后使用它:

<?php

    wp_reset_query();

    $args = array( 'numberposts' => '2' );

    $recent_posts = wp_get_recent_posts($args);

    foreach( $recent_posts as $recent ){

        setup_postdata($recent);

        ?>

        <div class="index_recent_post">

            <div class="index_recent_title">

                <h3><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a>'; ?></h3>

            </div>

            <?php

                if ( has_post_thumbnail($recent["ID"]) ) {

                    echo get_the_post_thumbnail($recent["ID"], 'frontpage-thumbnail');

                }

                echo '<p>' . get_the_excerpt($recent["ID"]) . '</p>';


                echo '<a href="' . get_permalink($recent["ID"]) . '" class="recent_link">MORE</a>';

            ?>

        </div>

        <?php

    }

    wp_reset_query();

    ?>

但是,虽然最新帖子( Headers ,缩略图,链接)的每个项目都完美无缺,但摘录却没有:它显示为空 . 如果我删除 wp_reset_query()setup_postdata() 行,它会显示MAIN帖子的摘录,但似乎没有任何方法可以让它显示最新的摘录,即使其他信息是最新的帖子完美展现 .

摘录也没有找到自定义摘录而没有找到它的功能 . 我可以通过 $recent["post_excerpt"] 获得一个自定义的摘录,但是如果它根据内容构建一个,如果自定义的不存在,那么它只会得到它,这是不太理想的 .

有没有人处理过这个问题,你能帮我找到问题所在吗?

1 回答

  • 2

    你可以使用下面的代码 . 我在我的本地服务器上测试了这段代码 . 它运行正常 .

    wp_reset_query();
    
    $args = array( 'numberposts' => '2' );
    
    $recent_posts = wp_get_recent_posts($args);
    
    foreach( $recent_posts as $recent ){
    
        setup_postdata($recent);
    
        ?>
    
        <div class="index_recent_post">
    
            <div class="index_recent_title">
    
                <h3><?php echo '<a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a>'; ?></h3>
    
            </div>
    
            <?php
    
                if ( has_post_thumbnail($recent["ID"]) ) {
    
                    echo get_the_post_thumbnail($recent["ID"], 'frontpage-thumbnail');
    
                }
    
                //echo '<p>' . get_the_excerpt($recent["ID"]) . '</p>';
                $content= $recent["post_content"];
    
                $excerpt = wp_trim_words( $content, $num_words = 55, $more = null ); 
                echo $excerpt;
    
    
                echo '<a href="' . get_permalink($recent["ID"]) . '" class="recent_link">MORE</a>';
    
            ?>
    
        </div>
    
        <?php
    
    }
    
    wp_reset_query();
    

相关问题