LoginSignup
1
0

More than 5 years have passed since last update.

Breadcrumb NavXTでliに付けたclassやitemcopeが削除されて保存される場合の対処法

Last updated at Posted at 2017-01-29

何が起きたか

Breadcrumb NavXT」というWordPressプラグインを用いて生成するパンくずリストをSchema.orgのBreadcumbに対応させるため、以下のように出力タグに属性をつけようと思いました。

<!-- リンクなし -->
<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="name">%htitle%</span><meta itemprop="position" content="%position%"></li>
<!-- リンクあり -->
<li itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><a itemscope itemtype="http://schema.org/Thing" itemprop="item" title="%title%" href="%link%"><span itemprop="name">%htitle%</span></a><meta itemprop="position" content="%position%"></li>

しかし、出力されるタグには<li>の属性だけなくなった状態で出力されてしまいました...。

<!-- リンクなし -->
<li><span itemprop="name">%htitle%</span><meta itemprop="position" content="%position%"></li>
<!-- リンクあり -->
<li><a itemscope itemtype="http://schema.org/Thing" itemprop="item" title="%title%" href="%link%"><span itemprop="name">%htitle%</span></a><meta itemprop="position" content="%position%"></li>

対処法

functions.phpに、Breadcrumb NavXTのbcn_allowed_htmlという関数にhookする関数を加えることで解決できました。

functions.php
function my_bcn_allowed_html($allowed_html)
{
    $allowed_html['li'] = array(
        'title' => true,
        'class' => true,
        'id' => true,
        'itemprop' => true,
        'itemscope' => true,
        'itemtype' => true
    );
    return $allowed_html;
}
add_filter('bcn_allowed_html', 'my_bcn_allowed_html');

参考

https://wordpress.org/support/topic/why-cant-i-add-itemscope-and-itemtype-to-category-template/
https://mtekk.us/archives/guides/how-to-add-li-and-other-tags-to-breadcrumb-templates/

1
0
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
1
0