因此,我编写了下面的短代码,它输出一个自定义帖子列表。当我回显这些值时,效果很好。但当我用一种稍微不同的方式连接变量时,情况就不一样了。这并不重要,但我现在想知道为什么。
这很好用。
function post_events($atts) {
global $post;
$args = array(
\'post_type\' => \'event\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'ASC\',
);
$posts = get_posts($args);
$posts_chunks = array_chunk($posts, 2);
foreach ($posts_chunks as $row) {
echo \'<div class="row">\';
foreach ($row as $post) {
setup_postdata($post);
echo \'<div class="col-md-6 event-item">\';
echo \'<a href="\' .get_the_permalink(). \'">\' .get_the_post_thumbnail(). \'</a>\';
echo \'<div class="event-item-text">\';
echo \'<h3><a href="\'.get_the_permalink(). \'">\' .get_the_title(). \'</a></h3>\';
echo \'<span class="event-date">\' .get_the_date("Y-m-d"). \'</span>\';
echo \'<p>\' .wp_trim_words( get_the_content(), 40, \'...\' ). \'</p>\';
echo \'</div>\';
echo \'</div>\';
}
echo \'</div>\';
}
}
add_shortcode(\'post_events\',\'post_events\');
出于某种原因,这只输出第一个帖子。。。
function post_events($atts) {
global $post;
$args = array(
\'post_type\' => \'event\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'ASC\',
);
$posts = get_posts($args);
$posts_chunks = array_chunk($posts, 2);
foreach ($posts_chunks as $row) {
echo \'<div class="row">\';
foreach ($row as $post) {
setup_postdata($post);
$output = \'<div class="col-md-6 event-item">\';
$output .= \'<a href="\' .get_the_permalink(). \'">\' .get_the_post_thumbnail(). \'</a>\';
$output .= \'<div class="event-item-text">\';
$output .= \'<h3><a href="\'.get_the_permalink(). \'">\' .get_the_title(). \'</a></h3>\';
$output .= \'<span class="event-date">\' .get_the_date("Y-m-d"). \'</span>\';
$output .= \'<p>\' .wp_trim_words( get_the_content(), 40, \'...\' ). \'</p>\';
$output .= \'</div>\';
$output .= \'</div>\';
return $output;
}
echo \'</div>\';
}
}
add_shortcode(\'post_events\',\'post_events\');
最合适的回答,由SO网友:Jacob Peattie 整理而成
您正在重置$output
每次循环时:
foreach ($row as $post) {
// etc.
$output = \'<div class="col-md-6 event-item">\'; // Here $output
// etc.
}
那个
$output
分配正在重置的值
$output
到
\'<div class="col-md-6 event-item">\'
, 扔掉所有附加的东西。
你也回来了$output
在第一个循环结束时:
foreach ($row as $post) {
// etc.
return $output;
}
停止整个函数并返回
$output
, 这只是当时的第一篇帖子。
你需要申报$output
在循环(两者)之外,则仅从该点开始追加,并在循环完成后返回:
function post_events($atts) {
global $post;
$args = array(
\'post_type\' => \'event\',
\'post_status\' => \'publish\',
\'orderby\' => \'date\',
\'order\' => \'ASC\',
);
$posts = get_posts($args);
$posts_chunks = array_chunk($posts, 2);
$output = \'\';
foreach ($posts_chunks as $row) {
$output .= \'<div class="row">\';
foreach ($row as $post) {
setup_postdata($post);
$output .= \'<div class="col-md-6 event-item">\';
$output .= \'<a href="\' .get_the_permalink(). \'">\' .get_the_post_thumbnail(). \'</a>\';
$output .= \'<div class="event-item-text">\';
$output .= \'<h3><a href="\'.get_the_permalink(). \'">\' .get_the_title(). \'</a></h3>\';
$output .= \'<span class="event-date">\' .get_the_date("Y-m-d"). \'</span>\';
$output .= \'<p>\' .wp_trim_words( get_the_content(), 40, \'...\' ). \'</p>\';
$output .= \'</div>\';
$output .= \'</div>\';
}
$output .= \'</div>\';
}
return $output;
}
add_shortcode(\'post_events\',\'post_events\');