Wordpress:带有分页的多个自定义帖子类型的归档页面问题

问题描述 投票:0回答:1

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

我有两个自定义帖子类型,'公平'和'展览'我目前有两个存档页面,预先获得后期操作:

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

到功能。

当我这样做时,我有我的两种帖子类型,但我的自定义存档页面(“archive-fair.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 } ?>
php wordpress custom-post-type archive
1个回答
0
投票

另一种方法是使用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'; 
});
© www.soinside.com 2019 - 2024. All rights reserved.