【发布时间】:2018-01-17 21:02:42
【问题描述】:
我创建了一个显示循环的短代码,其中应该显示“产品”中的所有帖子,这些帖子的复选框被选中为“是”。但是,它并没有显示所有帖子,它只显示一个。
这是我的代码:
function featured_products() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'products',
'meta_query' => array(
array(
'key' => 'home_featured',
'value' => '1',
'compare' => 'LIKE'
),
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$output = '<p>' . get_the_title() . '</p>';
}
echo '</ul>';
wp_reset_postdata();
} else {
$output = "<p>There aren't any products to display.</p>";
}
return $output;
}
add_shortcode('featured_products', 'featured_products');
在显示多个帖子方面我做错了什么?
这是我的自定义字段设置的样子:
现在我遇到了输出问题。出于某种原因,循环显示在页面内容容器的顶部:
function featured_products() {
$args = array(
'posts_per_page' => 10,
'post_type' => 'products',
'meta_query' => array(
array(
'key' => 'home_featured',
'value' => '1',
'compare' => 'LIKE'
),
),
);
$output .= '<div class="products">';
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$output .= '<div class="col-eq-height col-md-6" style="margin-bottom:30px;">';
$output .= '<div class="product">';
$output .= '<div id="thumbnail"><a href="' . get_the_permalink() . '">';
$output .= get_the_post_thumbnail();
$output .= '</a></div>';
$output .= '<div id="content">';
$output .= '<p id="title"><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></p>';
echo '<p>' . the_field('product_price_exc_vat') . '</p>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
}
wp_reset_postdata();
} else {
$output .= "<p>There aren't any products to display.</p>";
}
$output .= '</div>';
return $output;
}
add_shortcode('featured_products', 'featured_products');
【问题讨论】:
-
可能需要更改 'compare' => '=',是真/假字段吗?
-
还有 - 没有 ul 的开头 -
-
最后 - 你每次都在覆盖输出,所以它只需要最后一个帖子
-
收集输出 - 在它前面的一个点 - 像这样: $output .= "whatever"; - 并像这样在循环外启动 $output:$output = "";
-
很可能只是在其中放置一个点,因此它会收集所有数据,而不是覆盖它
标签: wordpress advanced-custom-fields