【发布时间】:2023-03-20 11:16:02
【问题描述】:
所以,我有几个使用 ACF 的自定义字段。在他们身上,我有一个名为 cd_location 的字段(组 -> 中继器 -> 选择)。
我制作了一个页面模板(基于我的自定义工作存档),它应该返回包含位置 Y (GET / xxx.com/?loc=YYY) 的所有课程,但它根本不起作用,我不这样做不知道为什么。有人可以建议吗?
我最常用的 php 代码:
global $post;
global $_GET;
$heading = get_field( 'heading', $post->ID );
$course_dates = get_field('course_dates', $post->ID);
然后我有循环:
<?php
if ( have_posts() ) :
$counter = 0;
while ( have_posts() ) : the_post(); ?>
<div class="one_half<?php echo ( ++$counter == 2 ) ? ' last_column' : ''; ?>">
<div class="break-link">
<?php
get_portfolio_item_thumbnail( $post->ID, '5', '528', '328', true );
?>
</div>
<h4 class="cont-subtitles"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<p class="excerpt"><?php echo excerpt(15); ?></p>
</div><!-- end one_half -->
<?php if ( $counter == 2 ) : ?>
<div class='clear'> </div>
<?php $counter = 0;
endif;
endwhile;
endif; ?>
这在functions.php上:
add_action( 'pre_get_posts', function( $query ) {
if ( isset( $query->query_vars[ 'post_type' ] ) && $query->query_vars[ 'post_type' ] == 'e-kursus' && isset( $_GET[ 'loc' ] ) ) {
$query->set( 'meta_key', 'cd_location' );
$query->set( 'meta_value', sanitize_text_field( $_GET[ 'loc' ] ) );
}
return $query;
} );
它返回一个带有页面名称的虚拟帖子,没有来自 ACF 的字段
【问题讨论】:
标签: php mysql wordpress advanced-custom-fields