【问题标题】:Custom post type archive in sidebar widget of post帖子侧边栏小部件中的自定义帖子类型存档
【发布时间】:2014-10-30 15:27:47
【问题描述】:

这很好用。我在下拉列表中看到自定义帖子类型存档,但是当我选择任何月份时,我会在该存档页面上看到所有自定义帖子。虽然每个月只有一个帖子,例如我在 7 月有 1 个帖子,在 8 月有 1 个帖子,下拉计数器显示正确,但是当我选择它们时,我会看到所有自定义帖子,而不仅仅是一个。有什么解决办法吗?

   add_filter( 'getarchives_where', 'custom_getarchives_where' );
    function custom_getarchives_where( $where ){
        $where = str_replace( "post_type = 'post'", "post_type IN ( 'post', 'fruits' )", $where );
        return $where;
    }

    add_action( 'pre_get_posts', 'add_podcasts_to_archive' );
    function add_podcasts_to_archive( $query ) {
        if ( $query->is_main_query() && $query->is_archive() && !is_admin() )
            $query->set( 'post_type', array( 'post', 'fruits' ) );
        return $query;
    }

【问题讨论】:

  • 我怀疑您的问题是存档页面本身的代码,您是否在存档页面上运行自定义查询

标签: wordpress archive custom-post-type


【解决方案1】:

您问题中的代码有效。我真的怀疑您的问题在于您的archive.php 模板。

这是我的想法

  • 您已将主查询替换为自定义查询。任何存档页面上的不正确自定义查询都是灾难性的。鉴于您从自定义帖子类型获得所有帖子的原因,这是我能想到的唯一原因

我的解决方案是,删除您的自定义查询,将其替换为默认循环,如果您需要更多自定义,请将其添加为 pre_get_posts

编辑

供您将来阅读的额外阅读

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    • 2017-11-21
    相关资源
    最近更新 更多