Quantcast
Channel: デジタルポケット
Viewing all articles
Browse latest Browse all 33

[wordpress] パンくずの構造化マークアップ

$
0
0

パンくずを構造化するメリット

クローラーがWebサイトの構造を把握しやすいので検索エンジン対策になる。
検索結果にページURLではなく(http://ドメイン.com >> カテゴリー名 >> 投稿名)のように、パンくずを表示できる。

microdataによるパンくずを構造化マークアップする方法

(1)下記functions.phpに追記
(2)テンプレートの任意の位置にタグを記述

(1)functions.phpに追記

function get_breadcrumbs(){
    global $wp_query;
    if ( !is_home() ){
        // Start the UL
        echo '<ul class="bcs" itemscope="itemscope" itemtype="http://data-vocabulary.org/Breadcrumb">';
        // Add the Home link
        echo '<li><a href="'. get_settings('home') .'" itemprop="url"><span itemprop="title">ホーム&nbsp;</span></a></li>';
        if ( is_category() ) {
            $catTitle = single_cat_title( "", false );
            $cat = get_cat_ID( $catTitle );
            echo '<li> &raquo;&nbsp;<span itemprop="title">' . get_category_parents( $catTitle, TRUE, " &raquo; " ) ."</span></li>";
        } elseif ( is_archive() && !is_category() ) {
            echo '<li> &raquo;&nbsp;<span itemprop="title">Archives</span></li>';
        } elseif ( is_search() ) {
            echo '<li> &raquo;&nbsp;<span itemprop="title">Search Results</span></li>';
        } elseif ( is_404() ) {
            echo '<li> &raquo;&nbsp;<span itemprop="title">404 Not Found</span></li>';
        } elseif ( is_single() ) {
            $category = get_the_category();
            $category_id = get_cat_ID( $category[0]->cat_name );
            echo '<li> &raquo;&nbsp;<span itemprop="title">'. get_category_parents( $category_id, TRUE, " &raquo; " ) .'</span></li>';
            echo '<li> &raquo;&nbsp;<span itemprop="title">'. the_title('','', FALSE) .'</span></li>';
        } elseif ( is_page() ) {
            $post = $wp_query->get_queried_object();
            if ( $post->post_parent == 0 ){
                echo '<li> &raquo;&nbsp;<span itemprop="title">'.the_title('','', FALSE)."</span></li>";
            } else {
                $title = the_title('','', FALSE);
                $ancestors = array_reverse( get_post_ancestors( $post->ID ) );
                array_push($ancestors, $post->ID);
  
                foreach ( $ancestors as $ancestor ){
                    if( $ancestor != end($ancestors) ){
                        echo '<li> &raquo;&nbsp;<span itemprop="title"><a href="'. get_permalink($ancestor) .'" itemprop="url">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</a></span></li>';
                    } else {
                        echo '<li> &raquo;&nbsp;<span itemprop="title">'. strip_tags( apply_filters( 'single_post_title', get_the_title( $ancestor ) ) ) .'</span></li>';
                    }
                }
            }
        }
        // End the UL
        echo "</ul>";
    }
}

(2016/01/06 修正)

(2)テンプレートの任意の位置にタグを記述
 header.phpなど

  <!-- pankuzu -->
   <?php get_breadcrumbs(); ?>
  <!-- /pankuzu -->

Viewing all articles
Browse latest Browse all 33

Trending Articles