libs.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // 获取特定分类别名下的所有文章
  3. function get_posts_by_category_slug($category_slug) {
  4. $args = array(
  5. 'numberposts' => -1, // 获取所有文章
  6. 'post_type' => 'post', // 文章类型
  7. 'category_name' => $category_slug, // 分类别名
  8. 'orderby' => 'date', // 按日期排序
  9. 'order' => 'ASC', // 降序排序
  10. );
  11. $posts = get_posts($args);
  12. return $posts;
  13. }
  14. // 获取特定分类下所有子目录
  15. function get_subdirectories($parent_category_id) {
  16. $args = array(
  17. 'parent' => $parent_category_id,
  18. 'taxonomy' => 'category',
  19. 'hide_empty' => false,
  20. 'exclude' => array($parent_category_id), // 排除父分类本身
  21. 'orderby' => 'id', // 按日期排序
  22. 'order' => 'ASC', // 降序排序
  23. );
  24. $subcategories = get_terms( $args );
  25. return $subcategories;
  26. }
  27. // 返回并输出下拉菜单
  28. function get_submenus($flus, $parent_category_id) {
  29. $subdirs = get_subdirectories($parent_category_id);
  30. $li = '';
  31. if ( !empty( $subdirs ) && !is_wp_error( $subdirs ) ) {
  32. foreach ( $subdirs as $category ) {
  33. $li.= '<li><a href="/'.$flus.'?fid='.$category->term_id.'">'.$category->name.'</a></li>';
  34. }
  35. }
  36. return $li;
  37. }
  38. // 底部菜单导航
  39. function get_footer_submenus($flus, $parent_category_id) {
  40. $subdirs = get_subdirectories($parent_category_id);
  41. $li = '';
  42. if ( !empty( $subdirs ) && !is_wp_error( $subdirs ) ) {
  43. foreach ( $subdirs as $category ) {
  44. $li.= '<dd><a href="/'.$flus.'?fid='.$category->term_id.'">'.$category->name.'</a></dd>';
  45. }
  46. }
  47. return $li;
  48. }
  49. // 输出左侧栏菜单
  50. function get_slider_submenus($flus, $parent_category_id, $cur_id) {
  51. $subdirs = get_subdirectories($parent_category_id);
  52. $li = '';
  53. if ( !empty( $subdirs ) && !is_wp_error( $subdirs ) ) {
  54. foreach ( $subdirs as $category ) {
  55. $li.= '<li class="'.($cur_id == $category->term_id ? 'on' : '').'"><a href="/'.$flus.'?fid='.$category->term_id.'"><span>&gt;&gt;</span>'.$category->name.'</a></li>';
  56. }
  57. }
  58. return $li;
  59. }
  60. ?>