Wordpress-将index.php用于post和自定义post类型
我需要将index.php文件用于我的标准wordpress新闻,但也用于自定义帖子类型 在index.php文件中,我使用了一个没有参数的标准循环 我正试图让它与“pre_get_posts”的动作一起工作。我一直在尝试更改查询帖子类型,但仍然不起作用Wordpress-将index.php用于post和自定义post类型,wordpress,loops,post,Wordpress,Loops,Post,我需要将index.php文件用于我的标准wordpress新闻,但也用于自定义帖子类型 在index.php文件中,我使用了一个没有参数的标准循环 我正试图让它与“pre_get_posts”的动作一起工作。我一直在尝试更改查询帖子类型,但仍然不起作用 $query->set('post_type', 'any') 所以我需要一种方法让/dossiers/使用index.php,这意味着当在url中检测到/dossiers/时,我需要更改循环的查询 非常感谢 编辑: 下面是我用“pre
$query->set('post_type', 'any')
所以我需要一种方法让/dossiers/使用index.php,这意味着当在url中检测到/dossiers/时,我需要更改循环的查询
非常感谢
编辑:
下面是我用“pre_get_post”调用的函数:
下面是$query的var_转储:
object(WP_Query)#100 (42) {
["query"]=>
array(2) {
["page"]=>
string(0) ""
["name"]=>
string(8) "dossiers"
}
["query_vars"]=>
array(46) {
["page"]=>
string(0) ""
["name"]=>
string(8) "dossiers"
["error"]=>
string(0) ""
["m"]=>
int(0)
["p"]=>
int(0)
["post_parent"]=>
string(0) ""
["subpost"]=>
string(0) ""
["subpost_id"]=>
string(0) ""
["attachment"]=>
string(0) ""
["attachment_id"]=>
int(0)
["static"]=>
string(0) ""
["pagename"]=>
string(0) ""
["page_id"]=>
int(0)
["second"]=>
string(0) ""
["minute"]=>
string(0) ""
["hour"]=>
string(0) ""
["day"]=>
int(0)
["monthnum"]=>
int(0)
["year"]=>
int(0)
["w"]=>
int(0)
["category_name"]=>
string(0) ""
["tag"]=>
string(0) ""
["cat"]=>
string(0) ""
["tag_id"]=>
string(0) ""
["author_name"]=>
string(0) ""
["feed"]=>
string(0) ""
["tb"]=>
string(0) ""
["paged"]=>
int(0)
["comments_popup"]=>
string(0) ""
["meta_key"]=>
string(0) ""
["meta_value"]=>
string(0) ""
["preview"]=>
string(0) ""
["s"]=>
string(0) ""
["sentence"]=>
string(0) ""
["fields"]=>
string(0) ""
["category__in"]=>
array(0) {
}
["category__not_in"]=>
array(0) {
}
["category__and"]=>
array(0) {
}
["post__in"]=>
array(0) {
}
["post__not_in"]=>
array(0) {
}
["tag__in"]=>
array(0) {
}
["tag__not_in"]=>
array(0) {
}
["tag__and"]=>
array(0) {
}
["tag_slug__in"]=>
array(0) {
}
["tag_slug__and"]=>
array(0) {
}
["post_type"]=>
string(3) "any"
}
["tax_query"]=>
NULL
["meta_query"]=>
bool(false)
["post_count"]=>
int(0)
["current_post"]=>
int(-1)
["in_the_loop"]=>
bool(false)
["comment_count"]=>
int(0)
["current_comment"]=>
int(-1)
["found_posts"]=>
int(0)
["max_num_pages"]=>
int(0)
["max_num_comment_pages"]=>
int(0)
["is_single"]=>
bool(true)
["is_preview"]=>
bool(false)
["is_page"]=>
bool(false)
["is_archive"]=>
bool(false)
["is_date"]=>
bool(false)
["is_year"]=>
bool(false)
["is_month"]=>
bool(false)
["is_day"]=>
bool(false)
["is_time"]=>
bool(false)
["is_author"]=>
bool(false)
["is_category"]=>
bool(false)
["is_tag"]=>
bool(false)
["is_tax"]=>
bool(false)
["is_search"]=>
bool(false)
["is_feed"]=>
bool(false)
["is_comment_feed"]=>
bool(false)
["is_trackback"]=>
bool(false)
["is_home"]=>
bool(false)
["is_404"]=>
bool(false)
["is_comments_popup"]=>
bool(false)
["is_paged"]=>
bool(false)
["is_admin"]=>
bool(false)
["is_attachment"]=>
bool(false)
["is_singular"]=>
bool(true)
["is_robots"]=>
bool(false)
["is_posts_page"]=>
bool(false)
["is_post_type_archive"]=>
bool(false)
["query_vars_hash"]=>
string(32) "3cd80adcb57d626df6535f6dafb98057"
["query_vars_changed"]=>
bool(false)
["thumbnails_cached"]=>
bool(false)
}
“name”的值被设置为“dossiers”,这应该是一个帖子名的slug,如果我将其设置为“”,我将使用我的上一个自定义帖子类型重定向到single.php。我怀疑您的条件没有与
$query
对象正确交互
尝试:
如果这不起作用,也许这会起作用:
$query_var_name = get_query_var('name');
if( $query->is_main_query() && $query_var_name == 'dossiers' )
另外,添加一些测试以确保index.php实际上是Wordpress在访问/dossiers时使用的模板文件,为什么不能使用
single-{custom post type}.php
模板文件?wordpress层次结构的工作方式index.php将始终是最后一个被调用的模板。因为它将是索引的精确副本。我的条件正在工作,因为:[“post_type”]=>string(3)“any”这是因为Wordpress查询类足够聪明,可以将您的set
应用于相应的query\u var
。如果您想要“名称”,我很确定您必须从query\u vars
获取它。我的意思是。。。这个条件不是问题所在,如果我完全删除它,当我转到/dossiers/你将post\u type
查询变量设置为any
时,我仍然看不到我的自定义帖子类型,所以除非any
是你的自定义帖子类型,否则我想这就是原因。
if( $query->is_main_query() && $query->query_vars['name'] == 'dossiers' )
$query_var_name = get_query_var('name');
if( $query->is_main_query() && $query_var_name == 'dossiers' )