wp_queryコールでwp_reset_query()を使用する必要がありますか?
-
16-10-2019 - |
質問
次のコードを使用して投稿を取得します。
<?php
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=5&cat=3');
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
<h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<div class="meta">
By <?php the_author() ?>
</div>
<div class="storycontent">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
使用する必要がありますか wp_reset_query()
?もしそうなら、どこに配置すればよいですか?
解決
やあ @janochen:
簡単な答え: 番号。
以下は、関数のPHPコードが何であるかです wp_reset_query()
から /wp-includes/query.php
WordPress v3.0.4およびその後呼び出された関数。あなたはそれが主にグローバル変数の変更においてであることを見ることができます。
使用するとき new WP_Query($args)
値から値からローカル変数に割り当てられるので、この質問に対する答えをすでに知っているので、電話する必要はありません。 wp_reset_query()
:
function wp_reset_query() {
unset($GLOBALS['wp_query']);
$GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
wp_reset_postdata();
}
function wp_reset_postdata() {
global $wp_query;
if ( !empty($wp_query->post) ) {
$GLOBALS['post'] = $wp_query->post;
setup_postdata($wp_query->post);
}
}
function setup_postdata($post) {
global $id, $authordata, $day, $currentmonth, $page, $pages, $multipage, $more, $numpages;
$id = (int) $post->ID;
$authordata = get_userdata($post->post_author);
$day = mysql2date('d.m.y', $post->post_date, false);
$currentmonth = mysql2date('m', $post->post_date, false);
$numpages = 1;
$page = get_query_var('page');
if ( !$page )
$page = 1;
if ( is_single() || is_page() || is_feed() )
$more = 1;
$content = $post->post_content;
if ( strpos( $content, '<!--nextpage-->' ) ) {
if ( $page > 1 )
$more = 1;
$multipage = 1;
$content = str_replace("\n<!--nextpage-->\n", '<!--nextpage-->', $content);
$content = str_replace("\n<!--nextpage-->", '<!--nextpage-->', $content);
$content = str_replace("<!--nextpage-->\n", '<!--nextpage-->', $content);
$pages = explode('<!--nextpage-->', $content);
$numpages = count($pages);
} else {
$pages = array( $post->post_content );
$multipage = 0;
}
do_action_ref_array('the_post', array(&$post));
return true;
}
-マイク
他のヒント
それは必要ありません WP_Query
それ自体が しかし 関連する機能/メソッドを使用する場合(または少なくとも良いこと) the_post()
また setup_postdata()
)グローバル変数をデータで埋める。
基本的に新しい作成 WP_Query
オブジェクトは単なるデータ検索ですが、それを使用してアクティブループを実行し、テンプレートタグにアクセスできるようにすると環境が変更され、その後すべてをリセットすることができます。
全体的に - それを呼ぶことは意味のあるパフォーマンスのペナルティではないので、それをすべきか忘れて、不思議なことに壊れているかどうかを決定するよりも、常に電話をかける方が簡単です。
アップデート
wp_reset_postdata()
機能はより適切な選択のようです。 wp_reset_query()
グローバルをリセットします $wp_query
(どのカスタム WP_Query
オブジェクトは影響しません) と $post
(上記のように)変数。 wp_reset_postdata()
復元のみ $post
, 、それで十分です。
いいえ。独自のWP_QUERYオブジェクトをインスタンス化する場合、それはあなたがそうすることに関係しています。ただし、改ざんする場合 global $wp_query
変数、まあ、その変数を同時に使用している人のスクリプトに影響を与えるグローバルネームスペースで。また、データを変更するために何かをする場合は、使用した後もリセットする必要があります。
このようなカスタムクエリを使用している場合
$cat = new WP_query();
$cat->query("cat=19,20,-23&showposts=5&orderby=rand");
while ($cat->have_posts()) : $cat->the_post();
$data = get_post_meta( $post->ID, 'key', true );
$img_arrays []= $data['productimage'];
$lnk_arrays[] =get_permalink($post_ID);
endwhile;
wp_reset_query();
そうすれば、問題に遭遇することはありません。それ以外の場合、同じページに別のループがある場合、予期しない結果を得ることができます。上記のコードでwp_reset_query()を使用しませんでした(これはheader.phpファイルに配置されました。その後、single.phpに入ったとき、イライラする他のカテゴリの詳細ページを取得しました。上部でクエリをリセットするのを忘れました。すぐに、魅力のように機能し始めました。