某些自定义帖子中未显示自定义分类

时间:2020-04-16 作者:njinok

我已经为自定义帖子类型“Movies”创建了一个自定义分类列表,下面是部分代码:

public function init_post_type() 
    {
        // get label
        $labels = array(
            \'name\'                  => __(\'Movies\', \'tmdb\'),
            \'singular_name\'         => __(\'movie\', \'tmdb\'),
            \'add_new\'               => __(\'Add new movie\', \'tmdb\'),
            \'add_new_item\'          => __(\'Add new movie\', \'tmdb\'),
            \'edit_item\'             => __(\'Edit movie\', \'tmdb\'),
            \'new_item\'              => __(\'New movie\', \'tmdb\'),
            \'view_item\'             => __(\'View movie\', \'tmdb\'),
            \'search_items\'          => __(\'Search into movies\', \'tmdb\'),
            \'not_found\'             => __(\'No movies found\', \'tmdb\'),
            \'not_found_in_trash\'    => __(\'No movies in trash\', \'tmdb\')
        );

        // start formationg arguments
        $args = array(
            \'labels\' => $labels,
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true,
            \'has_archive\' => true,
            \'query_var\' => true,
            \'rewrite\' => true,
            \'menu_icon\' => TMDB_asset_url( \'images/icon.png\' ),
            \'capability_type\' => \'post\',
            \'show_in_menu\' => true,
            \'supports\' => array( \'title\', \'editor\', \'thumbnail\', \'comments\' )
        );

        register_post_type( \'movies\', $args );

        // add taxonomies for this post type
        $this->init_taxonomies();

        add_action( \'admin_head\', array( $this, \'add_32px_icon\' ) );

        // add meta boxes to "movies" post type
        add_action(\'admin_menu\', array($this, \'add_to_menu_metabox\'));

        // change the layout of movies list
        add_filter(\'manage_edit-movies_columns\', array( $this, \'movies_edit_columns\' ) );
        add_action(\'manage_posts_custom_column\', array( $this, \'movies_posts_columns\' ), 10, 2);  
    }

    // Initialize New Taxonomy Labels for the questions custom post type
    public function init_taxonomies() 
    {
        $labels = array(
            \'name\'              => __( \'Franchises\', \'tmdb\' ),
            \'singular_name\'     => __( \'franchise\', \'tmdb\' ),
            \'search_items\'      => __( \'Search franchise Types\', \'tmdb\' ),
            \'all_items\'         => __( \'All franchise Types\', \'tmdb\' ),
            \'parent_item\'       => __( \'Parent franchise Types\', \'tmdb\' ),
            \'parent_item_colon\' => __( \'Parent franchise Types:\', \'tmdb\' ),
            \'edit_item\'         => __( \'Edit franchise Types\', \'tmdb\' ),
            \'update_item\'       => __( \'Update franchise Type\', \'tmdb\' ),
            \'add_new_item\'      => __( \'Add New franchise Type\', \'tmdb\' ),
            \'new_item_name\'     => __( \'New franchise Type\', \'tmdb\' ),
        );


        // register taxonomy to hold our genre
        register_taxonomy(
            \'franchise\',
            array(\'movies\'),
            array(
                \'hierarchical\' => true,
                \'query_var\' => true,
                \'show_in_nav_menus\' => true,
                \'labels\' => $labels,
                \'show_tagcloud\' => false
            )
        );
我使用以下代码显示这些自定义分类:

<?php if( isset($the_movie[\'_tagline\']) ) { ?>
                    <li><span><?php esc_attr_e(\'Tagline\', \'tmdb\'); ?>:</span> <?php echo $the_movie[\'_tagline\'];?></li>
<?php } ?>
<?php if( isset($the_movie[\'_homepage\']) ) { ?>
                    <li><span><?php esc_attr_e(\'Film website\', \'tmdb\'); ?>:</span> <a href="<?php echo $the_movie[\'_homepage\'];?>" target="_blank"><?php echo $the_movie[\'_homepage\'];?></a></li>

<?php the_terms( $post->ID, \'regisseur\', \'Regisseur: \', \', \', \' \' ); ?><br>
<?php the_terms( $post->ID, \'land\', \'Land: \', \', \', \' \' ); ?><br>
<?php the_terms( $post->ID, \'subgenre\', \'Subgenres: \', \', \', \' \' ); ?><br>
<?php the_terms( $post->ID, \'stijl\', \'Stijl: \', \', \', \' \' ); ?><br>
<?php the_terms( $post->ID, \'streaming\', \'Te zien bij: \', \', \', \' \' ); ?><br>
<?php the_terms( $post->ID, \'plot\', \'Plot: \', \', \', \' \' ); ?><br>
<?php } ?>
我知道我可能应该使用get\\u the\\u term\\u list,但它还不能工作。我现在的问题是自定义分类法的最后一个列表。他们表现得很好,除了少数帖子,这对我来说毫无意义。

这里有一个链接,它工作正常:

Link to post that works

这里有一个链接,指向分类法未显示的位置:

Link to post not showing custom taxonomies

我已经尝试过一些事情:

确保填写问题帖子的所有自定义分类字段并保存帖子

1 个回复
SO网友:njinok

我设法让它工作起来了。这是我使用的代码:


<?php echo get_the_term_list( $post->ID, \'regisseur\', \'Regisseur: \', \', \', \'\' ); ?>
所有自定义分类现在都显示良好。我现在唯一需要的是,当自定义分类法没有值时,不要显示空行。如果有人有什么想法?

相关推荐