WordPress:ページ内で特定のエントリーのcontentを表示する方法。

今回は、Wordpressのページテンプレート上で特定のエントリーを抽出表示する方法試してみましたわ。

<?php
$drafts = $wpdb->get_results(”
SELECT post_content FROM $wpdb->posts
WHERE ID=’126′”);
if ($drafts): foreach ($drafts as $draft): setup_postdata($draft); the_content() ?>
<?php endforeach; ?>
<?php else : ?>
Not Found
<?php endif; ?>

ちゅう感じです。

もともとは固定ページで3カラム、んで、それぞれのカラムで別のコンテンツを編集したいとのことで、エントリーよりもページの方が記事が増えると楽チンなので、ページでの更新を想定してくんでみました。

当初はカスタムフィールドの値を表示するようにしてみましたが、記入するテキストフィールドが 狭いし、ソースを理解していないと記入するのが難しいので、
リッチエディタを使える形で検討、

結果、複数のページのエントリーを直接SQL発行して取得して表示させる方法をとりました。

なんか使えそうならば、つかってみてくださいな>WordPressの方。

参考:

http://codex.wordpress.org/Function_Reference/wpdb_Class#get_results_-_SELECT_Generic_Results

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)