当我试图使用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>