简体   繁体   English

如何通过“高级自定义字段”复选框过滤自定义帖子

[英]How filter custom posts by Advanced Custom Fields checkbox

I am creating a property site where there is a featured property on the home page. 我正在创建一个属性网站,主页上有一个特色属性。 To define a property as featured I have created an acf checkbox with the value as Yes when checked. 要将属性定义为特色,我创建了一个acf复选框,选中该复选框时其值为Yes。 I have tried filtering the posts by checking if the checkbox is checked but I cannot figure it out. 我已经尝试通过检查复选框是否选中来过滤帖子,但是我无法弄清楚。 Here's my code which isn't working; 这是我的代码不起作用;

<?php 
    $args = array(
        'post_type'         => 'property',
        'posts_per_page'    => 1,
        'meta_key'          => 'featured_property',
        'meta_value'        => 'Yes'
    );

    $query = new WP_Query( $args );
?>

<?php if( $query->have_posts() ) : ?>
    <?php  
        $main_field = get_field('images');
        $first_row = $main_field[0];
        $img = $first_row['image'];
        $img_crop = $img['sizes']['fresh_size'];
    ?>

    <img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
    <?php wp_reset_postdata(); ?>
<?php endif; ?>

在此处输入图片说明

READ THIS: for anyone attempting to do this with a checkbox like I was don't. 阅读此内容:对于尝试使用复选框(例如我不是)​​的用户。 after a little research i found out "Checkboxes are stored as serialized data and you're not going to be able to use WP_Query to filter by a checkbox field" Use true / false instead and check if the value equals '1' or '2' depending on what you are trying to achieve. 经过一番研究之后,我发现“复选框存储为序列化数据,您将无法使用WP_Query通过复选框字段进行过滤”,请使用true / false并检查值是否等于“ 1”或“ 2” '取决于您要实现的目标。

https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/ https://support.advancedcustomfields.com/forums/topic/using-checkbox-fields-in-custom-queries/

Remove this part: 删除此部分:

'meta_key'          => 'featured_property',
'meta_value'        => 'Yes'

Instead, filter out who has the checkbox checked inside the loop. 相反,请过滤出谁在循环内选中了复选框。 You are also missing parts of the loop. 您还缺少循环的一部分。 Try this code: 试试这个代码:

    <?php if( $query->have_posts() ) : ?>
        (...)

        <!-- start of the loop -->
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
              <?php if( get_field('featured_property') ) { // << FROM HERE ?>
                  <img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
              <?php } // << TO HERE ?>
        <?php endwhile; ?><!-- end of the loop -->

        <?php wp_reset_postdata(); ?>
    <?php endif; ?>

I have cut out the first part of your code to make it easier to read. 我已经剪掉了代码的第一部分,以使其更易于阅读。

-- -

Or, if you would like to use meta_key instead, try adding: 或者,如果您想使用meta_key,请尝试添加:

'compare' => 'EXISTS'
<?php 
$args = array(
    'post_type'         => 'property',
    'posts_per_page'    => 1,
    'meta_key'          => 'featured_property',
    'meta_value'        => 'Yes'
);

$query = new WP_Query( $args );
?>

<?php if( $query->have_posts() ): ?>
<ul>
<?php while( $query->have_posts() ) : $query->the_post();
   $images = get_field('images');
    $first_row = $main_field[0];
    $img = $first_row['image'];
    $img_crop = $img['sizes']['fresh_size'];
?>
    <img src="<?php echo $img_crop; ?>" alt="featuredproperty" class="img-fluid">
<?php endwhile; ?>
</ul>
<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). 
?>

Please check your settings under acf checkbox "Choices", check if it "Yes : Yes" or "yes : Yes" and fix your 'meta_value' if you have "yes : Yes" to 'meta_value' => 'yes'. 请在acf复选框“选择”下检查您的设置,检查它是“是:是”还是“是:是”,如果您将“是:是”设置为“ meta_value” =>“是”,请修正您的“ meta_value”。 Checkbox saves data as value-label. 复选框将数据另存为值标签。 I think you have issue with your configuration of checkbox. 我认为您对复选框的配置有疑问。

What type of 'images' field do you use? 您使用哪种类型的“图像”字段? Is it repeater or gallery? 是直放站还是画廊? If you using gallery then for src of the image you need to use: $images = get_field('images'); 如果使用Gallery,则需要使用图像的src:$ images = get_field('images'); $img_crop = $images[0]['sizes']['fresh_size']; $ img_crop = $ images [0] ['sizes'] ['fresh_size'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM