【问题标题】:Wordpress : issue with archive page for multiple custom post type with paginationWordpress:带有分页的多个自定义帖子类型的存档页面问题
【发布时间】:2016-08-27 05:34:00
【问题描述】:

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

我有两种自定义帖子类型,“公平”和“展览” 我目前有两个存档页面,带有 pre get post 操作:

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】:

    另一种方法是使用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'; 
    });
    

    【讨论】:

    • 我不使用 template_include,我有一个页面“archive-fair”,我在里面执行了一个循环。我可以做这样的事情吗?
    • 你正在做的事情也很有可能,用你正在使用的 post_type 设置显示完整的代码
    • 您需要哪部分代码?我只有自定义帖子类型的创建,第一条消息中更改请求的代码以及 archive-fair.php (... 代码 html)
    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2012-11-29
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多