我一直在关注下面的案子有一段时间了。我在一页上列出了tags
我试着过滤。例如,当帖子有ID
属于10586
, 显示的唯一标记应为10586
.
的名称
taxonomy
是
post_tag
该项的值与
ID
该帖子已打开。带有
ID
属于
10586
, 有一个名为
10586
.
我试图通过以下代码实现这一点:
function my_acf_load_field( $field )
{
global $post;
$post_id = get_the_ID();
$field[\'taxonomy\'] = array();
wp_reset_query();
$query = new WP_Query(array(
\'post_type\' => \'actor\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1,
\'tag\' => $post_id,
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_tag\',
\'field\' => \'slug\',
\'terms\' => \'post_id\'
)
)
));
foreach($query->posts as $product_id=>$macthed_product){
$choices[$macthed_product->ID] = $macthed_product->post_title;
}
$field[\'taxonomy\'] = array();
if( is_array($choices) )
{
foreach( $choices as $key=>$choice )
{
$field[\'taxonomy\'][$key] = $choice;
if($choice->name == $post->ID){
$choice = get_the_terms( $post->ID, \'taxonomy\' );
}
}
}
wp_reset_query();
return $field;
}
add_filter(\'acf/load_field/name=actor_tags_actor_profile\', \'my_acf_load_field\');
在函数中运行代码后。php返回如上图所示的标记。非常感谢您的帮助。
SO网友:codeminator
最后,我更改了复选框的标记,并应用了以下代码,仅显示值等于其显示位置的复选框。
function my_acf_load_field( $field )
{
global $post;
$post_id = get_the_ID();
$field[\'choices\'] = array();
wp_reset_query();
$query = new WP_Query(array(
\'post_type\' => \'projectsoverview\',
\'orderby\' => \'menu_order\',
\'order\' => \'ASC\',
\'posts_per_page\' => -1,
\'tag\' => $post_id
));
foreach($query->posts as $product_id=>$macthed_product){
$choices[$macthed_product->ID] = $macthed_product->post_title;
}
$field[\'choices\'] = array();
if( is_array($choices) )
{
foreach( $choices as $key=>$choice )
{
$field[\'choices\'][$key] = $choice;
}
}
wp_reset_query();
return $field;
}
add_filter(\'acf/load_field/name=actor_tags_actor_profile\', \'my_acf_load_field\');