WordPressで自動で挿入されてしまうpタグを消す
|
目次
これが結構厄介です。
記事内でプログラムなどを説明するときや、結果でCSSを導入するときなど、勝手にpタグが入ってしまうとHTMLやCSSが効かなくなったりしてしまうのです。 プラグインでも無効にできますが、今回は手動での対処方法を書きます。
Pタグとはこれ
<p>このタグのこと</p>
個別ページのみでPタグを削除する方法
個別ページ(single.phpやpage.phpなど)の先頭に下記を記述する方法
<?php remove_filter('the_content', 'wpautop'); ?>
<?php the_content(); ?>
functions.phpに記述する方法
自動整形を無効にする投稿タイプを6行目
$arr_types = array('投稿タイプ');
このような感じで記述してください。
add_filter('the_content', 'wpautop_filter', 9);
function wpautop_filter($content) {
global $post;
$remove_filter = false;
$arr_types = array('page'); //ここを変更
$post_type = get_post_type( $post->ID );
if (in_array($post_type, $arr_types)) $remove_filter = true;
if ( $remove_filter ) {
remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');
}
return $content;
}
スポンサードリンク
全ての投稿でPタグを削除する方法
functions.phpに記述する方法
remove_filter(‘the_content’, ‘wpautop’); remove_filter(‘the_excerpt’, ‘wpautop’);
テーマ全体でPタグを削除する方法
functions.phpに記述する方法
add_action('init', function() {
remove_filter('the_excerpt', 'wpautop');
remove_filter('the_content', 'wpautop');
});
add_filter('tiny_mce_before_init', function($init) {
$init['wpautop'] = false;
$init['apply_source_formatting'] = ture;
return $init;
});
HTMLが勝手に消されることのみに対応する場合(pタグ自動挿入は有)
これはちょっと変化球的な感じです。
add_action('init', function() {
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
remove_filter('the_excerpt', 'wpautop');
remove_filter('the_excerpt', 'wptexturize');
remove_filter('the_title', 'wpautop');
remove_filter('the_title', 'wptexturize');
remove_filter('the_editor_content', 'wp_richedit_pre');
});
add_filter('tiny_mce_before_init', function($init) {
$init['wpautop'] = false;
$init['apply_source_formatting'] = ture;
return $init;
});
WordPress開発のお供に・・書籍はいかがでしょう
スポンサードリンク
コメントを書き込む