WordPressな日々

WordPressの制作覚書

アクションフックについて

featured-contentの'do_action( 'twentyfourteen_featured_posts_before' );'と'do_action( 'twentyfourteen_featured_posts_after' );'は、アクション・フックと呼ばれるもので、この場所で任意の関数を実行できる。do_action()に引数はフックの場所を識別する名前。

add_action( 'twentyfourteen_featured_posts_after', 'display_text' );

というように、add_action(フック場所、関数)という形で実行する関数を定義する。

参考: WordPress › Support » featured_posts_before and featured_posts_after

テーマ’TwentyFourteen'で、アクションフックの使用例。

// Include the featured content template.
    <div id="featured-content" class="featured-content">
       <div class="featured-content-inner">
       <?php
        /**
         * Fires before the Twenty Fourteen featured content.
         *
         * @since Twenty Fourteen 1.0
         */
        do_action( 'twentyfourteen_featured_posts_before' );

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

        /**
         * Fires after the Twenty Fourteen featured content.
         *
         * @since Twenty Fourteen 1.0
         */
        do_action( 'twentyfourteen_featured_posts_after' );

        wp_reset_postdata();
       ?>
       </div><!-- .featured-content-inner -->
    </div><!-- #featured-content .featured-content -->