Blog

【WordPress】投稿記事・固定ページの<p>タグを削除する方法

2016/12/20

投稿 カスタム投稿

備忘録です。

投稿記事・固定ページで自動挿入される<p>タグを削除する方法です。

参考サイト
wordpressに自動挿入されるPタグを削除する3つの方法
知らないと恥ずかしい!WordPressでpタグを自動挿入させない方法

functions.pnpに記述
全ての投稿記事・固定ページで<p>タグが削除されます。

remove_filter('the_content', 'wpautop');
remove_filter( 'the_excerpt', 'wpautop' );

投稿テンプレートsingle.php、固定ページのテンプレートpage.phpに記述
個別のテンプレートだけ指定できます。

<?php remove_filter('the_content', 'wpautop'); ?>
<?php the_content(); ?>

特定の投稿タイプで<p>タグを削除
functions.pnpに記述

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;
}

カテゴリー

月間アーカイブ

MORE

ミュージシャンズ・プラザ

神社仏閣ホームーページ制作

ホームページ制作問合せ