WordPressでホームページを作成しています。
トップページに特定のカテゴリーの固定ページ(投稿記事ではなく)の一覧を設置したいと思っています。
★タイトル
★アイキャッチ画像
★文章の抜粋
を掲載したいです。
固定ページのカテゴリーは
functions.phpに
//固定ページにカテゴリーを設定
add_action('init','add_categories_for_pages');
function add_categories_for_pages(){
register_taxonomy_for_object_type('category', 'page');
}
add_action( 'pre_get_posts', 'nobita_merge_page_categories_at_category_archive' );
function nobita_merge_page_categories_at_category_archive( $query ) {
if ( $query->is_category== true && $query->is_main_query() ) {
$query->set('post_type', array( 'post', 'page', 'nav_menu_item'));
}
}
と記入して解決しました。
あとはカテゴリーを指定した固定ページのタイトル、アイキャッチ、文の抜粋です。
投稿記事の一覧であれば
<!-- 新着順にする--> <?php query_posts($query_string .'&orderby=modified'); ?> <!-- カテゴリーを指定する--> <?php $posts = get_posts("numberposts=10 &category=3"); foreach ($posts as $post): setup_postdata($post); ?> <div class="topics"> <h3><a href="<?php the_permalink() ?>"><?php the_title() ?></a></h3> <div class="topics_img"> <a href="<?php the_permalink(); ?>"> <?php the_post_thumbnail(); ?></a> </div> <?php echo mb_substr(get_the_excerpt(),0, 50);?> <a href="<?php the_permalink();?>"> … 続きを読む</a> </div> <?php endforeach; ?> で実現できますが、 固定ページの場合は反応がありません。 投稿記事の場合とどこが違うのでしょうか。 コードを書き直すとすればどのようにすればよいでしょうか。 また、他に良い方法がありましたらアドバイスをお願いしたいと思っております。