簡體   English   中英

wordpress標記檔案,使用自定義帖子類型排序

[英]wordpress tag archive, sort order with custom post type

我將這段代碼添加到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中傳遞了無效的參數”

這是“內爆”行。

我的目標是讓標簽存檔中的結果按帖子類型(而不是日期或標題)對列表進行排序。

$thequery->query_vars['post_type']將返回特定的post_type作為類似於post的字符串。 你不能內爆。

您應該只可以刪除有關嘗試使其內爆的部分:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM