Wordpress - 使用index.php发布帖子和自定义帖子类型

时间:2012-11-21 14:42:31

标签: wordpress loops post

我需要将index.php文件用于我的标准wordpress新闻,还要用于自定义帖子类型。

在我的index.php文件中,我使用没有参数或参数的标准循环。

我正在尝试使用“pre_get_posts”操作。我一直在尝试更改查询帖子类型,但仍然无效。

$query->set('post_type', 'any')

所以我需要一种/ dossiers /使用index.php的方法,这意味着我需要在url中检测到/ dossiers /时更改循环查询。

非常感谢!

编辑:

这是我用“pre_get_post”调用的函数:

function custom_post_types( $query ) { 
    if( $query->is_main_query() && $query->query['name'] == 'dossiers' ) {
        $query->set('post_type', 'any');

        var_dump($query);
    }   
}

这是$ query的var_dump:

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。

1 个答案:

答案 0 :(得分:0)

我怀疑你的条件没有与$query对象正确交互。

尝试:

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' )

另外,添加某种测试以确保index.php实际上是Wordpress在访问/档案时使用的模板文件

相关问题