Wordpress:多个自定义帖子类型的存档页面存在分页问题



我是wordpress的新手,我正在尝试为多个自定义帖子类型创建一个存档页面。

我有两种自定义的帖子类型,"博览会"one_answers"展览"我目前有两个存档页面,带有预获取后操作:

add_action( 'pre_get_posts', function ( $q ) 
{
    if (    !is_admin() // VERY important, targets only front end queries
         && $q->is_main_query() // VERY important, targets only main query
         && $q->is_post_type_archive( 'fair' ) // Which post type archive page to target
    ) {
        $q->set( 'oderby', 'meta_value_num' );
        $q->set( 'meta_key', '_datepicker');
        $q->set('showposts', 3);
        $q->set('is_post_type_archive', false);
        $q->set('order', 'ASC');  
        // Rest of your arguments to set
}
});

两种自定义的帖子类型都有相同的元框,我想在存档页面上显示两种帖子类型"公平",或者在必要时显示在新的中

我试着添加

        $q->set('post_type', array('fair', 'exhibition'));

到函数。

当我这样做的时候,我有我的两个帖子类型,但我的自定义存档页面("archivefair.php")似乎不叫

如何通过功能分页正确执行两种帖子类型的存档页面?

谢谢并为我的英语感到抱歉,我希望这是可以理解的。

编辑:我的archive-post.php页面看起来像:

 <?php get_header(); 
 while ( have_posts() ) 
 {
   the_post();
    $p_type =     get_post_type(get_the_ID());
   if ($p_type == 'fair') 
       $img = get_post_meta(get_the_ID(), "wp_fair_attachment", true);
   else 
      $img = get_post_meta(get_the_ID(), "wp_exhibition_attachment", true); 
 ?> 
  some html
 <?php } ?>

另一种方法是使用template_include

试试这个代码,让我知道它是否有效:

add_filter( 'template_include', function( $template ){
    $your_types = array( 'fair', 'exhibition' );
    $post_type = get_post_type();
    if ( ! in_array( $post_type, $your_types ) )
        return $template;
    return get_stylesheet_directory() . '/single-fair.php'; 
});

最新更新