WordPressな日々

WordPressの制作覚書

テーマ'Twentyfourteen'をカスタマイズする

参考:

WordPressのTwenty Fourteenをカスタマイズ

Twenty Fourteenカスタマイズ | Salty WordPress

funtions.phpの関数多重定義の処理について。

子テーマの作り方 functions.phpのカスタマイズ方法
ryus.co.jp

functions.phpの書き方 [WordPress] functions.phpを編集、修正、管理しやすくする書き方

カスタマイズの目標

現在、Twentyfourteenのトップページには、最新の6つの投稿が'Featured-Content'としてサムネール表示され、その下に最新の投稿1つの記事が表示される。

このしくみを利用して、Featured-Contentをすべて表示する固定ページテンプレートを作る。

なおテーマ修正は、子テーマ'twentyfourteen_child_singularity'で行なう。

トップページの'Featured Post'を表示しているのは、page-top.phpの次の部分。

if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
        // Include the featured content template.
        get_template_part( 'featured-content' );
    }

’twentyfourteen_has_featured_posts() ’は、functions.phpに定義されている関数で、間接的に'twentyfourteen_get_featured_posts'がよばれている。

この関数は、featured postを抜き出すフィルターフックを適用している。

function twentyfourteen_get_featured_posts() {
    /**
     * Filter the featured posts to return in Twenty Fourteen.
     *
     * @since Twenty Fourteen 1.0
     *
     * @param array|bool $posts Array of featured posts, otherwise false.
     */
    return apply_filters( 'twentyfourteen_get_featured_posts', array() ); //<-- 引数array()をわたすフィルターフック'twentyfourteen_get_featured_posts'を作成。その返り値を返す。
}

このフィルターで呼び出される関数は、add_filterで定義する。これは、inc/featured-content.phpに定義されている。

    $filter = $theme_support[0]['featured_content_filter'];

    // Theme can override the number of max posts.
    if ( isset( $theme_support[0]['max_posts'] ) ) {
        self::$max_posts = absint( $theme_support[0]['max_posts'] );
    }

    add_filter( $filter, array( __CLASS__, 'get_featured_posts' )    );

'get_featured_posts'は、同じ inc/featured-content.php内の、(CLASSが指す)Featured_Cotentクラスの静的メソッドとして定義されている。このメソッドの返り値は、featured postの配列になる。

$theme_supportは、

 public static function init() {
        $theme_support = get_theme_support( 'featured-content' );

で、get_theme_support("featured-content')の返り値として定義している。これは、function.php

 // Add support for featured content.
    add_theme_support( 'featured-content', array(
        'featured_content_filter' => 'twentyfourteen_get_featured_posts',
        'max_posts' => 6,
    ) );

と定義されているもの。

public static function get_featured_posts() {
        $post_ids = self::get_featured_post_ids();

        // No need to query if there is are no featured posts.
        if ( empty( $post_ids ) ) {
            return array();
        }

        $featured_posts = get_posts( array(
            'include'        => $post_ids,
            'posts_per_page' => count( $post_ids ),
        ) );

        return $featured_posts;
    }

投稿数は、'posts_per_page' => count( $post_ids ), で設定している。

$post_idsは、次の get_featured_posts() の中で、get_featured_post_ids()の返り値を代入。

public static function get_featured_posts() {
        $post_ids = self::get_featured_post_ids();

        // No need to query if there is are no featured posts.
        if ( empty( $post_ids ) ) {
            return array();
        }

        $featured_posts = get_posts( array(
            'include'        => $post_ids,
            'posts_per_page' => count( $post_ids ),
        ) );

        return $featured_posts;
    }

get_featured_post_ids()は次の通り。$featured_ids = ... の箇所を参照すると、'numberposts'というのがあり、これは `self::$max_posts''が代入されている。

なお、get_transient()/set_transient() は、一種のキャッシュ。また、フィルターフック名は、theme_supportの返り値として取得している。

public static function get_featured_post_ids() {
        // Get array of cached results if they exist.
        $featured_ids = get_transient( 'featured_content_ids' );

        if ( false === $featured_ids ) {
            $settings = self::get_setting();
            $term     = get_term_by( 'name', $settings['tag-name'], 'post_tag' );

            if ( $term ) {
                // Query for featured posts.
                $featured_ids = get_posts( array(
                    'fields'           => 'ids',
                    'numberposts'      => self::$max_posts,
                    'suppress_filters' => false,
                    'tax_query'        => array(
                        array(
                            'field'    => 'term_id',
                            'taxonomy' => 'post_tag',
                            'terms'    => $term->term_id,
                        ),
                    ),
                ) );
            }

            // Get sticky posts if no Featured Content exists.
            if ( ! $featured_ids ) {
                $featured_ids = self::get_sticky_posts();
            }

            set_transient( 'featured_content_ids', $featured_ids );
        }

        // Ensure correct format before return.
        return array_map( 'absint', $featured_ids );
    }

$max_postsは、

// Theme can override the number of max posts.
        if ( isset( $theme_support[0]['max_posts'] ) ) {
            self::$max_posts = absint( $theme_support[0]['max_posts'] );
        }

によって、$theme_support[0]の'max_posts'によって、初期値の15をオーバーライドされている。

$theme_supportは、functions.phpのadd_theme_support()で定義している。

ここで、

$filter = $theme_support[0]['featured_content_filter'];

という行がある。functions.php

// Add support for featured content.
    add_theme_support( 'featured-content', array(
        'featured_content_filter' => 'twentyfourteen_get_featured_posts',
        'max_posts' => 6,
    ) );

で、'featured_content_filter'には'twentyfourteen_get_featured_posts'が定義されているので、上の$filterには、'twentyfourteen_get_featured_posts'が代入されることになる。

add_theme_supportで設定した情報は、get_theme_supportで取得する。ふたたび、inc/featured-content.phpのpublic static function init()に、

$theme_support = get_theme_support( 'featured-content' );

という行があり、ここでテーマサポートのfeatured-contentを取得している。

get_template_part( 'featured-content' );

は、featured-content.phpを読み込む。inc/featured-content.phpではなく、themeトップ階層のfeatured-content.phpであることに注意。

$featured_posts = twentyfourteen_get_featured_posts();

foreach ( (array) $featured_posts as $order => $post ) :
    setup_postdata( $post );

     // Include the featured content template.
    get_template_part( 'content', 'featured-post' );
endforeach;

さらに、get_template_part( 'content', 'featured-post' ); で、content-featured-post.php (featured-post1件のサムネイル画像とタイトルを表示するテンプレート)が読みこまれる。

if ( has_post_thumbnail() ) :
            if ( 'grid' == get_theme_mod( 'featured_content_layout' ) ) {
                the_post_thumbnail();
            } else {
                the_post_thumbnail( 'twentyfourteen-full-width' );
            }
        endif;

の、if ( 'grid' == get_theme_mod( 'featured_content_layout' ) )  は、管理画面、外観ーカスタマイズ、「おすすめコンテンツ」のレイアウトの設定値がグリッドかどうかの条件分岐。

補足

テーマTwentyFourteenのget_featured_postは、inc/featured-content.php内に定義されている。【wordpress】twentyfourteen_get_featured_postsフックの呼び元

'CLASS'は自分自身のクラスを指す。inc/featured-content.php内の

add_filter( $filter,                              array( __CLASS__, 'get_featured_posts' )    );

はこれが書かれているクラス、すなわち、'Featured_Content'クラスの、'get_featured_posts'関数を指す。