wordpress自定义帖子类型存档页面问题

时间:2015-11-17 10:46:13

标签: php wordpress

我知道有很多帖子和解决方案。但我的问题并没有解决这些问题。请在这里查看一次。

我有5种不同的自定义帖子类型。所以我在我的function.php文件中使用这些代码在archive.php页面中显示类别&的自定义帖子类型。标签

// Add custom post types to archives
function custom_post_archive($query) {
    if ($query->is_archive)
        $query->set( 'post_type', array('nav_menu_item', 'post', 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5') );
    remove_action( 'pre_get_posts', 'custom_post_archive' );
}
add_action('pre_get_posts', 'custom_post_archive');

// Add custom post types to tag
function custom_post_tag($query) {
    if ($query->is_tag)
        $query->set( 'post_type', array('nav_menu_item', 'post', 'post_type_1', 'post_type_2', 'post_type_3', 'post_type_4', 'post_type_5') );
    remove_action( 'pre_get_posts', 'custom_post_tag' );
}
add_action('pre_get_posts', 'custom_post_tag');

使用此功能后,我的存档页面会显示类别帖子&贴标签完美。没有其他问题..但是当我打开一些帖子类型档案时..例如:

http://exmple.com/post_type_1/http://exmple.com/post_type_3/

全部显示所有帖子类型的所有帖子!!!

同样在我的仪表板中,帖子类型很乱,并显示所有帖子类型的所有帖子显示在帖子和&其他5个帖子类型。

所以我想要一个解决方案或建议来解决这个问题。请帮忙。

1 个答案:

答案 0 :(得分:1)

抱歉,谢谢。我通过编写另一个代码修复了问题。

以下是代码:

function my_post_types( $query ) {
    if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        //$post_types = get_post_types('remote_exploit', 'webapp_exploit', 'localpriesc_exploit', 'pocnddos', 'shellcode_exploit', 'securiy_papers', 'cheat_sheet' ); 
       // $query->set( 'post_type', $post_types);
       $query->set( 'post_type', array('nav_menu_item', 'post', 'remote_exploit', 'webapp_exploit', 'localpriesc_exploit', 'pocnddos', 'shellcode_exploit', 'securiy_papers', 'cheat_sheet') );
        return $query;
    }
}
add_filter( 'pre_get_posts', 'my_post_types');

现在一切正常。

非常感谢。

相关问题