您正在使用get_posts
它返回一个帖子数组,您可以使用foreach
, 但不是the_loop
. 请尝试此版本的代码:
function ia_news_display_test($atts){ // [ia_news cat=\'category\' num=\'numbertodisplay\']
extract(shortcode_atts(array(
\'cat\' => \'any\',
\'num\' => \'2\',
), $atts));
$dirloop = new WP_Query( array(
\'post_type\' => \'ia_news\',
\'category_name\' => $cat,
\'posts_per_page\' => $num,
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\'
));
if ($dirloop->have_posts())
{
$content = "<ul class=\'ia_news_list\'>\\n";
while ( $dirloop->have_posts() ) : $dirloop->the_post();
$file_id = get_post_meta(get_the_id(), "upload_file", true);
$file_begin = get_post_meta(get_the_id(), "begin_date", true);
$file_end = get_post_meta(get_the_id(), "end_date", true);
if (\'\' != $file_end)
{
$file_end = " to ".$file_end;
}
$file_url = wp_get_attachment_url($file_id);
if (\'\' != $file_url)
{
//CHECK FOR EXISTENCE OF FILE URL
$content .= "<li class=\'ia_nl\'><a href=\'".$file_url."\'>".$cat." ".$file_begin.$file_end."</a></li>\\n";
}
endwhile;
$content .= "</ul>\\n";
}
else
{
$content = "nothing";
}
wp_reset_postdata();
return $content;
}
add_shortcode(\'ia_news_test\', \'ia_news_display_test\');
此外,如果您是自定义字段名
upload_file
, 然后尝试使用
get_post_meta
而不是像你的代码那样长时间的削减。