• Hola.

    Tengo un problema a la hora de insertar los breadcrumbs en mi template, concretamente en las páginas de mi CPT ya que en category todo está perfecto.

    Cuento un poco la estructura del CPT:
    En page-guias tengo todas las guías de viajes. Vamos a poner por ejemplo Málaga, Córdoba y Madrid.

    Cada guía tiene una serie de enlaces que son páginas hijo. Estos enlaces son referentes a Información general, Qué ver, Dónde comer…

    Para poder mostrar la información de las páginas hijo tuve que crear una página intermedia para comprobar si se trataba de la página padre o la página hijo

        if( $post->post_parent != 0 ) {
            include(get_template_directory() .'/guia-hijo.php');
        } else {
            include(get_template_directory() .'/guia-padre.php');
        }

    – page-guias:
    – single-guia.php (comprueba si es padre o hijo)
    – guia-padre.php
    – guia-hijo.php

    Si estoy en la página padre el breadcrumb sería este y está bien:
    Inicio / Guías / Málaga

    Si estoy en la página hijo el breadcrumb sería este pero faltaría incluir la página padre (Málaga):
    Inicio / Guías / Cómo llegar

    No se como podría añadir ese nivel que me falta.
    Este es el código que estoy usando para los breadcrumbs:

    function custom_breadcrumbs() {
           
        // Settings
        $separator          = ' / ';
        $breadcrums_id      = 'breadcrumbs';
        $breadcrums_class   = 'breadcrumbs';
        $home_title         = 'Inicio';
                
        global $post,$wp_query;
           
        if ( !is_front_page() ) {
           
            // Crear los breadcrumbs
            echo '<ul id="' . $breadcrums_id . '" class="' . $breadcrums_class . '">';
               
            // Home page
            echo '<li class="item-home"><a class="bread-link bread-home" href="' . get_home_url() . '" title="' . $home_title . '">' . $home_title . '</a></li>';
            echo '<li class="separator separator-home"> ' . $separator . ' </li>';
               
            if ( is_single() ) {
                  
                // Si post es un CPT
                $post_type = get_post_type();
                  
                // Si es CPT imprime nombre y link
                if( $post_type != 'post' ) {
                      
                    $post_type_object = get_post_type_object( $post_type );
                    $post_type_archive = get_post_type_archive_link( $post_type );
                  
                    echo '<li class="item-cat item-custom-post-type-' . $post_type . '"><a class="bread-cat bread-custom-post-type-' . $post_type . '" href="' . $post_type_archive . '" title="' . $post_type_object->labels->name . '">' . $post_type_object->labels->name . '</a></li>';
                    echo '<li class="separator"> ' . $separator . ' </li>';
                  
                }
                  
                // Obtener información de categoría del post
                $category = get_the_category();
                 
                if( !empty($category) ) {
                  
                    // Get last category post is in
                    $last_category = end(array_values($category));
                      
                    // Get parent any categories and create array
                    $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, ','),',');
                    $cat_parents = explode(',',$get_cat_parents);
                      
                    // Loop through parent categories and store in variable $cat_display
                    $cat_display = '';
                    foreach($cat_parents as $parents) {
                        $cat_display .= '<li class="item-cat">'.$parents.'</li>';
                        $cat_display .= '<li class="separator"> ' . $separator . ' </li>';
                    }
                 
                }
                                
                // Comprobar si es post está en una categoría
                if( !empty($last_category) ) {
                    echo $cat_display;
                    echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
                      
                // Else if post is in a custom taxonomy
                } else {
                      
                    echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '" title="' . get_the_title() . '">' . get_the_title() . '</strong></li>';
                }
                  
            } else if ( is_category() ) {
                   
                // Category page
                echo '<li class="item-current item-cat"><strong class="bread-current bread-cat">' . single_cat_title('', false) . '</strong></li>';
                   
            } else if ( is_page() ) {
                   
                // Standard page
                if( $post->post_parent ){
                       
                    // If child page, get parents 
                    $anc = get_post_ancestors( $post->ID );
    
                    // Get parents in the right order
                    $anc = array_reverse($anc);
                       
                    // Parent page loop
                    if ( !isset( $parents ) ) $parents = null;
                    foreach ( $anc as $ancestor ) {
                        $parents .= '<li class="item-parent item-parent-' . $ancestor . '"><a class="bread-parent bread-parent-' . $ancestor . '" href="' . get_permalink($ancestor) . '" title="' . get_the_title($ancestor) . '">' . get_the_title($ancestor) . '</a></li>';
                        $parents .= '<li class="separator separator-' . $ancestor . '"> ' . $separator . ' </li>';
                    }
                       
                    // Display parent pages
                    echo $parents;
                       
                    // Current page
                    echo '<li class="item-current item-' . $post->ID . '"><strong title="' . get_the_title() . '"> ' . get_the_title() . '</strong></li>';
                       
                } else {
                       
                    // Just display current page if not parents
                    echo '<li class="item-current item-' . $post->ID . '"><strong class="bread-current bread-' . $post->ID . '"> ' . get_the_title() . '</strong></li>';
                       
                }
                   
            } 
            echo '</ul>';
        }   
    }
Viendo 2 respuestas - de la 1 a la 2 (de un total de 2)
Viendo 2 respuestas - de la 1 a la 2 (de un total de 2)
  • El debate ‘Problema con breadcrumbs’ está cerrado a nuevas respuestas.