ACF(Advance Custom Field)で自動挿入される邪魔なpタグを除去!
WordPressで自動挿入されるpタグ(オートパラグラフ)
を無効化する最も簡単な方法といえば
functions.phpに
<?php remove_filter ('the_content', 'wpautop'); ?>
を追記することですが、
この方法ではACFのWYSIWYGエディタに
自動挿入されるpタグを除去することはできません。
ACFで自動pタグを除去したい場合、
次のように記述する必要があります。
remove_filter ('acf_the_content', 'wpautop');
また、出力するフィールドごとに
自動挿入するもとしないものを分けたい場合は
次のような関数を作成しておくと便利です。
function the_field_without_wpautop( $field_name ) {
remove_filter('acf_the_content', 'wpautop');
the_field( $field_name );
add_filter('acf_the_content', 'wpautop');
}
function the_subfield_without_wpautop( $field_name ) {
remove_filter('acf_the_content', 'wpautop');
the_sub_field( $field_name );
add_filter('acf_the_content', 'wpautop');
}
使い方は引数にフィールド名を持たせて、
通常のフィールドの場合は
the_field_without_wpautop( ‘フィールド名’ )を
サブフィールドの場合は
the_subfield_without_wpautop( ‘フィールド名’ )
を
フィールドを出力したい個所に記述するだけ。
<?php the_field_without_wpautop('title');?>
ACFで自動挿入されるpタグにお困りの方は
ぜひこの解決策を試してみてください。



コメント