如何更改/自定义帖子的类型和顺序?

时间:2019-05-26 06:43:12

标签: archive taxonomy add-filter

在我的分类档案中,我要显示几种类型的帖子:附件,肖像,书籍和帖子。是否可以用pre_get_post订购它们,还是应该为每个帖子创建一个新的(小型)查询?

我担心会用肮脏的女巫的查询破坏模板 taxonomy-artist.php 的“自然流”。但是我还需要根据帖子的类型更改布局。

  
      
  1. 在functions.php中,首先测试确定,但对顺序没有任何控制(默认情况下,我认为是在日期之前)
  2.   

    function add_CPT_to_archive_query() {
        global $wp_query;

        if (is_tax('artist')) { // Just for my template
        $wp_query->query_vars['post_type'] =  array( 'attachment', 'post', 'portrait', 'books' ); // the CPT I need to display
        $wp_query->query_vars['post_status'] =  array( null );
        return $wp_query;
        }
    }
    add_action('parse_query', 'add_CPT_to_archive_query');

  
      
  1. [直接在我的模板taxonomy-artist.php中]来自@VayuRobins(Query Custom Post Types & Order By Custom Post Type)的第二次测试:很好,但是它不再显示我的附件...
  2.   


    $my_post_types = array( 'portrait', 'attachment', 'book', 'post' );
    $posts_shown = array();
    $args = array(
    'post_type' => array( 'portrait', 'attachment', 'post', book ),
    'post_status' => 'any',
    'posts_per_page' => -1
    );
    $my_query = new WP_Query( $args );
    foreach ( $my_post_types as $post_type ):
    while ( $my_query->have_posts() ): $my_query->the_post();
    if ( $post_type == get_post_type() && ! in_array( get_the_ID(), $posts_shown ) ) {
    echo '
' . get_post_type() .': '. get_the_title() . '
'; $posts_shown[] = get_the_id(); break; } endwhile; $my_query->rewind_posts(); endforeach; wp_reset_postdata();
  
      
  1. 我尝试了最后一个想法(对不起,它应该显示为   filthy!...):在functions.php中创建多个具有更改优先级的add_filter。但是第二个add_filter似乎“覆盖”了第一个...
  2.   


    function add_portrait() {
    global $wp_query;

        if (is_tax('artiste') OR is_tax() OR is_tag() OR is_category()) {
                $wp_query->query_vars['post_type'] =  array( 'portrait', );
                $wp_query->query_vars['post_status'] =  array( null );
                return $wp_query;
            }
        }
        add_filter('parse_query', 'add_portrait');


    function add_attachment() {
      global $wp_query;

      if (is_tax('artiste')) {
          $wp_query->query_vars['post_type'] =  array( 'attachment' );
          $wp_query->query_vars['post_status'] =  array( null );
          return $wp_query;
      }
    }
    add_filter('parse_query', 'add_attachment');

感谢您帮助我理解错误并找到正确的方向!

0 个答案:

没有答案