E 'meglio pratica usare query_posts, WP_Query o get_posts per creare vari cicli personalizzati all'interno di una pagina?

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

  •  16-10-2019
  •  | 
  •  

Domanda

In questo momento, sto usando get_posts per recuperare i tipi di messaggi Cusstom con una tassonomia personalizzato assegnato ad esso al fine di generare contenuti statici in questo modo:

<?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(); ?>

Che cosa è una migliore pratica in questo caso?

Per usare query_posts, WP_Query o get_posts?

entrare descrizione dell'immagine qui

È stato utile?

Soluzione

Ciao @janoChen:

Se avete una scelta, andare con WP_Query. Entrambe le altre funzioni (query_posts() e get_posts()) chiamata WP_Query indirettamente.

Il primo è progettato per consentire di modificare la query principale dopo che la query standard è già stato eseguito, per esempio quando si desidera un secondo ciclo. Ma query_posts() colpisce variabili globali e può avere effetti collaterali. Se l'uso possibile WP_Query, invece, e il codice sarà più robusto.

Per quanto riguarda get_posts(), è solo un wrapper WP_Query con alcuni valori di default potenzialmente inaspettate modo si potrebbe anche chiamare direttamente WP_Query ed evitare questi problemi.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top