Breadcrumb che mostrano pagine genitore e figlio

7 mar 2012, 16:12:38
Visualizzazioni: 18K
Voti: 3

Stiamo cercando di creare una funzione per i breadcrumb che mostri non solo il percorso verso la pagina corrente, ma anche verso altre pagine con lo stesso genitore.

Ad esempio, su example.com/parent1/child3, i breadcrumb mostrerebbero Home > Parent1 > Child1 > Child2 > Child3 > Child4 invece di semplicemente Home > Parent1 > Child3.

Stiamo usando una versione modificata dello script breadcrumb da http://dimox.net/wordpress-breadcrumbs-without-a-plugin/:

function dimox_breadcrumbs( $args = array() ) {
$defaults = array(
    'delimiter'         => '»',
    'home'              => 'Home',
    'before'            => '<span class="current">',
    'after'             => '</span>',
    'before_wrapper'    => '<div id="crumbs">',
    'after_wrapper'     => '</div>',
    'before_link'       => '<li>',
    'after_link'        => '</li>'
);

$opts = wp_parse_args( $args, $defaults );

$delimiter = $opts['delimiter'];
$home = $opts['home']; // testo per il link 'Home'
$showCurrent = 0; // 1 - mostra il titolo corrente del post/pagina nei breadcrumb, 0 - non mostrare
$before = $opts['before']; // tag prima del crumb corrente
$after = $opts['after']; // tag dopo il crumb corrente
$b_link = $opts['before_link'];
$a_link = $opts['after_link'];
$title = '';
$sub_title = '';

if ( 1 || (!is_home() && !is_front_page()) || is_paged() ) {

    echo $opts['before_wrapper'];

    $out = '<ul>';

    global $post;
    $homeLink = get_bloginfo('url');
    $out .= $b_link . '<a href="' . $homeLink . '">' . $home . '</a>' . $a_link . $delimiter;

    if ( is_category() ) {
        global $wp_query;
        $cat_obj = $wp_query->get_queried_object();
        $thisCat = $cat_obj->term_id;
        $thisCat = get_category($thisCat);
        $parentCat = get_category($thisCat->parent);
        if ($thisCat->parent != 0) {
            $out .= $b_link . (get_category_parents($parentCat, TRUE, $delimiter)) . $a_link;        
        }

        $title = __( 'Archivio per la categoria "%"', LANGUAGE_ZONE );
        $sub_title = single_cat_title('', false);
        $title = str_replace( '%', $sub_title, $title );

    }elseif ( is_day() ) {
        $out .= $b_link . '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $a_link . $delimiter;
        $out .= $b_link . '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a>' . $a_link . $delimiter;
        $title = $sub_title = get_the_time('d');

    }elseif ( is_month() ) {
        $out .= $b_link . '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $a_link . $delimiter;
        $title = $sub_title = get_the_time('F');

    }elseif ( is_year() ) {
        $title = $sub_title = get_the_time('Y');

    }elseif ( is_search() ) {
        $title = __( 'Risultati di ricerca per "%"', LANGUAGE_ZONE );
        $sub_title = get_search_query();
        $title = str_replace( '%', $sub_title, $title );

    }elseif ( is_single() && !is_attachment() ) {
        if ( get_post_type() != 'post' ) {
            $post_type = get_post_type_object(get_post_type());
            $slug = $post_type->rewrite;
//     echo $b_link . $post_type->labels->singular_name . $a_link . $delimiter;
//     echo $b_link . '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>' . $a_link . $delimiter;
            $title = $sub_title = get_the_title();
        }else {
// blog e altro
            $menu_data = dt_current_menu_item();

//                $cat = get_the_category(); $cat = $cat[0];
//                echo $b_link . get_category_parents($cat, TRUE, ' ' . $delimiter . ' ') . $a_link;
            $out .= $b_link . '<a href=' . esc_url($menu_data['link']) . '>' . $menu_data['title'] . '</a>' . $a_link;
            $title = get_the_title();
            $sub_title = $menu_data['title'];
        }

    }elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
        $post_type = get_post_type_object(get_post_type());
        $title = $sub_title = $post_type->labels->singular_name;

    }elseif ( is_attachment() ) {
        $parent = get_post($post->post_parent);
//            $cat = get_the_category($parent->ID); $cat = $cat[0];
//            $out .= get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
        $out .= $b_link . '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>'  . $a_link . $delimiter;
        $title = $sub_title = get_the_title();

    }elseif ( is_page() && !$post->post_parent ) {
        $title = $sub_title = get_the_title();

    }elseif ( is_page() && $post->post_parent ) {
        $parent_id  = $post->post_parent;
        $breadcrumbs = array();
        while ($parent_id) {
            $page = get_page($parent_id);
            $breadcrumbs[] = $b_link . '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>' . $a_link;
            $parent_id  = $page->post_parent;
        }
        $breadcrumbs = array_reverse($breadcrumbs);
        foreach ($breadcrumbs as $crumb) $out .= $crumb . $delimiter;
        $title = $sub_title = get_the_title();

    }elseif ( is_tag() ) {
        $title = __( 'Post taggati con "%"', LANGUAGE_ZONE );
        $sub_title = single_tag_title('', false);
        $title = str_replace( '%', $sub_title, $title );

    }elseif ( is_author() ) {
        global $author;
        $userdata = get_userdata($author);
        $title = __( 'Articoli pubblicati da %', LANGUAGE_ZONE );
        $sub_title = $userdata->display_name;
        $title = str_replace( '%', $sub_title, $title );

    }elseif ( is_404() ) {
        $title = $sub_title = __( 'Errore 404', LANGUAGE_ZONE );

    }elseif( is_home() ) {
        $title = $sub_title = __( 'Blog', LANGUAGE_ZONE );

    }

    if( strlen($title) > 50 ) {
        $title = substr( $title, 0, 50 );
        $title .= '...';
    }

    $out .= $before . $title . $after;

    if ( get_query_var('paged') ) {
        if( is_category() ||
            is_day() ||
            is_month() ||
            is_year() ||
            is_search() ||
            is_tag() ||
            is_author() )
        {
            $out .= ' (';
        }

        $out .= '&nbsp;' . __('Pagina', LANGUAGE_ZONE) . ' ' . get_query_var('paged');

        if( is_category() ||
            is_day() ||
            is_month() ||
            is_year() ||
            is_search() ||
            is_tag() ||
            is_author() )
        {
            $out .= ')';
        }
    }

    $out .= '</ul>';

    if( $opts['show_trail'] ) {
        echo $out;
    }

    echo '<h1>' . dt_first_letter_filter( $sub_title ) . '</h1>';

    echo $opts['after_wrapper'];

}
} // fine dimox_breadcrumbs

Qualche idea? Lo apprezziamo molto.

3
Commenti

quando inserisco questo nel mio tema (dopo aver commentato la funzione dt_first_letter_function() che non ho)... ottengo genitore > figlio > nipote > sei qui. qual è esattamente il problema?

helgatheviking helgatheviking
7 mar 2012 16:48:00

Vorrei che mostrasse altre pagine dello stesso livello. Quindi invece di mostrare solo il percorso/dove sei stato, mostrerebbe altre pagine figlie con lo stesso genitore. Se Genitore1 ha 4 Pagine Figlie, mostrerebbe tutte e 4 le pagine figlie con stile CSS per evidenziare la pagina figlia corrente. In altre parole, mostrerebbe Home > Genitore1 > Figlio1 > Figlio2 > Figlio3 (Pagina Corrente) > Figlio4. Un esempio di questo utilizzando le categorie può essere trovato qui: link. Pensi che ci sia un modo per farlo senza dover creare un menu personalizzato su ogni pagina?

Nick Nick
7 mar 2012 17:13:37

nick, scusa se non ho capito cosa volevi dato che ho assunto che figlio1 e figlio implicassero una gerarchia di genitore > figlio > nipote invece di una relazione tra fratelli. Ho postato una soluzione che dovrebbe ottenere le pagine fratelli per qualsiasi pagina specifica

helgatheviking helgatheviking
7 mar 2012 17:45:54
Tutte le risposte alla domanda 1
5

Non sono sicuro su come integrarlo nei tuoi breadcrumb... che per natura penso siano gerarchici.

Ma questo dovrebbe darti una lista delle pagine che hanno lo stesso genitore della pagina in cui ti trovi (ovvero i suoi "sibling") anche se in modo completamente non stilizzato

global $post;

$args = array('child_of' => $post->post_parent, 'exclude'=> $post->ID);
if ($post->post_parent) $pages = get_pages($args);

if ($pages) foreach ($pages as $page): 
    echo $page->post_title .'<br/>';
endforeach;

Se elenchi la tua pagina corrente e poi elenchi i suoi sibling, puoi ricreare programmaticamente ciò che stai cercando, credo

modifica 1: imposta postdata così possiamo usare get_permalink(), get_the_title() dentro un "loop" ecc. invece dei metodi dell'oggetto. get_permalink() ottiene l'URL dell'elemento corrente nel loop

global $post;

$args = array('child_of' => $post->post_parent, 'exclude'=> $post->ID);
if ($post->post_parent) $pages = get_pages($args);

if ($pages) :

// memorizza la variabile $temp
$temp = $post;

$siblings = '<ul>';

foreach ($pages as $post): setup_postdata($post); 

    $siblings .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
endforeach;

// ripristina la variabile $post
$post = $temp;

$siblings .= '</ul>';

echo $siblings;

endif;

Gli argomenti sono diversi, ma http://codex.wordpress.org/Function_Reference/get_posts#Reset_after_Postlists_with_offset è il foreach loop con cui di solito lavoro. È anche per get_posts, ma get_pages funziona allo stesso modo tranne che restituisce solo pagine.

7 mar 2012 17:44:28
Commenti

Grazie mille, helga! Ha funzionato perfettamente per far apparire le altre pagine... come potrei farle diventare link? Scusa, sono un principiante con PHP. Apprezzo davvero il tuo aiuto!

Nick Nick
7 mar 2012 18:25:22

GRAZIE!!! Ha spostato le pagine sorelle sulla stessa riga, ma per qualche motivo ancora non puntano alle rispettive pagine...

Nick Nick
8 mar 2012 02:06:31

"pagine sorelle sulla stessa riga"? Le ho fatte visualizzare in un elenco non ordinato così puoi stilizzarle come preferisci. I link funzionano bene per me. Ho appena testato di nuovo. Hai cambiato qualcosa? Questo codice non è pensato per essere incollato direttamente nella funzione breadcrumbs, quindi probabilmente potrebbe... semplicemente non l'ho testato in quel modo.

helgatheviking helgatheviking
8 mar 2012 02:48:07

Sì, scusa. Dovevo sistemare qualcosa nel CSS. Ora funziona perfettamente. Grazie per tutto il tuo aiuto! Lo apprezzo davvero!

Nick Nick
8 mar 2012 13:01:02

prego. Se la mia risposta ha risolto il tuo problema, potresti segnarla come soluzione? (la spunta verde)

helgatheviking helgatheviking
8 mar 2012 16:21:12