Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Topic: https://ja.wordpress.org/support/topic/投稿記事内に固定ページを挿入することは可能で/
- Description: 投稿記事内に固定ページを挿入することは可能でしょうか(コメントを含む)
- Author: https://ja.forums.wordpress.org/profile/13907727
- */
- add_shortcode( 'mypage', 'my_insert_page_with_comments' );
- function my_insert_page_with_comments( $atts ) {
- $atts = shortcode_atts( array(
- 'id' => '', // 固定ページのID (スラッグより優先)
- 'slug' => '', // 固定ページのスラッグ
- 'count' => 5 // 表示するコメントの最大数
- ),
- $atts );
- $mypage = NULL;
- if ( $atts['id'] ) // IDが指定されていたら
- $mypage = get_post( $atts['id'] ); // IDで固定ページを取得
- elseif ( $atts['slug'] ) // スラッグが指定されていたら
- $mypage = reset( get_posts( array(
- 'name' => $atts['slug'], // スラッグで
- 'post_type' => 'page' // 固定ページを取得
- ) ) );
- if ( empty( $mypage ) ) // 見つからなかった
- return '指定された固定ページが見つかりません。';
- $s = '<div>固定ページここから'; // ショートコードを展開した結果
- // 固定ページへジャンプするリンクを取得
- $link = get_page_link( $mypage->ID );
- $s .= "<div>";
- $s .= "ジャンプ: <a href=\"{$link}\">{$mypage->post_title}</a>";
- $s .= '</div>';
- // 固定ページの本文を取得
- $s .= '<hr /><div>';
- $s .= wpautop( do_shortcode( $mypage->post_content ) );
- $s .= '</div><hr />';
- // 固定ページのコメントを取得
- $comments = get_comments( array(
- 'number' => $atts['count'], // 表示するコメントの最大数
- 'post_id' => $mypage->ID
- ) );
- // コメントをフォーマットして取得
- $comments = wp_list_comments( array( 'echo' => false ), $comments );
- $s .= $comments;
- $s .= '固定ページここまで</div>'; // ショートコードを展開した結果
- return $s;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement