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

コメントを残す

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

Your post must include a link to this post.

How does this work?
Write a response on your own website and include a link to this post. Then submit your response’s URL here. Your response may appear here after moderation. To update or remove it, edit or delete your original post and submit the same URL again. Learn more about Webmentions.