$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom taxonomy - List all taxonomies with their descriptions|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom taxonomy - List all taxonomies with their descriptions

matteradmin6PV0评论

I have this snippet below that lists all my categories in linked hierarchical order and their descriptions. I was hoping someone could update it to allow for a specific custom taxonomy instead?

function list_cats_with_desc() {

  $base = wp_list_categories('echo=0&hide_empty=0&title_li=0&orderby=ID&order=ASC');

  // wp_list_categories adds a "cat-item-[category_id]" class to the <li> so let's make use of that!
  // Shouldn't really use regexp to parse HTML, but oh well.
  // (for the curious, here's why:  )

  $get_cat_id = '/cat-item-[0-9]+/';

  preg_match_all($get_cat_id, $base, $cat_id);

  // Let's prepare our category descriptions to be injected.
  // Format will be <a>category-name<span>category-desc</span></a>

  $inject_desc = array();
  $i = 0;
  foreach($cat_id[0] as $id) {
    $id = trim($id,'cat-item-');
    $id = trim($id,'"');
    $desc = trim(strip_tags(category_description($id)),"\n");   // For some reason, category_description returns the
                                                                // description wrapped in an unwanted paragraph tag which
                                                                // we remove with strip_tags. It also adds a newline
                                                                // which we promptly trim out.
    if($desc=="") $desc = " &bull; --- ";
    $inject_desc[$i] = '</a> <span class="cat-desc">' . $desc . '</span>';
    $i++;
  }
  // Now we inject our descriptions
  $base_arr = explode("\n", $base);
  $base_i = 0;
  foreach($inject_desc as $desc) {
    // We check whether there's an occurence of "</a>"
    while(strpos($base_arr[$base_i], "</a>") === false) {
      $base_i++;
    }
    // If we find one, inject our description <span>
    $base_arr[$base_i] = str_replace("</a>", $desc, $base_arr[$base_i]);
    $base_i++;
  }
  $base = implode("\n", $base_arr);
  echo $base;
}

I have this snippet below that lists all my categories in linked hierarchical order and their descriptions. I was hoping someone could update it to allow for a specific custom taxonomy instead?

function list_cats_with_desc() {

  $base = wp_list_categories('echo=0&hide_empty=0&title_li=0&orderby=ID&order=ASC');

  // wp_list_categories adds a "cat-item-[category_id]" class to the <li> so let's make use of that!
  // Shouldn't really use regexp to parse HTML, but oh well.
  // (for the curious, here's why: http://stackoverflow/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 )

  $get_cat_id = '/cat-item-[0-9]+/';

  preg_match_all($get_cat_id, $base, $cat_id);

  // Let's prepare our category descriptions to be injected.
  // Format will be <a>category-name<span>category-desc</span></a>

  $inject_desc = array();
  $i = 0;
  foreach($cat_id[0] as $id) {
    $id = trim($id,'cat-item-');
    $id = trim($id,'"');
    $desc = trim(strip_tags(category_description($id)),"\n");   // For some reason, category_description returns the
                                                                // description wrapped in an unwanted paragraph tag which
                                                                // we remove with strip_tags. It also adds a newline
                                                                // which we promptly trim out.
    if($desc=="") $desc = " &bull; --- ";
    $inject_desc[$i] = '</a> <span class="cat-desc">' . $desc . '</span>';
    $i++;
  }
  // Now we inject our descriptions
  $base_arr = explode("\n", $base);
  $base_i = 0;
  foreach($inject_desc as $desc) {
    // We check whether there's an occurence of "</a>"
    while(strpos($base_arr[$base_i], "</a>") === false) {
      $base_i++;
    }
    // If we find one, inject our description <span>
    $base_arr[$base_i] = str_replace("</a>", $desc, $base_arr[$base_i]);
    $base_i++;
  }
  $base = implode("\n", $base_arr);
  echo $base;
}
Share Improve this question asked Nov 24, 2018 at 19:39 PetePete 1,0582 gold badges14 silver badges40 bronze badges 5
  • why wp query was not used? – user145078 Commented Nov 24, 2018 at 20:09
  • No idea, someone gave it to me like this. – Pete Commented Nov 24, 2018 at 20:32
  • 1 So you want list of custom taxonomy with their description. am i correct? what kind of post your custom taxonomy is linked to? – user145078 Commented Nov 24, 2018 at 20:52
  • 1 developer.wordpress/reference/functions/wp_list_categories you can pass the custom taxonomy as $arg to wp_list_categories() – user145078 Commented Nov 24, 2018 at 20:56
  • The custom taxonomy is linked to a post – Pete Commented Nov 25, 2018 at 7:46
Add a comment  | 

1 Answer 1

Reset to default 0

This worked...

function list_cats_with_desc() {

  $base = wp_list_categories('echo=0&hide_empty=0&title_li=0&orderby=ID&order=ASC&taxonomy=CUSTOM-TAXONOMY-SLUG');

  // wp_list_categories adds a "cat-item-[category_id]" class to the <li> so let's make use of that!
  // Shouldn't really use regexp to parse HTML, but oh well.
  // (for the curious, here's why: http://stackoverflow/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 )

  $get_cat_id = '/cat-item-[0-9]+/';

  preg_match_all($get_cat_id, $base, $cat_id);

  // Let's prepare our category descriptions to be injected.
  // Format will be <a>category-name<span>category-desc</span></a>

  $inject_desc = array();
  $i = 0;
  foreach($cat_id[0] as $id) {
    $id = trim($id,'cat-item-');
    $id = trim($id,'"');
    $desc = trim(strip_tags(category_description($id)),"\n");   // For some reason, category_description returns the
                                                                // description wrapped in an unwanted paragraph tag which
                                                                // we remove with strip_tags. It also adds a newline
                                                                // which we promptly trim out.
    if($desc=="") $desc = " &bull; --- ";
    $inject_desc[$i] = '</a> <span class="cat-desc">' . $desc . '</span>';
    $i++;
  }
  // Now we inject our descriptions
  $base_arr = explode("\n", $base);
  $base_i = 0;
  foreach($inject_desc as $desc) {
    // We check whether there's an occurence of "</a>"
    while(strpos($base_arr[$base_i], "</a>") === false) {
      $base_i++;
    }
    // If we find one, inject our description <span>
    $base_arr[$base_i] = str_replace("</a>", $desc, $base_arr[$base_i]);
    $base_i++;
  }
  $base = implode("\n", $base_arr);
  echo $base;
}
Post a comment

comment list (0)

  1. No comments so far