Created
May 15, 2013 04:50
-
-
Save hissy/5581703 to your computer and use it in GitHub Desktop.
wp_reset_query と wp_reset_postdata のあれこれ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function query_posts($query) { | |
| $GLOBALS['wp_query'] = new WP_Query(); | |
| return $GLOBALS['wp_query']->query($query); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| グローバル変数 $wp_query を上書きするのが query_posts。 | |
| wp_reset_query() が何をしているかというと |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function wp_reset_query() { | |
| $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; | |
| wp_reset_postdata(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| グローバル変数 $wp_query を元に戻してから wp_reset_postdata() | |
| だから、query_postsを使っていない時は wp_reset_postdata() だけでOK | |
| 逆に、query_postsを使ったのに wp_reset_postdata() だけ使っちゃうと |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function wp_reset_postdata() { | |
| global $wp_query; | |
| if ( !empty($wp_query->post) ) { | |
| $GLOBALS['post'] = $wp_query->post; | |
| setup_postdata($wp_query->post); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| グローバル変数 $wp_query が上書きされたままこの処理をしてしまうので、おかしなことになります |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment