質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Q&A

解決済

1回答

5513閲覧

sticky postsでピン止めした記事リストをページに表示したい。

destrudo

総合スコア143

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

0グッド

0クリップ

投稿2017/05/19 09:54

編集2017/05/19 10:37

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) }
となっています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

最初のコードを以下のようにしてみました。

php

1<?php 2$sticky = get_option( 'sticky_posts' ); 3$args = array( 4 'posts_per_page' => 3, 5 'post__in' => $sticky, 6 'post_type' =>'news' 7); 8$query = new WP_Query( $args ); 9while ( $query->have_posts() ) : $query->the_post(); //追加 10if ( isset($sticky[0]) ) { 11 the_title('<h1 class="page-title">', '</h1>'); 12} 13?> 14<?php endwhile; ?> //追加 15<?php wp_reset_postdata(); ?> //追加 16

参考まで。

追記

php

1<?php 2/* Get all sticky posts */ 3$sticky = get_option( 'sticky_posts' ); 4 5/* Get the 5 newest stickies (change 5 for a different number) */ 6$sticky = array_slice( $sticky, 0, 5 ); 7 8/* Query sticky posts */ 9$the_query = new WP_Query( array( 'post__in' => $sticky, 'ignore_sticky_posts' => 1, 'post_type' => 'news' ) ); //カスタム投稿を追加 10// The Loop 11if ( $the_query->have_posts() ) { 12// $return .= '<ul>'; 13 echo '<ul>'; //functions.phpに書き込まないならechoで 14 while ( $the_query->have_posts() ) { 15 $the_query->the_post(); 16// $return .= '<li><a href="' .get_permalink(). '" title="' . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>'; 17 echo '<li><a href="' .get_permalink(). '" title="' . get_the_title() . '">' . get_the_title() . '</a><br />' . get_the_excerpt(). '</li>'; //functions.phpに書き込まないならechoで 18 } 19 echo '</ul>'; //閉じ忘れ 20} else { 21 echo "no posts found"; 22} 23/* Restore original Post Data */ 24wp_reset_postdata(); 25get_footer(); ?>

もう一つの方も修正してみました。
紹介されているサイトに書いてあるのは、functions.phpに書くやり方のようです。
なので $return を echo に直しました。

参考まで。

投稿2017/05/19 11:10

編集2017/05/19 13:01
8-0_nyan5

総合スコア2352

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

destrudo

2017/05/19 13:30

ありがとうございます!できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問