使用WP_Query()和Foreach()输出ACF生成值的正确方法 [英] Correct Way to Output ACF Generated Values Using WP_Query() and Foreach()

查看:107
本文介绍了使用WP_Query()和Foreach()输出ACF生成值的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用高级自定义字段(ACF)的设置字段和子字段.结构类似于:

I have setup fields and sub-fields using Advanced Custom Fields (ACF). The structure is similar to:

Field: Pet Details
- Sub-Field: Pet Name (text)
- Sub-Field: Pet Birthday (date picker)
- Sub-Field: Pet Gender (taxonomy, select)
- Additional... 

我已经可以使用 get_field() get_sub_field()函数在自定义帖子类型(/cpt_slug/post_title)中使用这些标签,但是我没有能够将默认的ACF函数( get_field() get_sub_field()等)与显示此信息的其他页面结合使用-在这种情况下, domain.com/account (woocommerce我的帐户页面),这很好,因为我很高兴使用 WP_Query .

I have been able to use these within my custom post type (/cpt_slug/post_title) using the get_field(), get_sub_field() functions, however I have not been able to usilise the default ACF functions ( get_field(), get_sub_field(), etc ) in conjunction with additional pages where this information is being displayed - in this case, domain.com/account (woocommerce my account page) which is fine as I'm happy to use WP_Query.

虽然我下面有一个有效的代码,但它要求我为每个acf sub_field设置 $ variable = get_post_meta($ pet-> ID,field_sub_field,true).给定数组结构,

While I have a working code below, it requires me to setup $variable = get_post_meta( $pet->ID, field_sub_field, true ) for each acf sub_field. Given the array structure,

我能想到的最好结果是 echo $ details ['pet_details_pet_name'] [0] ,它输出'Cooper'(正确),但是由于我对数组的了解有限,"0"将变成"1"并且不允许其工作的机会?我不相信cpt/acf会在数组中添加第二个值,但是我想听听您的想法.

The best result I could come up with was echo $details['pet_details_pet_name'][0] which outputs 'Cooper' (correct), but with my limited knowledge of arrays, is there any chance that the '0' will ever become a '1' and not allow this to work? I don't believe that the cpt/acf will ever put a second value in the array but I would like to hear your thoughts.

代码

global $current_user;

$query = new WP_Query ( array ( 
    'post_type' => 'audp_pets', 
    'author' => $current_user->ID, 
    'order' => 'ASC' 
    ) );

$pets = $query->posts;

if ( $pets ) {

    foreach ( $pets as $pet ) {

        var_dump ( $pet );

        ?><hr><?php

        echo "\$pet->post_title = " . $pet->post_title . '<br />';

        ?><hr><?php

        $details = get_post_meta( $pet->ID );

        $pet_name = get_post_meta( $pet->ID, 'pet_details_pet_name', true );
        echo "\$pet_name = " . $pet_name . '<br />';

        ?><hr><?php

        var_dump ( $details );


    }

wp_reset_postdata();

}

输出(源)

object(WP_Post)#13185 (24) {
  ["ID"]=>
  int(952)
  ["post_author"]=>
  string(1) "1"
  ["post_date"]=>
  string(19) "2020-01-16 16:24:17"
  ["post_date_gmt"]=>
  string(19) "2020-01-16 05:24:17"
  ["post_content"]=>
  string(0) ""
  ["post_title"]=>
  string(6) "AA0AA0"
  ["post_excerpt"]=>
  string(0) ""
  ... (additional fields)
}

(N.B -- $pet->post_title = AA0AA0)

array(56) {
  ["_edit_last"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["_edit_lock"]=>
  array(1) {
    [0]=>
    string(12) "1579152259:1"
  }
  ["pet_details_pet_name"]=>
  array(1) {
    [0]=>
    string(6) "Cooper"
  }
  ["_pet_details_pet_name"]=>
  array(1) {
    [0]=>
    string(19) "field_5e1189ab705b2"
  }
  ["pet_details_pet_birthday"]=>
  array(1) {
    [0]=>
    string(8) "20130313"
  }
  ... (additional fields)
}

(N.B. $pet_name = Cooper)

先谢谢您.

推荐答案

基本上,您可以这样做:

Basically, You can do it like this :

global $current_user;

$args = array ( 
'post_type' => 'audar_pets', 
'author' => $current_user->ID, 
'order' => 'ASC' 
);

$query = new WP_Query( $args );

//Here we set the loop up so we can use it all in the normal way.. 
if($query->have_posts()){
    while ($query->have_posts()) {
        $query->the_post();
        echo get_the_title();
        //Get you fields
        var_dump(get_field('pet_details'));

        if( have_rows('pet_details') ):

            // loop through the rows of data
            while ( have_rows('pet_details') ) : the_row();

                // display a sub field value
                the_sub_field('pet_name');

            endwhile;

        else :

        // no rows found

    endif;
    } 
wp_reset_postdata();
}

但这是在猜测您使用的是中继器,它的确是一个子字段,而不仅仅是另一个字段.

But this is guessing that you are using a repeater and it is indeed a sub field, and not just another field..

您也可以通过在末尾添加帖子ID来使用 have_rows 等,就像使用任何ACF函数一样.

You can also use the have_rows etc by adding the post id at the end, like you can with any ACF function.

您可以通过添加ID $ value = get_field("text_field",123);

You can get any field, from any page, post, attachment etc you want, by adding the id $value = get_field( "text_field", 123 );

这篇关于使用WP_Query()和Foreach()输出ACF生成值的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆