ACF 复选框循环不显示所有帖子
ACF Checkbox loop not displaying all posts
我创建了一个显示循环的短代码,其中它应该显示 'products' 中的所有 post,这些复选框的值已被选中 'yes'。但是,它并没有显示所有 post,它只显示一个。
这是我的代码:
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');
我在显示多个 post 方面做错了什么?
这是我的自定义字段设置的样子:
现在我在输出方面遇到了问题。由于某种原因,循环显示在页面内容容器的顶部:
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');
这里的问题是,$output 每次迭代都会被覆盖。
意味着您只会获得最后一个称号。
在 = 符号前面放一个点,将收集它而不是覆盖它。
所以总结一下 -
开始
$output ="";
在循环外,然后在你的 while
内将它推到一起
$output .="whateverHtml".$variable."whateverHtml";
我创建了一个显示循环的短代码,其中它应该显示 'products' 中的所有 post,这些复选框的值已被选中 'yes'。但是,它并没有显示所有 post,它只显示一个。
这是我的代码:
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');
我在显示多个 post 方面做错了什么?
这是我的自定义字段设置的样子:
现在我在输出方面遇到了问题。由于某种原因,循环显示在页面内容容器的顶部:
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');
这里的问题是,$output 每次迭代都会被覆盖。
意味着您只会获得最后一个称号。
在 = 符号前面放一个点,将收集它而不是覆盖它。
所以总结一下 -
开始
$output ="";
在循环外,然后在你的 while
内将它推到一起$output .="whateverHtml".$variable."whateverHtml";