$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'); ?>categories - Is there a way to fetch the category of a page when using wp_list_pages?|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)

categories - Is there a way to fetch the category of a page when using wp_list_pages?

matteradmin11PV0评论

Currently we’re using wp_list_pages to show a list of child pages. However, we’d like to get the category of each page, and display some CSS on the front-end based on what category each page is in. This is the query we’re currently using:

wp_list_pages( 'post_status=publish&post_type=board-meetings&title_li=' );

Is there a way of of getting the category of each page?

Currently we’re using wp_list_pages to show a list of child pages. However, we’d like to get the category of each page, and display some CSS on the front-end based on what category each page is in. This is the query we’re currently using:

wp_list_pages( 'post_status=publish&post_type=board-meetings&title_li=' );

Is there a way of of getting the category of each page?

Share Improve this question edited Mar 12, 2019 at 14:51 Howdy_McGee 20.9k24 gold badges91 silver badges177 bronze badges asked Mar 12, 2019 at 11:33 June CooperJune Cooper 12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can get an array of pages using get_pages() and then for each page retrieve categories using wp_get_object_terms. Your code may look like this:

$args = array(
   'post_type' => 'board-meeting',
   'post_status' => 'publish'
 ); 

$pages = get_pages($args); 
if ( ! empty( $pages ) ) {
    echo '<ul>';
    foreach($pages as $page){
          $cats = wp_get_object_terms( $page->ID,  'category' ); // Array of categories
          $cat = '';
          if ( ! empty( $cats ) ) {
              if ( ! is_wp_error( $cats ) ) {
                  $cat = $cats[0]->slug; // slug of 1st category in the array $cats
              }
               // slug of category used as CSS class name for anchor tag
               echo '<li><a class='. $cat.' href="' . get_permalink($page-ID) . '">' . esc_html( $page->post_title ) . '</a></li>'; 
           }
    }
    echo '</ul>';
}

You may adjust the code to your needs.

I hope this helps.

Post a comment

comment list (0)

  1. No comments so far