尝试在页面模板中生成最近发布的帖子列表后,无法呈现自定义字段

时间:2015-09-10 作者:HelpNeeder

当我试图使用WP函数或WP query呈现一个最近发布类型的列表时,我无法呈现我必须在一点之后呈现的自定义字段。

类框的内容feat-block 每次使用以下命令之一时,结果都是空的:

$recent_news_posts = wp_get_recent_posts($news_post_settings); // grab post as array
$recent_news_posts = get_posts($news_post_settings); // grab post as an object
$recent_news_posts = wp_get_archives(\'postbypost\', \'5\');
$recent_news_posts = $wpdb->get_results($request);
为什么会这样?

这是我的代码:

<div id="home-right-bottom">
    <div class="news-block home-block">
        <h2>In the News</h2>
        <div class="content">
            <?php

                $news_post_settings = array(
                    \'posts_per_page\'   => 5,
                    \'offset\'           => 0,
                    \'category\'         => \'\',
                    \'category_name\'    => \'\',
                    \'orderby\'          => \'date\',
                    \'order\'            => \'DESC\',
                    \'include\'          => \'\',
                    \'exclude\'          => \'\',
                    \'meta_key\'         => \'\',
                    \'meta_value\'       => \'\',
                    \'post_type\'        => \'post\',
                    \'post_mime_type\'   => \'\',
                    \'post_parent\'      => \'\',
                    \'author\'           => \'\',
                    \'post_status\'      => \'publish\',
                    \'suppress_filters\' => true
                );

                //$recent_news_posts = wp_get_recent_posts($news_post_settings); // grab post as array
                //$recent_news_posts = get_posts($news_post_settings); // grab post as an object
                //$recent_news_posts = wp_get_archives(\'postbypost\', \'5\');

                global $wpdb;

                $post_limit = 5;
                $request = "SELECT ID, post_title, post_date, guid FROM $wpdb->posts WHERE post_status = \'publish\' AND post_type=\'post\' ORDER BY post_date DESC LIMIT $post_limit";
                $recent_news_posts = $wpdb->get_results($request);

                print \'<ul class="recent_news">\';

                foreach($recent_news_posts as $post) {
                    /*$title = $post->post_title;
                    $date = mysql2date(\'m.d.y\', $post->post_date);
                    $url = $post->guid;*/
                    $title = $post[\'post_name\'];
                    $date = mysql2date(\'m.d.y\', $post[\'post_date\']);
                    $url = $post[\'guid\'];

                    print \'<li><span class="date">\' . $date . \'</span> - <a href="\' . $url . \'">\' . $title . \'</a></li>\';
                }

                print \'</ul>\';

            ?>
        </div>
    </div>
运行上述命令后,不会渲染此块。如果我对上述内容进行评论,以下内容有效:

    <?php

        //if(get_field(\'featured_news\')):

            $feat = get_field(\'featured_news\');
    ?>

    <div class="feat-block home-block">
        <h2>Featured News</h2>
        <div class="content">
            <?php

                if(!empty($feat[0][\'news_link\'])) {
                    print \'<a href="\' . $feat[0][\'news_link\'] . \'">\';
                }

                if(!empty($feat[0][\'news_image\'])) {

                    $news_image_url = $feat[0][\'news_image\'][\'url\'];
                    $news_image = aq_resize($news_image_url, 300, 180, true);

            ?>
                <img src="<?php echo $news_image; ?>" alt="<?php echo $feat[0][\'news_image\'][\'caption\']; ?>" class="feat_img">
            <?php
                }
            ?>
            <p><?php
                if(!empty($feat[0][\'news_short_description\'])) {
                    print $feat[0][\'news_short_description\'];
                }
           ?></p>

           <?php

            if(!empty($feat[0][\'news_link\'])) {
                print \'</a>\';
            }

           ?>
        </div>
    </div>

    <?php

        //endif;

    ?>
</div>

1 个回复
最合适的回答,由SO网友:Milo 整理而成

覆盖全局$post 该函数从中获取post ID以获取自定义字段。您需要添加wp_reset_postdata() 运行其他使用$post.

相关推荐

如何将Java脚本添加到Custom-Page.php模板?

如何将javascript添加到自定义页面。php模板?如何使从w3schools ajax教程获得的以下javascript在自定义页面上工作。php模板?任何帮助都将不胜感激。工作javascript包含在以下HTML中:<!DOCTYPE html> <html> <style> table,th,td { border : 1px solid black; border-collapse: collapse;&#x