我有一个优质的WordPress主题,默认情况下有面包屑,但在帖子页面上不显示类别(和子类别)项目:主页>分类>子-category>帖子标题我想添加“类别”到我的WP面包屑
相反,它只是显示如下:首页>文章标题
这是处理面包屑的功能:
(我标志着我认为部分代码这个标志很重要,以节省您的时间:========== >>)
// BREADCRUMBS
if (! function_exists('ishyoboy_get_breadcrumbs')) {
function ishyoboy_get_breadcrumbs() {
global $ish_options, $ish_woo_id;
$return = '';
$return .= '<div class="ish-pb-breadcrumbs"><div><div>' . "\n";
if (! is_front_page()) {
if (function_exists('is_woocommerce')) {
if (!is_woocommerce() && !is_woocommerce_page()){
$return .= '<a class="ish-pb-breadcrumbs-home" href="';
$return .= home_url();
$return .= '">';
$return .= '<span>' . __('Home', 'ishyoboy') . '</span>';
$return .= "</a> > ";
}
}
else{
$return .= '<a class="ish-pb-breadcrumbs-home" href="';
$return .= home_url();
$return .= '">';
$return .= '<span>' . __('Home', 'ishyoboy') . '</span>';
$return .= "</a> > ";
}
}
else {
$return .= '<span class="ish-pb-breadcrumbs-home">';
$return .= '<span>' . __('Home', 'ishyoboy') . '</span>';
$return .= "</span>";
}
if (!is_front_page() && is_home()) {
global $page;
$return .= $page->post_title;
}
if (is_archive() && 'post' == get_post_type() && ! is_category() && (!function_exists('is_woocommerce') || !is_woocommerce()) ) {
$hpage = get_page(get_option('page_for_posts'));
if ('page' == get_option('show_on_front') && isset($hpage) && '' != $hpage){
$return .= get_page_parents($hpage->ID, TRUE, ' > ', FALSE);
}
if (is_day()) :
$return .= sprintf(__('Daily Archives: %s', 'ishyoboy'), '<span>' . get_the_date() . '</span>');
elseif (is_month()) :
$return .= sprintf(__('Monthly Archives: %s', 'ishyoboy'), '<span>' . get_the_date(_x('F Y', 'monthly archives date format', 'ishyoboy')) . '</span>');
elseif (is_year()) :
$return .= sprintf(__('Yearly Archives: %s', 'ishyoboy'), '<span>' . get_the_date(_x('Y', 'yearly archives date format', 'ishyoboy')) . '</span>');
else :
$return .= __('Archives', 'ishyoboy');
endif;
}
else if (is_archive() && 'post' != get_post_type() && (!function_exists('is_woocommerce') || !is_woocommerce()) ) {
$type = get_post_type(get_the_ID());
$obj = get_post_type_object($type);
if (is_object($obj)){
$return .= $obj->labels->name;
}
}
if ((is_category() || is_single()) && ((!function_exists('is_woocommerce_page') || !function_exists('is_woocommerce')) || (!is_woocommerce() && !is_woocommerce_page()))) {
$post_id = ish_get_the_ID();
$post_type = get_post_type();
switch ($post_type){
case 'portfolio-post' :
$terms = get_the_terms($post_id, 'portfolio-category');
$term = (! empty($terms)) ? array_pop($terms) : '';
if (isset($ish_options['page_for_custom_post_type_portfolio-post']) && '-1' != $ish_options['page_for_custom_post_type_portfolio-post']){
$portfolio_page = get_page($ish_options['page_for_custom_post_type_portfolio-post']);
$separator = " > ";
$return .= '<a href="' . get_page_link($ish_options['page_for_custom_post_type_portfolio-post']) . '" title="' . esc_attr(sprintf(__("View all posts in %s" , 'ishyoboy'), $portfolio_page->post_title)) . '">'.$portfolio_page->post_title.'</a>' . $separator;
}
if (! empty($term)){
$return .= get_term_parents($term->term_id, 'portfolio-category', TRUE, ' > ', FALSE);
}
break;
// from here ==========>>
case 'post' :
if (is_category()){
global $cat;
$category = get_category($cat);
if ($category->parent && ($category->parent != $category->term_id)){
$return .= get_category_parents($category->parent, TRUE, ' > ', FALSE);
}
$return .= single_cat_title('', false);
}
else{
$category = get_the_category();
if (is_object($category)){
$ID = $category[0]->cat_ID;
$return .= get_category_parents($ID, TRUE, ' > ', FALSE);
}
}
break;
// to here ==========>>
default :
$type = get_post_type(get_the_ID());
$obj = get_post_type_object($type);
if (is_object($obj)){
$return .= '<a href="' . get_post_type_archive_link($type) .'">' . $obj->labels->name . '</a> > ';
}
}
}
else if ((function_exists('is_woocommerce_page') && function_exists('is_woocommerce')) && (is_woocommerce() || is_woocommerce_page())){
ob_start();
woocommerce_breadcrumb(array(
'delimiter' => ' > ',
'wrap_before' => '',
'wrap_after' => '',
'before' => '',
'after' => '',
'home' => '<span class="ish-pb-breadcrumbs-home"><span>' . _x('Home', 'breadcrumb', 'woocommerce') . '</span></span>',
));
$return .= ob_get_contents();
ob_end_clean();
ob_end_flush();
}
else if (is_tax()){
if (is_tax('portfolio-category')){
$current_term = get_queried_object();
if (!empty($current_term)){
//var_dump($current_term);
if (isset($ish_options['page_for_custom_post_type_portfolio-post']) && '-1' != $ish_options['page_for_custom_post_type_portfolio-post']){
$portfolio_page = get_page($ish_options['page_for_custom_post_type_portfolio-post']);
$separator = " > ";
$return .= '<a href="' . get_page_link($ish_options['page_for_custom_post_type_portfolio-post']) . '" title="' . esc_attr(sprintf(__("View all posts in %s" , 'ishyoboy'), $portfolio_page->post_title)) . '">'.$portfolio_page->post_title.'</a>' . $separator;
}
if ($current_term->parent != 0){
$return .= get_term_parents($current_term->parent, 'portfolio-category', TRUE, ' > ', FALSE);
}
$return .= $current_term->name;
}
}
}
else if (is_page()){
global $post;
if ($post->post_parent != 0){
$return .= get_page_parents($post->post_parent, TRUE, ' > ', FALSE);
}
}
if (!function_exists('is_woocommerce_page') || !is_woocommerce_page()){
if(is_single()) {$return .= get_the_title();}
if(is_page()) {
global $post;
$frontpage = get_option('page_on_front');
if ($frontpage && $frontpage == $post->ID){
/*$return .= '<a class="home" href="';
$return .= home_url();
$return .= '">';
$return .= '<span>' . __('Home', 'ishyoboy') . '</span>';
$return .= "</a>";*/
} else {
$return .= get_the_title();
}
}
if(is_tag()){ $return .= __('Tag: ', 'ishyoboy') . single_tag_title('',FALSE); }
if(is_404()){ $return .= __('404 - Page not Found', 'ishyoboy'); }
if(is_search()){ $return .= __('Search', 'ishyoboy'); }
if(is_year()){ $return .= get_the_time('Y'); };
}
$return .= '</div></div></div>';
return $return;
}
}
if (!function_exists('the_post_thumbnail_caption')) {
function the_post_thumbnail_caption(){
$thumb = get_post_thumbnail_id();
if (! empty($thumb)){
$thumb_object = get_post($thumb);
if (! empty($thumb_object)) {
echo '<div class="wp-caption"><p class="wp-caption-text">' . $thumb_object->post_excerpt . '</p></div>';
}
}
}
}
if (!function_exists('get_the_post_thumbnail_caption')) {
function get_the_post_thumbnail_caption(){
$thumb = get_post_thumbnail_id();
if (! empty($thumb))
return get_post($thumb)->post_excerpt;
return null;
}
}
if (! function_exists('ishyoboy_activate_fancybox_on_blog_single')) {
function ishyoboy_activate_fancybox_on_blog_single() {
if (is_singular() && !is_singular('product')){
?>
<script type="text/javascript">
jQuery(document).ready(function($){
var thumbnails = jQuery("a:has(img)").not(".nolightbox").filter(function() { return /\.(jpe?g|png|gif|bmp)$/i.test(jQuery(this).attr('href')) });
if (thumbnails.length > 0){
thumbnails.addClass('openfancybox-image').attr('rel', 'fancybox-post-image-<?php the_ID() ?>');
}
});
</script>
<?php
}
}
}
add_action('wp_head', 'ishyoboy_activate_fancybox_on_blog_single');
if (! function_exists('ishyoboy_dummy_functions')) {
function ishyoboy_dummy_functions() {
if (false) {
posts_nav_link();
wp_link_pages();
$args = '';
add_theme_support('custom-header', $args);
add_theme_support('custom-background', $args);
}
}
}
然后我所做的代码产生所需变化的变化 - 类别出现在面包屑:首页>分类>子类别>帖子标题
但在分类存档页屑是这样的:首页>分类>类别
活生生的例子:http://pigtelligent.com/science/
改变我“VE在上述代码所做的:
case 'post' :
$terms = get_the_terms($post_id, 'category');
$term = (! empty($terms)) ? array_pop($terms) : '';
if (! empty($term)){
$return .= get_term_parents($term->term_id, 'category', TRUE, ' > ', FALSE);
}
我知道,有人更聪明,这应该是很容易的,但我花了一整天试图弄清楚这一点,所以你可以帮帮我吗? :)
你是对的,我用的是什么文件,这的确是* is_archive() * 现在,您将如何解决“停止所有额外的逻辑并只是吐出类别名称”? – 2014-12-11 16:52:11
太好了。如果(is_category()|| is_single())&& ...) 尝试: 'if(is_archive()){...} else if(is_category() )){...} else if(is_single()&& ...){}' – 2014-12-11 23:09:43