wordpressで「Seamless Sticky Custom Post Types」のプラグインをインストールして、カスタム投稿タイプでも、ページの固定表示できるようにしました。ピン止めした記事一覧を任意のページで表示したいのですが、その方法が分かりませんでした。
<?php $sticky = get_option( 'sticky_posts' ); var_dump($sticky); $args = array( 'posts_per_page' => 3, 'post__in' => $sticky, 'post_type' =>'news' ); $query = new WP_Query( $args ); if ( isset($sticky[0]) ) { the_title('<h1 class="page-title">', '</h1>'); } ?>
このようなコードを試してみたのですが、ピン止めした記事は表示できませんでした。var_dump()で、$stickyの値をみてみると、IDが取得できたので、ピン止めした記事のIDの取得までは出来ることは確認できています。
このサイトのコードも試してみたのですが、何も表示されませんでした。
ここのコードも試してみましたが、no posts foundの方に飛んでしまします。
<?php /* Get all sticky posts */ $sticky = get_option( 'sticky_posts' ); /* Get the 5 newest stickies (change 5 for a different number) */ $sticky = array_slice( $sticky, 0, 5 ); /* Query sticky posts */ $the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1 ) ); // The Loop if ( $the_query->have_posts() ) { $return .= '<ul>'; while ( $the_query->have_posts() ) { $the_query->the_post(); $return .= '<li><a href="' .get_permalink(). '" title="' . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>'; } } else { echo "no posts found"; } /* Restore original Post Data */ wp_reset_postdata(); get_footer();?>
【追記】
var_dump()の結果は
array(2) { [0]=> int(170) [1]=> int(202) }
となっています。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/19 13:30