Last active
November 8, 2019 07:49
-
-
Save david-zw-liu/382e5c21bf4e3f1d8455036eaf6227aa to your computer and use it in GitHub Desktop.
修改 Wordpress 標籤雲的排序(放到主題的 function.php 檔案最後面即可)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// 修改顯示標籤的個數 | |
$tags_number = 25; | |
// 這邊可以修改標籤的順序,數字越大的排在越前面 | |
$tag_order = [ | |
'2019電商新趨勢' => 101, | |
'OMO' => 100, | |
'新零售' => 99, | |
'線上線下整合' => 98, | |
'智能POS' => 97, | |
'智慧商店' => 96.5, | |
'電商金流' => 96, | |
'電商物流' => 95, | |
'電商行銷' => 94, | |
'IG行銷' => 93, | |
'網紅行銷' => 92, | |
'內容行銷' => 91, | |
'網紅行銷' => 90, | |
'口碑行銷' => 89, | |
'會員經營' => 88, | |
'分潤機制' => 87, | |
'定期定額' => 86, | |
'CVR' => 85, | |
'轉換率' => 84, | |
'流量' => 83, | |
'客單價' => 82, | |
'圖表分析' => 82, | |
]; | |
function custom_tag_sort($tags) { | |
global $tag_order; | |
global $tags_number; | |
$tags_sorted = array(); | |
foreach($tags as $tag) { | |
$tags_sorted[] = $tag; | |
} | |
usort($tags_sorted, function ($a, $b) use ($tag_order) { | |
$sa = $tag_order[$a->name] ?? 0; | |
$sb = $tag_order[$b->name] ?? 0; | |
// 優先用預先定義的權重排 | |
if ($sa != 0 || $sb != 0) { | |
return $sb <=> $sa; | |
} | |
// 剩餘的用關聯到的文章數排 | |
return $b->count <=> $a->count; | |
}); | |
return array_slice($tags_sorted, 0, $tags_number); | |
} | |
add_filter('tag_cloud_sort', 'custom_tag_sort'); | |
function set_widget_tag_cloud_args($args) { | |
return wp_parse_args($args, ['number' => 0]); | |
} | |
add_filter('widget_tag_cloud_args','set_widget_tag_cloud_args'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment