web-dev-qa-db-ja.com

Page内にさまざまなカスタムループを作成するためにquery_posts、WP_Query、またはget_postsを使用することをお勧めしますか。

今のところ、私はget_postsを使用して、カスタム分類法が割り当てられたカスタム投稿タイプを取得して、このような静的コンテンツを生成します。

<?php
/**
 * Template Name: Front Page
 * @package WordPress
 * @subpackage Prominent
 * @since Prominent 1.0
 */
get_header(); ?>
<div class="shadow-top">
    <!-- Shadow at the top of the slider -->
</div>
<div id="intro">
    <div class="container">
        <div id="slider-wrapper">
            <div id="slider">
            <?php // Create custom loop ?>
            <?php $custom_posts = get_posts('post_type=page_content&page_sections=Slider (Front Page)'); ?>
            <?php foreach( $custom_posts as $post ) : setup_postdata( $post ); ?>
                <p><?php the_content(); ?></p>
            <?php endforeach; ?>
            <?php wp_reset_query(); ?>
            </div>
            <div class="shadow-slider">
                <!-- Shadow at the bottom of the slider -->
            </div>
        </div><!-- #slider-wrapper -->
    </div><!-- .container -->
</div><!-- #featured -->
<div class="shadow-bottom">
    <!-- Shadow at the bottom of the slider -->
</div>
<div id="tagline">
    <div class="container">
        <?php
        $page_id = $post->ID; // 123 should be replaced with a specific Page's id from your site, which you can find by mousing over the link to edit that Page on the Manage Pages admin page. The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123.
        $page_data = get_page( $page_id ); // You must pass in a variable to the get_page function. If you pass in a value (e.g. get_page ( 123 ); ), Wordpress will generate an error.

        $content = apply_filters('the_content', $page_data->post_content); // Get Content and retain Wordpress filters such as paragraph tags. Origin from: http://wordpress.org/support/topic/get_pagepost-and-no-paragraphs-problem
        $title = $page_data->post_title; // Get title
        ?>
        <div class="content0">
            <h2><?php echo $content; // Output Content ?></h2>
        </div>
    </div><!-- .container -->
</div><!-- #content-bottom -->
<div id="content">
    <div class="container">
        <div class="mainbar">
            <?php // Create custom loop
            $custom_posts = get_posts('post_type=page_content&page_sections=Content (Front Page)'); ?>
            <?php foreach( $custom_posts as $post ) : setup_postdata( $post ); ?>
                <div class="content-block">
                    <h2><?php the_title(); ?></h2>
                    <p><?php the_content(); ?></p>
                </div><!-- .content-block -->
            <?php endforeach; ?>
            <?php wp_reset_query(); ?>
        </div><!-- #mainbar -->
    </div><!-- .container -->
</div><!-- #content-bottom -->
<?php get_footer(); ?>

この場合のより良い実践は何ですか?

Query_posts、WP_Query、またはget_postsを使用するには

enter image description here

3
janoChen

こんにちは@ janoChen:

選択肢がある場合は、WP_Query他の関数(query_posts()およびget_posts())の両方が間接的にWP_Queryを呼び出します。

前者は、標準クエリがすでに実行された後、たとえば2回目のループが必要なときに、メインクエリを変更できるように設計されています。しかしquery_posts()はグローバル変数に影響を及ぼし、副作用をもたらす可能性があります。可能であれば代わりにWP_Queryを使用してください。そうすればあなたのコードはより堅牢になります。

get_posts()に関しては、それは単にWP_Queryのまわりの予想外のデフォルトを含むラッパーですので、WP_Queryを直接呼び出してそれらの問題を避けることもできます。

5
MikeSchinkel