前提・実現したいこと
Wordpress初心者です。
お忙しいところすみませんが、以下質問です。
1.自分で調べてみて他サイトを参考に「ad-in-body.php」ファイルを作成して、子テーマのfunction.phpの<?php以下に {「最初の見出しタグ(h2)」の前に自動で設置できるコード} を入れるということをやってみたのですが、なぜかうまくいきません。
(広告そのものが表示されない)
2.また、子テーマのfunction.phpには以前に上記とは別に、抜粋文の「...」の表記を「続きを読む」に変えて記事にリンクさせるためのリンクも入れてはいるのですが、そちらもうまくいきません。何かfunction.phpの記載方法が誤っているのか自分ではわからないため、アドバイスいただけますと助かります。
3.広告の上に表示されるはずの「スポンサーリンク」の言葉がなぜか文字化けしてしまいます。こちらも大変恐縮ですが、原因不明のため何かアドバイスいただけますと助かります。
情報不足でわかりづらいかもしれませんが、アドバイスなどいただけますと大変幸甚に存じます。
さらにFTPソフトで子テーマ中のcontent.phpを見ると必要なコードが記載されているのですが、Wordpressダッシュボードのテーマの編集⇒子テーマ⇒content.phpを見ると真っ白になってしまっています。(関係があるのか不明)
(4月13日 追記)
使い方よくわかっておらず大変失礼しました。
以下に子テーマのfunction.phpを貼り付けます。
また使用しているテーマはTwenty Twelveというものを使っており、子テーマを作成して編集しております。
https://ja.wordpress.org/themes/twentytwelve/
発生している問題・エラーメッセージ
該当のソースコード
<?php /** * Sets the post excerpt length to 40 words. * * To override this length in a child theme, remove the filter and add your own * function tied to the excerpt_length filter hook. */ function twentytwelve_excerpt_length( $length ) { return 40; } add_filter( 'excerpt_length', 'twentytwelve_excerpt_length' ); if ( ! function_exists( 'twentytwelve_continue_reading_link' ) ) : /** * Returns a "Continue Reading" link for excerpts */ function twentytwelve_continue_reading_link() { return ' <a href="'. esc_url( get_permalink() ) . '">' . __( '続きを読む <span class="meta-nav">→</span>', 'twentytwelve' ) . '</a>'; } endif; // twentytwelve_continue_reading_link /** * Replaces "[...]" (appended to automatically generated excerpts) with an ellipsis and twentytwelve_continue_reading_link(). * * To override this in a child theme, remove the filter and add your own * function tied to the excerpt_more filter hook. */ function twentytwelve_auto_excerpt_more( $more ) { return ' …' . twentytwelve_continue_reading_link(); } add_filter( 'excerpt_more', 'twentytwelve_auto_excerpt_more' ); /** * Adds a pretty "Continue Reading" link to custom post excerpts. * * To override this link in a child theme, remove the filter and add your own * function tied to the get_the_excerpt filter hook. */ function twentytwelve_custom_excerpt_more( $output ) { if ( has_excerpt() && ! is_attachment() ) { $output .= twentytwelve_continue_reading_link(); } return $output; } add_filter( 'get_the_excerpt', 'twentytwelve_custom_excerpt_more' ); ?> add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } //H2見出しを判別する正規表現を定数にする define('H2_REG', '/<h2.*?>/i');//H2見出しのパターン //本文中にH2見出しが最初に含まれている箇所を返す(含まれない場合はnullを返す) //H3-H6しか使っていない場合は、h2部分を変更してください function get_h2_included_in_body( $the_content ){ if ( preg_match( H2_REG, $the_content, $h2results )) {//H2見出しが本文中にあるかどうか return $h2results[0]; } } function add_ads_before_1st_h2($the_content) { if ( is_single() ) {//固定ページも表示する場合はis_singular()にする ob_start();//バッファリング get_template_part('ad-in-body');//広告貼り付け用に作成したテンプレート $ad_template = ob_get_clean(); $h2result = get_h2_included_in_body( $the_content );//本文にH2タグが含まれていれば取得 if ( $h2result ) {//H2見出しが本文中にある場合のみ //最初のH2の手前に広告を挿入(最初のH2を置換) $count = 1; $the_content = preg_replace(H2_REG, $ad_template.$h2result, $the_content, 1); } } return $the_content; } add_filter('the_content','add_ads_before_1st_h2'); ?>
試したこと
エディターはTerapadを使っておりましたが、こちらの文字コードをUTF-8Nに変えたところ、Wordpressダッシュボード画面でみる各コードが真っ白になっているという現象は解決されました。