清理新帖子类型WordPress中的分类术语

时间:2021-02-02 作者:pasquale

我为一个帖子类型创建了分类法,它们很有效。问题是,当我在前端添加一篇新文章(在文章类型中)时,默认情况下会显示我在前一篇文章中输入的分类术语。实际上,新职位的分类术语不会重置。是否有此功能?我如何解决?

添加帖子类型

    if ( ! function_exists(\'bookmaker_post_type\') ) {

// Register Custom Post Type
function bookmaker_post_type() {

    $labels = array(
        \'name\'                  => _x( \'Bookmakers\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'         => _x( \'Bookmaker\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'             => __( \'Bookmakers\', \'text_domain\' ),
        \'name_admin_bar\'        => __( \'Bookmakers Reviews\', \'text_domain\' ),
        \'archives\'              => __( \'Item Archives\', \'text_domain\' ),
        \'attributes\'            => __( \'Item Attributes\', \'text_domain\' ),
        \'parent_item_colon\'     => __( \'Parent Item:\', \'text_domain\' ),
        \'all_items\'             => __( \'All Bookmakers\', \'text_domain\' ),
        \'add_new_item\'          => __( \'Add New Bookmaker\', \'text_domain\' ),
        \'add_new\'               => __( \'Add New Bookmaker\', \'text_domain\' ),
        \'new_item\'              => __( \'New Bookmaker\', \'text_domain\' ),
        \'edit_item\'             => __( \'Edit Bookmaker\', \'text_domain\' ),
        \'update_item\'           => __( \'Update Item\', \'text_domain\' ),
        \'view_item\'             => __( \'View Item\', \'text_domain\' ),
        \'view_items\'            => __( \'View Items\', \'text_domain\' ),
        \'search_items\'          => __( \'Search Item\', \'text_domain\' ),
        \'not_found\'             => __( \'Not found\', \'text_domain\' ),
        \'not_found_in_trash\'    => __( \'Not found in Trash\', \'text_domain\' ),
        \'featured_image\'        => __( \'Featured Image\', \'text_domain\' ),
        \'set_featured_image\'    => __( \'Set featured image\', \'text_domain\' ),
        \'remove_featured_image\' => __( \'Remove featured image\', \'text_domain\' ),
        \'use_featured_image\'    => __( \'Use as featured image\', \'text_domain\' ),
        \'insert_into_item\'      => __( \'Insert into item\', \'text_domain\' ),
        \'uploaded_to_this_item\' => __( \'Uploaded to this item\', \'text_domain\' ),
        \'items_list\'            => __( \'Items list\', \'text_domain\' ),
        \'items_list_navigation\' => __( \'Items list navigation\', \'text_domain\' ),
        \'filter_items_list\'     => __( \'Filter items list\', \'text_domain\' ),
    );
    $args = array(
        \'label\'                 => __( \'Bookmaker\', \'text_domain\' ),
        \'description\'           => __( \'Bookmaker review\', \'text_domain\' ),
        \'labels\'                => $labels,
        \'supports\'              => array( \'title\', \'editor\', \'thumbnail\', \'revisions\', \'custom-fields\' ),
        \'hierarchical\'          => false,
        \'public\'                => true,
        \'show_ui\'               => true,
        \'show_in_menu\'          => true,
        \'menu_position\'         => 5,
        \'show_in_admin_bar\'     => true,
        \'show_in_nav_menus\'     => true,
        \'can_export\'            => true,
        \'has_archive\'           => true,
        \'exclude_from_search\'   => false,
        \'publicly_queryable\'    => true,
        \'capability_type\'       => \'page\',
    );
    register_post_type( \'bookmaker\', $args );

}
add_action( \'init\', \'bookmaker_post_type\', 0 );

}    
添加分类法

function bookmaker_taxonomies() {
    
    // Add new taxonomy Languages, make it hierarchical (like categories)
    $labels = array(
        \'name\'              => _x( \'Languages\', \'taxonomy general name\', \'textdomain\' ),
        \'singular_name\'     => _x( \'Language\', \'taxonomy singular name\', \'textdomain\' ),
        \'search_items\'      => __( \'Search Languages\', \'textdomain\' ),
        \'all_items\'         => __( \'All Languages\', \'textdomain\' ),
        \'parent_item\'       => __( \'Parent Language\', \'textdomain\' ),
        \'parent_item_colon\' => __( \'Parent Language:\', \'textdomain\' ),
        \'edit_item\'         => __( \'Edit Language\', \'textdomain\' ),
        \'update_item\'       => __( \'Update Language\', \'textdomain\' ),
        \'add_new_item\'      => __( \'Add New Language\', \'textdomain\' ),
        \'new_item_name\'     => __( \'New Language Name\', \'textdomain\' ),
        \'menu_name\'         => __( \'Language\', \'textdomain\' ),
    );
 
    $args = array(
        \'hierarchical\'      => true,
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'language\' ),
    );
 
    register_taxonomy( \'language\', array( \'bookmaker\' ), $args );
 
    unset( $args );
    unset( $labels );

add_action( \'init\', \'bookmaker_taxonomies\', 0 );
用于在前端显示分类术语的函数

 function get_term_list($taxonomy){
    
       $terms = get_terms($taxonomy);
     
       echo \'\';
     
          foreach ( $terms as $term ) {
     
           // The $term is an object, so we don\'t need to specify the $taxonomy.
           $term_link = get_term_link( $term );
        
            // If there was an error, continue to the next term.
           if ( is_wp_error( $term_link ) ) {
               continue;
           }
     
           // We successfully got a link. Print it out.
           echo \'<a href="\' . esc_url( $term_link ) . \'">\' . $term->name . ", ",\'</a>\';
    } 
    
   echo \'\';

   }
检索分类术语并在前端显示

<div><?php get_term_list(\'language\'); ?></div>

1 个回复
SO网友:Tom J Nowell

您的代码没有列出post A中的术语,而是列出all 网站上的条款。您可以通过创建帖子Z并向Z添加新术语来测试这一点,您将在所有帖子上看到这些新术语,即使是帖子a。

原因是:

$terms = get_terms($taxonomy);
get_terms 不获取当前帖子上的条款,它只获取条款。

如果您只想要分配给当前职位的术语,则需要使用wp_get_post_terms 相反

$terms = wp_get_post_terms( get_the_ID(), $taxonomy );

https://developer.wordpress.org/reference/functions/wp_get_post_terms/

相关推荐

MySQL query for taxonomy-meta

我直接使用MySQL,使用MySQL workbench。你能帮我写一个SQL查询all custom taxonomies 以及他们的term-meta 字段?我有一个名为(slug)的自定义分类法census-tract 以及附加到每个字段的几个自定义字段:census-tract-income-level census-tract-population我如何加入以下表格wp_terms wp_termmeta wp_term_taxonomy 要查找WHERE wp_ter