【问题标题】:wordpress tag archive, sort order with custom post typewordpress 标签存档,使用自定义帖子类型排序
【发布时间】:2012-09-07 20:35:25
【问题描述】:

我将此代码添加到 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 中传递的参数无效”

这是“内爆”线。

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

【问题讨论】:

  • implode 期望一个数组作为它的参数之一。我怀疑你传递了两个字符串。

标签: php wordpress sorting filter


【解决方案1】:

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

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

$sortby = "find_in_set(post_type, '" . $thequery->query_vars['post_type'] . "')";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多