web-dev-qa-db-ja.com

親ページと子ページを表示するブレッドクラム

現在のページへのパスだけでなく、同じ親ページを持つ他のページへのパスも表示するパンくずリスト用の関数を作成しようとしています。

たとえば、example.com/parent1/child3では、ブレッドクラムには、単にHome> Parent1> Child3ではなく、Home> Parent1> Child1> Child2> Child3> Child4と表示されます。

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']; // text for the 'Home' link
$showCurrent = 0; // 1 - show current post/page title in breadcrumbs, 0 - don't show
$before = $opts['before']; // tag before the current crumb
$after = $opts['after']; // tag after the current crumb
$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 = __( 'Archive by category "%"', 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 = __( 'Search results for "%"', 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 and other stuff
            $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 = __( 'Posts tagged "%"', LANGUAGE_ZONE );
        $sub_title = single_tag_title('', false);
        $title = str_replace( '%', $sub_title, $title );

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

    }elseif ( is_404() ) {
        $title = $sub_title = __( 'Error 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;' . __('Page', 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'];

}
} // end dimox_breadcrumbs

何か案は?本当に感謝しています。

3
Nick

ブレッドクラムに戻して作業することについてのidk ...これは本質的に階層的だと思います。

しかし、これはあなたが現在いるページと同じ親を持つページのリスト(別名、その兄弟)をあなたに与えるでしょう。

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;

あなたのcurrentページをリストアップして、それからその兄弟をリストアップするなら、あなたはプログラムであなたが探しているものを作り直すことができます私が思う

編集1: postdataを設定するので、オブジェクトメソッドの代わりにget_permalink()、get_the_title()を「ループ」などの中で使用できます。 get_permalink()は、ループ内の現在の項目へのURLを取得します

global $post;

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

if ($pages) :

//store the $temp variable
$temp = $post;

$siblings = '<ul>';

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

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

//restore the $post variable
$post = $temp;

$siblings .= '</ul>';

echo $siblings;

endif;

引数は異なりますが、 http://codex.wordpress.org/Function_Reference/get_posts#Reset_after_Postlists_with_offset は私が通常使用するforeachループです。 get_postsもありますが、get_pagesはページを返すこと以外は同じように機能します。

6
helgatheviking