php – WordPress – pre_get_posts代替页面上的query_posts
我的情况有点复杂,我会尽可能简洁地解释.
我目前正在使用query_posts来修改我网站上的自定义页面上的主要查询,据我所知,尽管我已经阅读过,使用query_posts是不好的做法,原因有很多. 那么,为什么我使用query_posts而不是创建一个你可能会问的WP_Query对象? 这是因为我正在使用无限滚动插件,无限滚动不会很好用WP_query,但是当您使用query_posts修改主查询时,它的工作效果非常好.例如,使用无限卷动WP_query(主要关注)分页不起作用. 在一个页面上,我正在修改查询以获得大多数观看的帖子. <?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; ?> <?php query_posts( array( 'meta_key' => 'wpb_post_views_count','orderby' => 'meta_value_num','order' => 'DESC','paged' => $paged,) ); ?> <?php if (have_posts()) : ?> <?php while ( have_posts() ) : the_post() ?> <?php if ( has_post_format( 'video' )) { get_template_part( 'video-post' ); }elseif ( has_post_format( 'image' )) { get_template_part( 'image-post' ); } else { get_template_part( 'standard-post' ); } ?> <?php endwhile;?> <?php endif; ?> 所以经过很多阅读,我收集到我的其他选项修改主要查询是使用pre_get_posts,虽然我有点不确定如何去做这个. 以此为例: – function textdomain_exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat','-1,-2' ); } } add_action( 'pre_get_posts','textdomain_exclude_category' ); 好的,如此简单 – 如果是主页,修改主查询并排除两个类别. 我感到困惑,无法想像的是: >自定义页面模板的用例场景.使用我的query_posts修改我可以放在数组之前if(have_posts()),选择我的页面模板,发布它,我走了. 我如何用pre_get_posts做这件事,并确保它是分页的,即.与无限滚动一起工作?在所有使用pre_get_posts的示例中,没有数组.
如何使用pre_get_posts钩子通过自定义页面模板显示页面上的帖子列表?
我一直在玩pre_get_posts钩子,这里有一个想法 步骤1: 停止一个页面,例如用s子显示: example.com/show 第2步: 创建一个自定义页面模板: tpl_show.php 位于当前的主题目录. 步骤3: 我们构建以下pre_get_posts动作回调: function b2e_pre_get_posts( $query ) { $target_page = 'show'; // EDIT to your needs if ( ! is_admin() // front-end only && $query->is_main_query() // main query only && $target_page === $query->get( 'pagename' ) // matching pagename only ) { // modify query_vars: $query->set( 'post_type','post' ); // override 'post_type' $query->set( 'pagename',null ); // override 'pagename' $query->set( 'posts_per_page',10 ); $query->set( 'meta_key','wpb_post_views_count' ); $query->set( 'orderby','meta_value_num' ); $query->set( 'order','DESC' ); // Support for paging $query->is_singular = 0; // custom page template add_filter( 'template_include','b2e_template_include',99 ); } } add_action( 'pre_get_posts','b2e_pre_get_posts' ); 哪里 function b2e_template_include( $template ) { $target_tpl = 'tpl_show.php'; // EDIT to your needs remove_filter( 'template_include',99 ); $new_template = locate_template( array( $target_tpl ) ); if ( ! empty( $new_template ) ) $template = $new_template; ; return $template; } 这也应该给我们分页: example.com/show/page/2 example.com/show/page/3 等等 笔记 我更新了答案,并根据@PieterGoosen的建议删除了查询对象部分修改,因为它可以例如打破他的设置中的面包屑. 还删除了pre_get_posts钩子中的is_page()检查,因为在某些情况下它可能仍然会出现不规则.原因是查询对象并不总是可用.正在进行中. #27015.如果要使用is_page()或is_front_page(),则有workarounds possible. 我构建了下表,只是为了更好地概述主WP_Query对象的一些属性和查询变量,对于给定的块: 这是interesting to note,WP_Query中的分页依赖于nopaging未设置,当前页面不是单数(从4.4 source): // Paging if ( empty($q['nopaging']) && !$this->is_singular ) { $page = absint($q['paged']); if ( !$page ) $page = 1; // If 'offset' is provided,it takes precedence over 'paged'. if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) { $q['offset'] = absint( $q['offset'] ); $pgstrt = $q['offset'] . ','; } else { $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ','; } $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; } 我们可以看到生成的SQL查询的LIMIT部分在条件检查中.这就解释了为什么我们修改上面的is_singular属性. 我们可以使用其他过滤器/钩子,但是在这里我们使用了OP提到的pre_get_posts. 希望这个帮助. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |