LoginSignup
2
1

More than 1 year has passed since last update.

【Breadcrumb NavXT】アクションフックによるパンくずリストのタイトル・URLの変更

Last updated at Posted at 2022-03-01

※この記事では、Breadcrumb NavXT5.5.1を使用しています。

パンくずリストのカテゴリーのアーカイブページのURLを固定ページのURLに変更したいとの要望を受けたので、ちょっと調べて書いてみました。(というか、2年前にも同じ依頼があったので、公開できるように書き直しました。)

前提条件として、該当するカテゴリーのアーカイブページは使用しません。(表に出せない)

参考URL
http://notnil-creative.com/blog/archives/981

タイトルのみ変更の場合

function.php

<?php
function my_filter_breadcrumbs($bcnObj) {
    if ( count($bcnObj->trail) > 0 ) {
        for ( $i = 0; $i < count($bcnObj->trail); $i++ ) {
            if ( '変更前のテキスト' == $bcnObj->trail[$i]->get_title() ) {
            	$bcnObj->trail[$i]->set_template('変更後のテキスト');
            }
        }
    }
    return $bcnObj;
}
add_action('bcn_after_fill', 'my_filter_breadcrumbs');

URLも変更する場合

function.php

<?php
function my_filter_breadcrumbs($bcnObj) {
    $home_url = get_home_url( null, '/' );
    $url = $home_url . "/" . "変更したいURLのホームURL以下のアドレス";
    $new_url ='<a title="%ftitle%へ移動" href="'. $url .'"  class="%type%">変更後のタイトル</a>';
    
    if ( count($bcnObj->trail) > 0 ) {
        for ( $i = 0; $i < count($bcnObj->trail); $i++ ) {
            if ( '変更前のタイトル' == $bcnObj->trail[$i]->get_title() ) {
            $bcnObj->trail[$i]->set_template($new_url);
            }
        }
    }
    return $bcnObj;
}
}
add_action('bcn_after_fill', 'my_filter_breadcrumbs');

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1