1

我将此代码添加到 funtions.php

function tc_tag_for_cpt($query) {
  if(is_tag() && empty( $query->query_vars['suppress_filters'] ) && !is_admin()) {
    $post_type = get_query_var('post_type');
    $post_type = ($post_type) ?  $post_type : array('resources','post','page');     //,'nav_menu_item'
    $query->set('post_type',$post_type);
    return $query;
  }
}
add_filter( 'posts_orderby', 'sort_query_by_post_type', 10, 2 );
function sort_query_by_post_type( $sortby, $thequery ) {
    if(is_tag() && empty( $query->query_vars['suppress_filters'] ) && !is_admin()) {
        $thequery->set('orderby','post_type');
        if ( !empty($thequery->query_vars['post_type']) && isset($thequery->query_vars['orderby']) && $thequery->query_vars['orderby'] == 'post_type' )
            $sortby = "find_in_set(post_type, '" . implode( ',', $thequery->query_vars['post_type'] ) . "')";
    }
    return $sortby;
}

它应该在按帖子类型排序的标签存档上显示结果(资源是自定义帖子类型)。它会这样做,但屏幕顶部会出现一条错误消息:

“警告:implode() [function.implode]:第 279 行 /home/csleh3/public_html/sandbox/wp-content/themes/aodmarketing/functions.php 中传递的参数无效”

这是“内爆”线。

我的目标是让标签存档上的结果按帖子类型而不是日期或标题对列表进行排序。

4

1 回答 1

0

$thequery->query_vars['post_type']将返回特定的 post_type 作为字符串,如post. 你不能把它内爆。

您应该能够删除有关尝试内爆它的部分:

$sortby = "find_in_set(post_type, '" . $thequery->query_vars['post_type'] . "')";
于 2012-09-14T17:59:32.470 回答