【问题标题】:ACF Checkbox loop not displaying all postsACF 复选框循环不显示所有帖子
【发布时间】: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


【解决方案1】:

这里的问题是,$output 每次迭代都会被覆盖。

意味着你只会得到最后一个标题。

在 = 符号前面加一个点,将收集它而不是覆盖它。

总结一下——

开始

$output ="";

在循环之外,然后在你的 while 中将它推到一起

$output .="whateverHtml".$variable."whateverHtml";

【讨论】:

  • @JakePunton 使用 get_the_title() 而不是 the_title() - 原因:the_title() 在读取后立即回显 - 不用于短代码等 - 我认为这就是原因跨度>
  • 这行得通,但即使使用 get_field(); ACF 功能不工作。所以 WP 函数可以,但 ACF 不会从自定义字段中加载值。
  • 你应该仍然使用 get_field() - 如果由于某种原因找不到它 - 尝试将帖子 ID 作为第二个参数,如下所示: get_field('product_price_exc_vat', get_the_ID ());它仍然应该是 $output 的一部分
  • $output .= '

    ' 。 get_field('product_price_exc_vat', get_the_ID()) 。 '

    ';不是工作伙伴。
  • 不明白为什么这不起作用,我自己以前做过类似的事情吗?我能想到的唯一原因是它没有设置吗?还是ID错了?我想你可以打印 get_post_meta(get_the_ID());查看帖子的所有元值。
猜你喜欢
  • 1970-01-01
  • 2018-01-10
  • 1970-01-01
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2018-09-02
  • 2021-03-26
相关资源
最近更新 更多