query_posts、wp_query、またはget_postsを使用して、ページ内でさまざまなカスタムループを作成する方が良い方法ですか?

wordpress.stackexchange https://wordpress.stackexchange.com/questions/8028

  •  16-10-2019
  •  | 
  •  

質問

今、私は使用しています get_posts このような静的なコンテンツを生成するために、カスタム分類法でカスタム分類法を備えたCusstom投稿タイプを取得するには:

<?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

役に立ちましたか?

解決

やあ @janochen:

選択肢がある場合は、 一緒に行く WP_Query. 両方の他の関数(query_posts()get_posts()) 電話 WP_Query 間接的に。

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

はどうかと言うと get_posts(), 、それはただのラッパーです WP_Query 予期しない潜在的なデフォルトがあるので、電話をかけることもできます WP_Query 直接的かつそれらの問題を避けてください。

ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top