子テーマのfunctions.phpをいじる
子テーマの作り方は、以下の関連記事をご参照ください。
子テーマのディレクトリ直下にある「functions.php」に以下のコードを挿入します。
functions.php
- 子テーマ定義
- 自動保存無効化
- Headに(adsense)組み込み
- AMP対応したページ/投稿には、headerにJavaScriptコードは使えません。挿入しても無効化されます。
<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style')
);
}
// 自動保存を無効にする処理
function autosave_off() {
wp_deregister_script('autosave');
}
add_action( 'wp_print_scripts', 'autosave_off' );
// For no support to AMP, AMP do not allow insertion to Header
// 以下は、<Head>と</head>の間にGoogle Adsenseコードを挿入
// ただし、AMPでは、無秩序に挿入は許されないため、AMPを使用する場合は削除
function km_script () {
echo '<div><script data-ad-client="ca-pub-1277399675163968" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script></div>';
}
add_action( 'amp_post_template_head', 'km_script' );
//
?>
ログインしているときに処理する
編集の最中に、投稿内容をブラウザで表示させて確認する場合、「編集」リンクを投稿の最初や最後の方に表示できると便利です。以下は、投稿の最初に表示させるカスタマイズです。同時に前後の投稿ナビゲーションも表示させます。
content.phpに、以下のコードを挿入します。
<?php if (is_user_logged_in()) : ?>
<div>
<?php
if ( is_single() ) {
get_template_part( 'template-parts/navigation' );
}
// 編集
edit_post_link();
?>
</div>
<?php endif;?>
完成した – content.php
- 前後の投稿へのナビゲータ
- 投稿の編集リンクの表示
以下、完成した content.phpのコード
<?php
/**
* The default template for displaying content
*
* Used for both singular and index.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/
*
* @package WordPress
* @subpackage Twenty_Twenty
* @since 1.0.0
*/
?>
<article <?php post_class(); ?> id="post-<?php the_ID(); ?>">
<?php
get_template_part( 'template-parts/entry-header' );
if ( ! is_search() ) {
get_template_part( 'template-parts/featured-image' );
}
?>
<div>
<?php
// 全ページに表示 : カテゴリメニュー
get_template_part( 'template-parts/info-course' );
?>
</div>
<?php if (is_user_logged_in()) : ?>
<div>
<?php
if ( is_single() ) {
get_template_part( 'template-parts/navigation' );
}
// 編集
edit_post_link();
?>
</div>
<?php endif;?>
<div class="post-inner <?php echo is_page_template( 'templates/template-full-width.php' ) ? '' : 'thin'; ?> ">
<div class="entry-content">
<?php
if ( is_search() || ! is_singular() && 'summary' === get_theme_mod( 'blog_content', 'full' ) ) {
the_excerpt();
} else {
the_content( __( 'Continue reading', 'twentytwenty' ) );
}
?>
</div><!-- .entry-content -->
</div><!-- .post-inner -->
<div class="section-inner">
<?php
wp_link_pages(
array(
'before' => '<nav class="post-nav-links bg-light-background" aria-label="' . esc_attr__( 'Page', 'twentytwenty' ) . '"><span class="label">' . __( 'Pages:', 'twentytwenty' ) . '</span>',
'after' => '</nav>',
'link_before' => '<span class="page-number">',
'link_after' => '</span>',
)
);
// 編集
edit_post_link();
// Single bottom post meta.
twentytwenty_the_post_meta( get_the_ID(), 'single-bottom' );
if ( is_single() ) {
get_template_part( 'template-parts/entry-author-bio' );
}
?>
</div><!-- .section-inner -->
<div>
<?php
?>
</div>
<?php
// 全ページに表示 : カテゴリメニュー
get_template_part( 'template-parts/info-course-end' );
if ( is_single() ) {
get_template_part( 'template-parts/navigation' );
}
/**
* Output comments wrapper if it's a post, or if comments are open,
* or if there's a comment number – and check for password.
* */
if ( ( is_single() || is_page() ) && ( comments_open() || get_comments_number() ) && ! post_password_required() ) {
?>
<div class="comments-wrapper section-inner">
<?php comments_template(); ?>
</div><!-- .comments-wrapper -->
<?php
}
?>
</article><!-- .post -->