如何在WordPress快捷代码中显示分类

时间:2016-08-28 作者:Sfic

我想为WordPress创建类似[流派]的短代码。

add_action( \'init\', \'cptui_register_my_cpts_movie\' );
function cptui_register_my_cpts_movie() {
    $labels = array(
        "name" => __( \'MOVIE\', \'\' ),
        "singular_name" => __( \'Movie\', \'\' ),
        "menu_name" => __( \'MOVIES LIBRARY\', \'\' ),
        "all_items" => __( \'All Movies\', \'\' ),
        "add_new" => __( \'Add New Movie\', \'\' ),
        "add_new_item" => __( \'Add New Movie\', \'\' ),
        "edit" => __( \'Edit\', \'\' ),
        "edit_item" => __( \'Edit Movie\', \'\' ),
        "new_item" => __( \'New Movie\', \'\' ),
        "view" => __( \'View\', \'\' ),
        "view_item" => __( \'View Movie\', \'\' ),
        "search_items" => __( \'Search Movie\', \'\' ),
        "not_found" => __( \'No Movies found\', \'\' ),
        "not_found_in_trash" => __( \'No Movies found in Trash\', \'\' ),
        "parent_item_colon" => __( \'Parent Movie\', \'\' ),
        );

    $args = array(
        "label" => __( \'MOVIE\', \'\' ),
        "labels" => $labels,
        "description" => "All Publish Tools for Movies",
        "public" => true,
        "publicly_queryable" => false,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => false,
        "show_in_menu" => true,
                "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "movie", "with_front" => true ),
        "query_var" => true,

        "supports" => array( "title", "editor", "comments", "revisions", "thumbnail" ),     
        "taxonomies" => array( "genre", "year", "quality", "country", "awards", "type", "stars", "subtitle" ),
            );
    register_post_type( "movie", $args );

// End of cptui_register_my_cpts_movie()
}
我想在短代码中显示“流派”

function shortcode_for_genre() {
    ob_start();
    the_terms( $post->ID, array( \'taxonomy\' => \'genre\',), \'\', \', \', \' \' );
    return ob_get_clean();
}
add_shortcode (\'genre\', \'shortcode_for_genre\');
我尝试过上述代码和其他一些代码,但没有一个有效。这个短代码显示了帖子的类型。喜欢戏剧,动作。等

2 个回复
SO网友:Z. Zlatev

我在没有测试的情况下发现的第一个问题是$post 变量未全球化。尝试改变您的shortcode_for_genre() 功能类似:

function shortcode_for_genre() {
    // only proceed if viewing a singular movie cpt.
    if ( ! is_singular( \'movie\' ) )
        return \'\';

    global $post;

    ob_start();
    the_terms( $post->ID, array( \'taxonomy\' => \'genre\',), \'\', \', \', \' \' );
    return ob_get_clean();
}

add_shortcode (\'genre\', \'shortcode_for_genre\');

SO网友:Laxmana

我假设您有一个自定义的post类型电影和一个注册的分类法,该分类法被称为流派。

的第二个参数the_terms 功能错误。它必须是字符串而不是数组:

the_terms( $post->ID, \'genre\', \'\', \', \', \' \' );
你也不需要ob_start() 和返回ob_get_clean(). 使用get_the_termsget_the_term_list 而不是the_terms 并根据需要设置术语的格式。

add_shortcode (\'genre\', \'shortcode_for_genre\');

    function shortcode_for_genre($atts){

        if ( ! is_singular( \'movie\' ) ) return \'\';

        $html = \'\';            

        $terms = get_the_terms( get_the_ID(), \'genre\' );

        if ( $terms && ! is_wp_error( $terms ) ) { 

            foreach ( $terms as $term ) {

                $link = get_term_link( $term, \'genre\' );

               if ( is_wp_error( $link ) ) {
                   return \'\';
               }

               $html .= \'<a href="\' . esc_url( $link ) . \'" rel="tag">\' . $term->name . \'</a>\';

            }


        }

        return $html;

    }

add_shortcode (\'genre\', \'shortcode_for_genre\');

function shortcode_for_genre($atts){

            if ( ! is_singular( \'movie\' ) ) return \'\';

           return get_the_term_list( get_the_ID(), \'genre\', \'\', \', \', \'\');

        }

相关推荐

About wordpress child themes

我对WordPress和儿童主题有一些问题。据我所知,如果我不想在更新主题时失去任何东西,使用子主题是很重要的。我是WordPress的初学者,到目前为止,我一直在使用PageBuilder(管理面板上的板载自定义选项)自定义我的网站,并在“附加CSS”选项中加入几行CSS。所有这些都是在主主题上完成的(只是玩转尝试学习),现在我想开始使用一个儿童主题。问题如下:我不知道我是否可以像通过管理界面设计父主题那样设计我的子主题,或者我是否必须通过文本编辑器(在我的计算机上,然后通过FTP等方式上传)对所有内容