我在努力工作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,我很确定我遗漏了一些东西。非常感谢您的帮助;)