簡體   English   中英

如何通過 ACF 關系字段查詢自定義帖子類型 (CPT)?

[英]How do you query a custom post type (CPT) via an ACF relationship field?

概述

我有兩個CTPs

  1. Range :展示 4 種面包車類型
  2. Wheels展示了貨車可以擁有的所有車輪。

wheels帖子類型中,我有兩個ACF fields

  1. 圖像:這是image類型(車輪的圖像)。 該字段的nameimage
  2. 適用於:這是一個relationship字段,我們可以在其中選擇該wheel可用的貨車(從range后類型)。 這個字段的nameavailable_on_range

現在,我通過id查詢每個頁面的range ,並希望拉入在其關系字段中具有此range的所有輪子。

例如,允許wheel 7 在range /van 1 上可用,但在range /van 4 上不可用。

當前問題

我的relationship字段正在工作,因為我能夠獲取和輸出車輪名稱( post_title ),但是,我似乎無法獲得該車輪的image (沒有src )。

代碼

 <?php $args = array( 'post_type' => 'range', 'post_status' => 'publish', 'p' => $select_range, // id of post ); $query = new WP_Query( $args ); $title = ""; if ( $query->have_posts() ) : global $post; while ( $query->have_posts() ) : $query->the_post(); $title = get_the_title(); endwhile; wp_reset_postdata(); endif; $related_wheels = get_posts(array( 'post_type' => 'wheels', 'meta_query' => array( array( 'key' => 'available_on_range', // name of custom field 'value' => '"' . $select_range . '"', 'compare' => 'LIKE' ) ) )); ?> <div class="options-wheels"> <?php if( $related_wheels ): foreach( $related_wheels as $wheel ): $wheel_name = $wheel->post_title; $wheel_image = get_field("image"); ?> <div class="configurator__options-wheel"> <img src="<?php echo $wheel_image; ?>" alt="<?php echo $wheel_name; ?>" loading="lazy"/> </div> <?php endforeach; endif; ?> </div>

var_dump($wheel_image)返回NULL

我怎樣才能得到image

也試過

  • <img src="<?php echo the_field('image'); ?>" alt="<?php echo $wheel_name; ?>" loading="lazy"/>
  • <img src="<?php echo esc_url($wheel_image['url']); ?>" alt="<?php echo $wheel_name; ?>" loading="lazy"/>

var_dump($wheel)不顯示與image相關的任何內容:

 object(WP_Post)#2964 (24) { ["ID"]=> int(4363) ["post_author"]=> string(1) "2" ["post_date"]=> string(19) "2022-05-18 11:00:08" ["post_date_gmt"]=> string(19) "2022-05-18 11:00:08" ["post_content"]=> string(0) "" ["post_title"]=> string(7) "Wheel 1" ["post_excerpt"]=> string(0) "" ["post_status"]=> string(7) "publish" ["comment_status"]=> string(6) "closed" ["ping_status"]=> string(6) "closed" ["post_password"]=> string(0) "" ["post_name"]=> string(4) "4363" ["to_ping"]=> string(0) "" ["pinged"]=> string(0) "" ["post_modified"]=> string(19) "2022-05-18 14:02:47" ["post_modified_gmt"]=> string(19) "2022-05-18 14:02:47" ["post_content_filtered"]=> string(0) "" ["post_parent"]=> int(0) ["guid"]=> string(45) "http://rev.test/?post_type=wheels&p=4363" ["menu_order"]=> int(0) ["post_type"]=> string(6) "wheels" ["post_mime_type"]=> string(0) "" ["comment_count"]=> string(1) "0" ["filter"]=> string(3) "raw" }

這意味着我的$related_wheels查詢存在問題。 但是,我只想查詢與當前頁面的id匹配的wheels ,這就是查詢當前所做的。 如何讓它也顯示image數據?

請像這樣更改獲取圖像字段。 $wheel 的類型是對象,所以不能使用數組取值的方法。

$wheel_image = get_field("image", $wheel->ID);

然后您可以將圖像 url 添加到圖像標簽。

<div class="configurator__options-wheel">
  <img src="<?php echo $wheel_image['url']; ?>" alt="<?php echo $wheel_name; ?>" loading="lazy"/>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM