固定ページにカスタム投稿のphpをインクルードして投稿一覧などを表示

//-----------------------------------
//固定ページにphpインクルード
//-----------------------------------
function Include_my_php_information($params = array()) {
    extract(shortcode_atts(array(
        'file' => 'default'
    ), $params));
    ob_start();
    include(get_theme_root() . '/' . get_template() . "/inc/information.php");
    return ob_get_clean();
}
 
add_shortcode('myphp_information', 'Include_my_php_information');

上記の「information」部分を変更して使用する。「information.php」は「inc」フォルダに入れる。

[myphp_information file='information']

上記を固定ページに挿入する。

<ul>
	<?php
		global $post;
		$args = array(
		'posts_per_page' => 999,
		'post_type'=> 'information'
		);
		$myposts = get_posts( $args );
		foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
		<li><span><?php the_date( 'Y年m月d日' ); ?></span><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
		<?php endforeach;
		wp_reset_postdata();
    ?>
</ul>

上記は「information.php」の例。カスタム投稿「information」を999件、投稿日、記事へのリンク、タイトルを出力。

ページトップへ移動