某些时候,我们需要获取WordPress 所有分类的名字和ID,比如,倡萌的主题设置界面,就使用了这个功能,如下图
实现的方法很简单,一起来操作吧 在主题的 functions.php 最后一个 ?> 前添加下面的代码
/**
* 获取WordPress所有分类名字和ID
* https://www.wpdaxue.com/show-wordpress-category.html
*/
function show_category(){
global $wpdb;
$request = "SELECT $wpdb->terms.term_id, name FROM $wpdb->terms ";
$request .= " LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ";
$request .= " WHERE $wpdb->term_taxonomy.taxonomy = 'category' ";
$request .= " ORDER BY term_id asc";
$categorys = $wpdb->get_results($request);
foreach ($categorys as $category) { //调用菜单
$output = '<span>'.$category->name."(<em>".$category->term_id.'</em>)</span>';
echo $output;
}
} |
/**
* 获取WordPress所有分类名字和ID
* https://www.wpdaxue.com/show-wordpress-category.html
*/
function show_category(){
global $wpdb;
$request = "SELECT $wpdb->terms.term_id, name FROM $wpdb->terms ";
$request .= " LEFT JOIN $wpdb->term_taxonomy ON $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id ";
$request .= " WHERE $wpdb->term_taxonomy.taxonomy = 'category' ";
$request .= " ORDER BY term_id asc";
$categorys = $wpdb->get_results($request);
foreach ($categorys as $category) { //调用菜单
$output = '<span>'.$category->name."(<em>".$category->term_id.'</em>)</span>';
echo $output;
}
}
在需要显示的地方添加下面的调用代码即可
<?php show_category(); ?> |
<?php show_category(); ?>
是不是很简单?要实现不同的样式,就自己写css吧!
来源:
https://www.wpdaxue.com/show-wordpress-category.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32337.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57