Я хочу добавить & quot; категорию & quot; на мои панировочные сухари

У меня есть тема WordPress премиум-класса, которая по умолчанию содержит панировочные сухари, но на странице поста не отображается элемент категории (и подкатегории): Домой> Категория> Подкатегория> Заголовок сообщения

Вместо этого он просто выглядит так: Главная> Заголовок сообщения

Это функция, которая обрабатывает панировочные сухари:

(Я пометил ту часть кода, которая, на мой взгляд, важна этим знаком, чтобы сэкономить ваше время: ========== >>)

// 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> &gt; ";
}

}
else{
$return .= '<a class="ish-pb-breadcrumbs-home" href="';
$return .= home_url();
$return .= '">';
$return .= '<span>' . __( 'Home', 'ishyoboy' ) . '</span>';
$return .= "</a> &gt; ";
}

}
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, ' &gt; ', 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 = " &gt; ";
$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, ' &gt; ', 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, ' &gt; ', 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, ' &gt; ', 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> &gt; ';
}

}
}
else if ( ( function_exists('is_woocommerce_page') && function_exists('is_woocommerce') ) && (is_woocommerce() || is_woocommerce_page() )){
ob_start();
woocommerce_breadcrumb(array(
'delimiter'   => ' &gt; ',
'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 = " &gt; ";
$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, ' &gt; ', FALSE );
}

$return .= $current_term->name;
}
}
}
else if (is_page()){
global $post;

if ($post->post_parent != 0 ){
$return .= get_page_parents($post->post_parent, TRUE, ' &gt; ', 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/

Изменение, которое я внес в вышеупомянутый код:

        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, ' &gt; ', FALSE );
}

Я знаю, что для кого-то умнее это должно быть легко, но я потратил целый день, пытаясь понять это, так что вы можете мне помочь? 🙂

2

Решение

Категории могут быть хитрыми быстро, поскольку посты могут иметь несколько категорий, а целевые страницы категорий могут иметь доступ к большому количеству мета, которые могут вводить в заблуждение. Убедитесь, что вы просмотрели Иерархия шаблонов на странице кодекса, и вы получите четкое представление о том, как WP обрабатывает таксономию.

использование Что за файл чтобы выяснить, по какому шаблону вы находитесь на самом деле, поскольку вы не можете быть на странице категории, в результате чего is_category() вернуть ложь. Вы могли бы быть на одном (как if утверждение, заключающее в себе указанную логику, проверяет is_category() || is_single()), или вам может понадобиться добавить проверку для is_archive ().

Мое лучшее предложение — сначала выяснить, находитесь ли вы на странице категории, затем прекратить всю эту лишнюю логику и просто выплюнуть название категории. Не слишком много помощи, я знаю, но, надеюсь, это укажет вам правильное направление

0

Другие решения

function the_breadcrumb() {
global $post;
echo '<ul id="breadcrumbs">';
if (!is_home()) {
echo '<li><a href="';
echo get_option('home');
echo '">';
echo 'Home';
echo '</a></li><li class="separator"> / </li>';
if (is_category() || is_single()) {
echo '<li>';
the_category(' </li><li class="separator"> / </li><li> ');
if (is_single()) {
echo '</li><li class="separator"> / </li><li>';
the_title();
echo '</li>';
}
} elseif (is_page()) {
if($post->post_parent){
$anc = get_post_ancestors( $post->ID );
$title = get_the_title();
foreach ( $anc as $ancestor ) {
$output = '<li><a href="'.get_permalink($ancestor).'" title="'.get_the_title($ancestor).'">'.get_the_title($ancestor).'</a></li> <li class="separator">/</li>';
}
echo $output;
echo '<strong title="'.$title.'"> '.$title.'</strong>';
} else {
echo '<li><strong> '.get_the_title().'</strong></li>';
}
}
}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
echo '</ul>';
}
?>

Получить с помощью: <?php the_breadcrumb(); ?>

0