如何使用自定义帖子类型存档作为首页?
-
-
is_front_page()不适用于pre_get_postsis_front_page() will not work with pre_get_posts
- 0
- 2014-08-18
- Brad Dalton
-
5 个回答
- 投票数
-
- 2011-10-12
在将静态页面设置为主页之后,可以将其添加到
functions.php
中,您可以使用.这也会正确调用archive-POSTTYPE.php
模板.add_action("pre_get_posts", "custom_front_page"); function custom_front_page($wp_query){ //Ensure this filter isn't applied to the admin area if(is_admin()) { return; } if($wp_query->get('page_id') == get_option('page_on_front')): $wp_query->set('post_type', 'CUSTOM POST TYPE NAME HERE'); $wp_query->set('page_id', ''); //Empty //Set properties that describe the page to reflect that //we aren't really displaying a static page $wp_query->is_page = 0; $wp_query->is_singular = 0; $wp_query->is_post_type_archive = 1; $wp_query->is_archive = 1; endif; }
After you have set a static page as your home page you can add this to your
functions.php
and you are good to go. This will call thearchive-POSTTYPE.php
template correctly as well.add_action("pre_get_posts", "custom_front_page"); function custom_front_page($wp_query){ //Ensure this filter isn't applied to the admin area if(is_admin()) { return; } if($wp_query->get('page_id') == get_option('page_on_front')): $wp_query->set('post_type', 'CUSTOM POST TYPE NAME HERE'); $wp_query->set('page_id', ''); //Empty //Set properties that describe the page to reflect that //we aren't really displaying a static page $wp_query->is_page = 0; $wp_query->is_singular = 0; $wp_query->is_post_type_archive = 1; $wp_query->is_archive = 1; endif; }
-
这个函数在开始时需要`if(is_admin())return;`,否则会与管理区域混淆.This function needs `if(is_admin()) return;` at the very beginning, otherwise it messes with the admin area.
- 0
- 2013-09-11
- brasofilo
-
虽然这对我有用,但我的主要和次要菜单因此消失了.While this worked for me, my primary and secondary menus disappeared as result.
- 1
- 2015-04-19
- super9
-
几乎是正确的.这段代码更改了所有wp_queries,因此应该是if(is_home())而不是if($ wp_query->get .....)It's almost correctly. This code is changing all wp_queries, so it should be if ( is_home() ) instead of if ($wp_query->get.....)
- 0
- 2015-06-10
- Leo Caseiro
-
我使用的是相同的东西,但是在我的自定义页面模板而不是首页上,它没有显示任何结果(好像未添加任何自定义帖子).有什么想法吗?I'm using the same but on my custom page template instead of frontpage, and it shows no results (as if no custom posts were added). Any thoughts?
- 0
- 2018-07-22
- trainoasis
-
此解决方案不支持分页.任何/page/2网址仍显示前10个帖子.This solution doesn't support paging. Any /page/2 URL still shows the first 10 posts.
- 0
- 2019-07-19
- rg89
-
支持分页: 如果($ query->get('paged')){$paged=$ query->get('paged');} elseif($ query->get('page')){$paged=$ query->get('page');} 否则{$paged=1;} $ query-> set('paged',$paged);To support pagination: if ( $query->get('paged') ) { $paged = $query->get('paged'); } elseif ( $query->get('page') ) { $paged = $query->get('page'); } else { $paged = 1; } $query->set('paged', $paged);
- 1
- 2019-09-26
- Jonathan Nicol
-
- 2014-08-18
将CPT存档重命名为home.php
然后使用pre_get_posts更改主页查询,以便仅显示CPT
function wpsites_home_page_cpt_filter($query) { if ( !is_admin() && $query->is_main_query() && is_home() ) { $query->set('post_type', array( 'your-cpt' ) ); } } add_action('pre_get_posts','wpsites_home_page_cpt_filter');
用自定义帖子类型的名称替换您的cpt.
Re-name your CPT archive to home.php
Then use pre_get_posts to alter the home page query so only CPT's display
function wpsites_home_page_cpt_filter($query) { if ( !is_admin() && $query->is_main_query() && is_home() ) { $query->set('post_type', array( 'your-cpt' ) ); } } add_action('pre_get_posts','wpsites_home_page_cpt_filter');
Replace your-cpt with the name of your custom post type.
-
最后,给出一个清晰,可行的解释!finally, a clear, workable explanation!
- 2
- 2015-06-13
- Jack
-
- 2013-07-18
感谢答案ljaas-我一直在寻找解决这个确切问题的方法.为了调用自定义帖子类型存档模板,我必须添加以下条件:
$wp_query->is_post_type_archive = 1; $wp_query->is_archive = 1;
Thanks for the answer ljaas—I was looking to solve this exact problem. In order to get the custom post type archive template to be called I had to add the following conditions:
$wp_query->is_post_type_archive = 1; $wp_query->is_archive = 1;
-
您好Eli,欢迎来到WPSE."答案"旨在回答最初的问题(stackexchange网站为*非主题讨论论坛*).这对于* comment *非常适合.Hi Eli, welcome to WPSE. "Answers" are meant to answer the initial question (stackexchange sites are *not threaded discussion forums*). This would be a much better fit for a *comment*.
- 2
- 2013-07-18
- Johannes Pille
-
感谢约翰内斯的澄清.这就是我的想法,尽管由于没有可用的"添加评论"功能,我无法弄清楚如何对答案发表评论.这是对时间敏感的功能,还是我盲目?Thanks for the clarification Johannes. That is what I thought, though I could not figure out how to comment on the answer as there is no 'add comment' feature available. Is this a time-sensitive feature, or am I blind?
- 0
- 2013-07-20
- Eli
-
- 2015-03-26
这对我最好覆盖"设置">"阅读">"首页显示"中的博客文章和静态页面:
<?php /** * Set custom post type archive as front page. * * @since 1.0.0 */ function ql_set_as_front_page( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( ql_is_front_page( $query ) ) { $query->set( 'page_id', '' ); $query->is_page = false; $query->is_singular = false; $query->set( 'post_type', 'MYCPT' ); $query->is_archive = true; $query->is_post_type_archive = true; } } add_action( 'pre_get_posts', 'ql_set_as_front_page' ); /** * Taken from WP_Query::is_front_page and adapted to compare page_on_front with current page ID. * * @since 1.0.0 * * @param object $query The main WP Query. */ function ql_is_front_page( $query ) { if ( 'posts' == get_option( 'show_on_front') && $query->is_home() ) return true; elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $query->get('page_id') == get_option( 'page_on_front' ) ) return true; else return false; }
我将其与使用过滤器
front_page_template
和home_template
的模板覆盖一起使用,以返回自定义模板.This works better for me overriding both blog posts and static page in Settings > Reading > Front page displays:
<?php /** * Set custom post type archive as front page. * * @since 1.0.0 */ function ql_set_as_front_page( $query ) { if ( is_admin() || ! $query->is_main_query() ) { return; } if ( ql_is_front_page( $query ) ) { $query->set( 'page_id', '' ); $query->is_page = false; $query->is_singular = false; $query->set( 'post_type', 'MYCPT' ); $query->is_archive = true; $query->is_post_type_archive = true; } } add_action( 'pre_get_posts', 'ql_set_as_front_page' ); /** * Taken from WP_Query::is_front_page and adapted to compare page_on_front with current page ID. * * @since 1.0.0 * * @param object $query The main WP Query. */ function ql_is_front_page( $query ) { if ( 'posts' == get_option( 'show_on_front') && $query->is_home() ) return true; elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $query->get('page_id') == get_option( 'page_on_front' ) ) return true; else return false; }
I'm using it in conjunction with a template override using the filters
front_page_template
andhome_template
to return a custom template. -
- 2015-09-08
对我来说,它破坏了分页:您选择索引或静态页面作为主页,分页链接就会显示出来,但是当单击第2页时,我会得到:
- 如果是索引页(默认):404页
- 在静态页面的情况下:与页面1的结果相同:然后解释"paged"参数以显示页面类型的分页,而不是帖子类型列表的分页.
我认为它需要一些重写规则才能捕获分页的参数并正确传递.
无论如何,自定义模板页面应该是带有一些其他重写规则的解决方案.
For me it breaks the pagination : either you select the index or a static page as the home page, the pagination links shows up but when clicking on page 2 I get :
- in case of index page (default) : the 404 page
- in case of static page : the same results as page 1 : the "paged" argument is then interpreted to show the page type pagination, not the post type list pagination.
I think it needs some rewrite rules to catch the paged argument and pass it correctly.
Anyway, a custom template page should be the solution with some additional rewrite rules.
我想使用自定义帖子类型存档作为网站的首页,以便
是根据我的
archive-{post-type}.php
文件显示的自定义帖子类型档案.理想情况下,我想使用
functions.php
文件中的functions.php
更改查询.我尝试了以下操作,并以首页为首页:但是首页返回的是"主页"的内容,并且似乎忽略了自定义查询.
我在做什么错?一般来说,有没有更好的方法来解决这个问题?