仅显示与帖子ID相同的标签

时间:2021-07-07 作者:codeminator

我一直在关注下面的案子有一段时间了。我在一页上列出了tags 我试着过滤。例如,当帖子有ID 属于10586, 显示的唯一标记应为10586.

enter image description here

的名称taxonomypost_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返回如上图所示的标记。非常感谢您的帮助。

2 个回复
SO网友:Kees

能否尝试转义变量$post\\u id?

像这样:“”$post\\u id。“”

因此,请使用单引号两次。

您正在查询一个slug,而您的变量是一个数字。

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\');