wordpress - 在自定义分类模板中的 WP_Query 期间查询(2)ACF 发布对象
问题描述
为自定义分类和相关的自定义帖子类型构建 Wordpress 页面模板。在新的 WP_Query 中,我需要从 (2) 不同的 ACF 帖子对象字段中获取字段;名单员工和名单代表。代码按预期工作,直到 wp_reset_postdata(); 返回正确数量的结果,每个帖子中的数据在重置之前都是唯一的。重置后,每个帖子中的所有数据都相同。代码如下,我确信有一个更优雅的解决方案:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php the_field('list_number'); ?>
<?php
$staff = get_field('list_staff');
$rep = get_field('list_rep');
if( $staff ):
// override $post
global $post;
$post = $staff;
setup_postdata( $post );
?>
<?php the_permalink(); ?><?php the_title(); ?>
<?php
endif;
if( $rep ):
// override $post
$post = $rep;
setup_postdata( $rep );
?>
<?php the_field('mkt_co'); ?><?php the_field('mkt_tel'); ?>
<?php
endif;
wp_reset_postdata();
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>
解决方案
想出了这个。setup_postdata() 对于这个应用程序来说是一个完全错误的方向。ACF 页面上记录了“显示多个帖子对象的数据”的正确内容。正如文章所述“使用此方法,$post 对象永远不会更改,因此所有函数都需要相关帖子 ID 的第二个参数。” 在此处阅读更多信息;https://www.advancedcustomfields.com/resources/post-object/。我的工作代码如下:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php
the_field('list_number');
$post_object = get_field('list_staff');
if( $post_object ):
?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<?php echo get_the_title($post_object->ID); ?>
<?php
endif;
?>
<?php
$post_object = get_field('list_rep');
if( $post_object ):
?>
<p><?php the_field('mkt_co', $post_object->ID); ?></span></p>
<a href="tel:<?php the_field('mkt_tel', $post_object->ID); ?>"><?php the_field('mkt_tel', $post_object->ID); ?></a>
<?php
endif;
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>
推荐阅读
- python - Telethon 脚本未向某些电话号码发送代码
- websocket - 自定义事件不会从 feathersjs 发送到 feathers-vuex
- python - 将一个类的属性调用到另一个类中
- sql-server-2008 - SQL-EX.RU (Exercise #137) -- 在第二个检查数据库上返回不正确的数据集
- python - Tensorflow 最多三个函数
- google-cloud-platform - 分区需要多少行才能在 BigQuery 中获得良好的性能?
- spring-boot - 反应堆按需通量或水槽
- reactjs - 单击时关闭反应组件
- angular - 显示跨字段自定义验证错误信息
- arrays - 将选定的单元格分配给变量