需要显示3个帖子。我创建了以下代码:
add_shortcode(\'projects\', \'projects_shortcode\');
function projects_shortcode($atts, $content){
extract(shortcode_atts(array(
\'post_type\' => \'project\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 3,
\'caller_get_posts\' => 1
), $atts));
$args = array(
\'post_type\' => $post_type,
\'post_status\' => $post_status,
\'posts_per_page\' => $posts_per_page
);
global $post;
$posts = new WP_Query($args);
$out = \'\';
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();
$overview_image = get_field(\'small_overview_image\');
$out = \'<div class="row"><div class="col-md-4">
<a href="\'.get_permalink().\'" title="\' . get_the_title() . \'" class="project-item">
<div class="project-item_img"><img class="img-fluid" src="\'.$overview_image[\'url\'].\'" /></div>
<div class="project-item_title">
<div class="project-item_title-name">\'.get_the_title().\'</div>
<div class="project-item_title-description">\'.get_the_title().\'</div></div>\';
$out .=\'</a></div></div>\';
/* these arguments will be available from inside $content
get_permalink()
get_the_content()
get_the_category_list(\', \')
get_the_title()
and custom fields
get_post_meta($post->ID, \'field_name\', true);
*/
endwhile;
else
return; // no posts found
wp_reset_query();
return html_entity_decode($out);
}
在wp编辑器中使用短代码
[projects]
.
但此代码仅显示1个帖子。\'posts_per_page\' => 3
不工作。我不是php开发人员。此代码中哪里可能有错误?
最合适的回答,由SO网友:Jacob Peattie 整理而成
在这一行:
$out = \'<div class="row"><div class="col-md-4">
您正在重置
$out
每个职位的变量。将其更改为:
$out .= \'<div class="row"><div class="col-md-4">
这将把每个新帖子添加到前一篇帖子的输出中,以便在返回输出时包含所有帖子的输出。