使用ACF的WordPress环路故障

时间:2015-02-16 作者:Spentacular

我正在浏览一些书籍,以获得带有高级自定义字段的自定义帖子类型。其中一个ACF是复选框,我只想显示选中此复选框的值。我使用的当前回路是:

<?php

$args = array (
    \'post_type\' => \'book\',
    \'posts_per_page\' => \'2\'
);

$thequery = new WP_Query( $args );

?>

<?php if (have_posts()) : while ( $thequery->have_posts() ) : $thequery->the_post(); ?>
  <?php if( get_field(\'book_featured\') ): ?>
    <div class="sidebar-book-header">
        <img src="<?php the_field( \'book_cover\' ); ?>" alt="">
        <a href="<?php the_field( \'buy_now_amazon\' ); ?>" class="sidebar-button book-button" style="background-color: <?php the_field( \'book_color_highlight\' ); ?>" target="_blank"><span>Buy<br>Now</span></a>
    </div>

    <p class="sidebar-book-info"><?php the_field( \'book_excerpt\' ); ?> <a href="<?php the_permalink(); ?>" style="color: <?php the_field( \'book_color_highlight\' ); ?>">Read More.</a></p>
  <?php endif; ?>
<?php endwhile; endif; ?>
这将显示第一个值,但不包括第二个值(我已经检查了两个)。我做错了什么导致循环只显示一个?

3 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

问题是你在循环阶段过滤,而不是在查询阶段。发生的情况如下:

  • WP_Query, 给我最新的两本书book_featured, 因此,只有在显示
的情况下,才能将if ( book_featured ) 来自循环的条件(&A);将其添加到查询中:

$args = array(
    \'post_type\' => \'book\',
    \'posts_per_page\' => \'2\',
    \'meta_key\' => \'book_featured\',
    \'meta_value\' => \'1\', // I\'m not 100% sure this is how ACF stores checkbox data, you\'ll need to check the `wp_postmeta` table
);

SO网友:Spentacular

我切换到一个单选按钮,而不是复选框,并使用了这里描述的meta wp\\u查询选项,它成功了!

http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/

SO网友:cybmeta

我发现您的代码中有一个错误,这可能就是问题所在。该行:

<?php if (have_posts()) : while ( $thequery->have_posts() ) : $thequery->the_post(); ?>
应为:

<?php if ($thequery->have_posts()) : while ( $thequery->have_posts() ) : $thequery->the_post(); ?>
此外,您应该使用wp_reset_postdata() 在自定义查询的循环之后。

结束

相关推荐

使用ACF的WordPress环路故障 - 小码农CODE - 行之有效找到问题解决它

使用ACF的WordPress环路故障

时间:2015-02-16 作者:Spentacular

我正在浏览一些书籍,以获得带有高级自定义字段的自定义帖子类型。其中一个ACF是复选框,我只想显示选中此复选框的值。我使用的当前回路是:

<?php

$args = array (
    \'post_type\' => \'book\',
    \'posts_per_page\' => \'2\'
);

$thequery = new WP_Query( $args );

?>

<?php if (have_posts()) : while ( $thequery->have_posts() ) : $thequery->the_post(); ?>
  <?php if( get_field(\'book_featured\') ): ?>
    <div class="sidebar-book-header">
        <img src="<?php the_field( \'book_cover\' ); ?>" alt="">
        <a href="<?php the_field( \'buy_now_amazon\' ); ?>" class="sidebar-button book-button" style="background-color: <?php the_field( \'book_color_highlight\' ); ?>" target="_blank"><span>Buy<br>Now</span></a>
    </div>

    <p class="sidebar-book-info"><?php the_field( \'book_excerpt\' ); ?> <a href="<?php the_permalink(); ?>" style="color: <?php the_field( \'book_color_highlight\' ); ?>">Read More.</a></p>
  <?php endif; ?>
<?php endwhile; endif; ?>
这将显示第一个值,但不包括第二个值(我已经检查了两个)。我做错了什么导致循环只显示一个?

3 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

问题是你在循环阶段过滤,而不是在查询阶段。发生的情况如下:

  • WP_Query, 给我最新的两本书book_featured, 因此,只有在显示
的情况下,才能将if ( book_featured ) 来自循环的条件(&A);将其添加到查询中:

$args = array(
    \'post_type\' => \'book\',
    \'posts_per_page\' => \'2\',
    \'meta_key\' => \'book_featured\',
    \'meta_value\' => \'1\', // I\'m not 100% sure this is how ACF stores checkbox data, you\'ll need to check the `wp_postmeta` table
);

SO网友:Spentacular

我切换到一个单选按钮,而不是复选框,并使用了这里描述的meta wp\\u查询选项,它成功了!

http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/

SO网友:cybmeta

我发现您的代码中有一个错误,这可能就是问题所在。该行:

<?php if (have_posts()) : while ( $thequery->have_posts() ) : $thequery->the_post(); ?>
应为:

<?php if ($thequery->have_posts()) : while ( $thequery->have_posts() ) : $thequery->the_post(); ?>
此外,您应该使用wp_reset_postdata() 在自定义查询的循环之后。

相关推荐