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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//-----------------------------------
//固定ページに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');
//----------------------------------- //固定ページに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');
//-----------------------------------
//固定ページに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」フォルダに入れる。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
[myphp_information file='information']
[myphp_information file='information']
[myphp_information file='information']

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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>
<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件、投稿日、記事へのリンク、タイトルを出力。

ページトップへ移動