更智能的导航插件和自定义分类

时间:2014-04-22 作者:user50694

我在努力工作Smarter Navigation\'sget_referrer_category() 使用我创建的自定义分类法。我在函数中包含的自定义分类法。php如下所示:

add_action( \'init\', \'create_colors_nonhierarchical_taxonomy\', 0 );

function create_colors_nonhierarchical_taxonomy() {

  $labels = array(
    \'name\' => _x( \'Colors\', \'taxonomy general name\' ),
    \'singular_name\' => _x( \'Color\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Colors\' ),
    \'popular_items\' => __( \'Popular Colors\' ),
    \'all_items\' => __( \'All Colors\' ),
    \'parent_item\' => null,
    \'parent_item_colon\' => null,
    \'edit_item\' => __( \'Edit Color\' ), 
    \'update_item\' => __( \'Update Color\' ),
    \'add_new_item\' => __( \'Add New Color\' ),
    \'new_item_name\' => __( \'New Color Name\' ),
    \'separate_items_with_commas\' => __( \'Separate colors with commas\' ),
    \'add_or_remove_items\' => __( \'Add or remove colors\' ),
    \'choose_from_most_used\' => __( \'Choose from the most used colors\' ),
    \'menu_name\' => __( \'Colors\' ),
  ); 

  register_taxonomy(\'colors\',\'post\',array(
    \'hierarchical\' => false,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'update_count_callback\' => \'_update_post_term_count\',
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'color\' ),
  ));
}
现在,由于我不仅需要突出显示类别,还需要突出显示用户来自的标记和自定义分类法,因此我在下面的代码块中进行了更改this plugin file:

// Retrieve the category, based on the referrer URL. Useful if you have posts with multiple categories
function get_referrer_category() {
    global $posts;

    if ( ! $referrer_url = get_referrer_url( false ) )
        return false;

    foreach ( get_the_category( $posts[0]->ID ) as $cat ) {
        $cat_link = get_category_link( $cat->term_id );

        if ( false !== strpos( $referrer_url, $cat_link ) )
            return $cat;
    }

    foreach ( get_the_tags( $posts[0]->ID ) as $tag ) {
        $tag_link = get_tag_link( $tag->term_id );

        if ( false !== strpos( $referrer_url, $tag_link ) )
            return $tag;
    }

    foreach ( get_the_terms( $posts[0]->ID, \'color\' ) as $term ) {
        $term_link = get_term_link( $term->term_id );

        if ( false !== strpos( $referrer_url, $term_link ) )
            return $term;
    }

    return false;
}
我将这些行添加到内容单中。php:

<?php
  if ( $cat = get_referrer_category() )
    echo \'<span>Exploring // <a href="\' . get_category_link( $cat->term_id ) . \'">\' . $cat->slug . \'</a>\';
?>
类别和标记工作正常,但我仍然无法显示自定义分类法。我一点也不擅长PHP,我很确定我遗漏了一些东西。非常感谢您的帮助;)

1 个回复
SO网友:user50694

我已经解决了这个问题。以下是工作代码:

foreach ( get_the_terms( $posts[0]->ID, \'colors\' ) as $term ) {
    $term_link = get_term_link( $term, \'colors\' );

    if ( false !== strpos( $referrer_url, $term_link ) )
        return $term;
}

结束

相关推荐

Set terms in a custom post

我有一个自定义贴子“艺术家”和一个分类/类别“艺术家类别”,我想创建(通过开发)一个新贴子并设置艺术家的类别,但它不起作用。我用以下代码对其进行了测试:function createNewPost( $response ){ global $userMeta; $userID = $response->ID; $user = new WP_User( $userID ); $role = $userMeta->