###前提・実現したいこと
投稿画面でチェックした記事(おすすめ記事)を5件表示させたい
###発生している問題
‘posts_per_page’を最大投稿数と同じ20に設定しているときはちゃんとおすすめ記事が表示されます。しかし、‘posts_per_page’を最大投稿数以下に設定するとうまく表示されなくなるという問題が生じています。
###該当のソースコード
mainSidebar.php↓
<h2 class="mainSiderbarTitle">編集部おすすめ</h2> <ul class="mainSiderbarUl"> <?php $args = array( 'category_name' => $value->slug, 'posts_per_page' => 20, 'ignore_sticky_posts' => 1 ); $news_posts = new WP_Query( $args ); if( $news_posts->have_posts() ): while( $news_posts->have_posts() ): $news_posts->the_post(); if(get_field('postRecommended') ) { ?> <li class="flex-fluid justify-start item-start link"> <a href="<?php the_permalink(); ?>"> <figure class="mainSidebarIconArea"> <?php $img = get_field('postIcon'); $imgurl = wp_get_attachment_image_src($img, 'full'); ?> <?php the_post_thumbnail('medium'); ?> <img src="<?php echo $imgurl[0]; ?>" class="mainSidebarIcon"> </figure> </a> <div class="mainSidebarItem"> <a href="<?php the_permalink(); ?>" class="mainSidebarTitle"> <?php the_title(); ?> </a> <span class="frontPostTag"> <?php the_category(); ?> </span> </div> </li> <?php } endwhile; endif; ?> </ul>
index.php メインループ↓
<!-- ******************************************************************** * 記事一覧 ********************************************************************** --> <!-- 11/23 --> <?php if(is_paged()) : ?> <ul> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <li class="flex-fluid justify-start item-start link frontPostArea"> <a href="<?php the_permalink(); ?>"> <figure class="frontPostIconArea"> <?php $img = get_field('postIcon'); $imgurl = wp_get_attachment_image_src($img, 'full'); ?> <?php the_post_thumbnail('medium'); ?> </figure> </a> <div class="frontPostItem"> <a href="<?php the_permalink(); ?>"> <h3 class="frontPostTitle"> <?php the_title(); ?> </h3> </a> <a href="<?php the_permalink(); ?>"> <div class="frontPostSummary"> <?php echo get_the_content(); ?> </div> </a> <span class="frontPostTag"> <?php the_category(); ?> </span> </div> </li> <?php endwhile; endif; ?> </ul>
index.php mainSidebar.php呼び出し部分↓
<section class="col-sm-12 sp-lg-4" id="rightSidebarSection"> <?php get_template_part("./template/mainSidebar"); ?> </section>
###補足情報(言語/FW/ツール等のバージョンなど)
カスタムフィールド
->フィールド名:postRecommended
->フィールドタイプ:真/偽
管理画面の表示設定では最大投稿数を20に設定しています。
index.phpでmainSidebar.phpをget_template_part()を使って呼び出しています。
回答1件
あなたの回答
tips
プレビュー