Advertisement
Advertisement

More Related Content

Advertisement

Recently uploaded(20)

Секреты WP_Query

  1. WP_Query
  2. is_home() is_single() is_page()
  3. query_posts()
  4. if ( have_posts() ) while ( have_posts() ) the_post();
  5. query_posts( 'cat=-5' ); $posts = get_posts( 'cat=-5' ); $posts = new WP_Query( 'cat=-5' );
  6. 10 posts
  7. 10 posts + 10 posts cat = -5
  8. max_num_pages = 3 index.php 404.php max_num_pages = 5
  9. pre_get_posts
  10. add_action( 'pre_get_posts', 'my_func' ); function my_func( $query ) { $query->set( 'cat', '-5' ); }
  11. $query->is_main_query()
  12. add_action( 'pre_get_posts', 'my_func' ); function my_func( $query ) { if ( $query->is_main_query() ) $query->set( 'cat', '-5' ); }
  13. add_action( 'pre_get_posts', 'my_func' ); function my_func( $query ) { if ( $query->is_main_query() ) if ( $query->is_search() ) $query->set( 'post_type', 'post' ); }
  14. add_action( 'pre_get_posts', 'my_func' ); function my_func( $query ) { if ( $query->is_main_query() ) if ( $query->is_search() ) $query->set( 'posts_per_page', 30 ); }
  15. add_action( 'pre_get_posts', 'my_func' ); function my_func( $query ) { if ( $query->is_main_query() ) if ( $query->is_home() ) $query->set( 'post_type', array( 'post', 'book', ) ); }
  16. query_posts(); $posts = get_posts(); $posts = new WP_Query();
  17. $popular = new WP_Query( 'cat=3' ); while ( $popular->have_posts() ) { $popular->the_post(); ... }
  18. $popular = new WP_Query( 'cat=3' ); while ( $popular->have_posts() ) { $popular->the_post(); ... } // Основной запрос while ( have_posts() ) { the_post(); ... }
  19. // Основной запрос while ( have_posts() ) { the_post(); ... $category = get_the_category(); $related = new WP_Query( 'cat=' ... ); while ( $related->have_posts() ) { $related->the_post(); ... } }
  20. query_posts()
  21. $wp_query
  22. function have_posts() { global $wp_query; return $wp_query->have_posts(); }
  23. $wp_query =& $wp_the_query;
  24. function &query_posts( $query ) { ... unset( $wp_query ); $wp_query = new WP_Query(); return $wp_query->query( $query ); }
  25. function wp_reset_query() { ... unset( $wp_query ); $wp_query =& $wp_the_query; }
  26. $popular = new WP_Query( 'cat=3' ); while ( $popular->have_posts() ) { $popular->the_post(); ... } // Основной запрос while ( have_posts() ) { the_post(); ... }
  27. query_posts( 'cat=3' ); while ( have_posts() ) { the_post(); ... } wp_reset_query(); // Основной запрос while ( have_posts() ) { the_post(); ... }
  28. Если нужно изменить основной запрос pre_get_posts
  29. Если нужен вторичный запрос new WP_Query; get_posts()
  30. Если нужна головная боль query_posts()
Advertisement