Web制作忘備録

WPパンくず自作

トップ MEMORANDAM WORDPRESS

WPパンくず自作

ページの種類ごとに出し方が違うので、ページのテンプレごとに書くか、別ファイルにまとめて
<?php include(‘フォルダ名/ファイル名.php’); ?>で呼び出す。
function.phpに書く方法もある

固定ページ・親子関係で表示する時(page.php)

<div class="bread">
            <a href="/" class="btn_ro">トップ</a>
            <?php
            $parents_id = get_post_ancestors($post);   //親・先祖ページ全て取得
            $parents_id = array_reverse ( $parents_id );  //親・先祖で出力されるのでarray_reverseで逆にする
            ?>
            <?php foreach ($parents_id as $parent_id): ?>
                <?php
                $parent_link = get_permalink($parent_id); //親URL
                $parent_slug = get_post($parent_id)->post_name; // 親スラッグ
                $parent_title = get_post($parent_id)->post_title; // 親タイトル
                $parent_content = get_post($parent_id)->post_content;  //親の記事本文
                $parent_category = get_post($parent_id)->post_category; //親カテゴリー
                ?>
            <a href="<?php echo $parent_link;?>" class="btn_ro"><?php echo $parent_title?></a>
            <?php endforeach; ?>
            <p class="cure"><?php the_title(); ?></p>
</div>

投稿の個ページ(single.php)

<div class="bread">
    <a href="/">トップ</a>
    <a href="/" class="btn_ro">MEMORANDAM</a>  //投稿一覧

        <?php
        $category = get_the_category();
        $cat_name = $category[0]->cat_name;
        $cat_url = get_category_link($category[0]);
        ?>
        <a href="<?php echo $cat_url; ?>" class="btn_ro"><?php echo $cat_name; ?></a>

    <p class="cure"><?php the_title(); ?></p>
</div>

カテゴリページの場合(category.php)

<div class="bread">
    <a href="/" class="btn_ro">トップ</a>
    <a href="/memorandam/" class="btn_ro">MEMORANDAM</a>  //投稿一覧

    <?php
    $category_obj = get_queried_object();  //現在のページのカテゴリ情報取得
    $category_id = $category_obj->term_id;  //現在のページのカテゴリID
    $category_parent = $category_obj->category_parent; //親カテゴリのID取得

    $cat_parents_id = get_ancestors($category_id,'category');  //先祖情報取得
    $cat_parents_id = array_reverse ( $cat_parents_id );  //先祖情報の並べを逆にする
    ?>

    <?php foreach ($cat_parents_id as $cat_parent_id): ?>
        <?php
        $ct_url = get_category_link($cat_parent_id); //親URL
        $ct_slug = get_category($cat_parent_id)->slug; // 親スラッグ
        $ct_title = get_category($cat_parent_id)->name; // 親タイトル
        $ct_parent = get_category($cat_parent_id)->parent; // 親タイトル
        ?>

        <a href="<?php echo $ct_url;?>" class="btn_ro"><?php echo $ct_title;?></a>

    <?php endforeach; ?>

    <p class="cure"><?php echo $category_obj->name; ?></p>  //現在のカテゴリ名出力

</div>

タグページの場合(tag.php)

<?php if (is_tag()): ?>
    <a href="/memorandam" class="btn_ro">MEMORANDAM</a>  //投稿一覧
    <p class="cure"><?php single_tag_title(); ?></p>
<?php endif; ?>