• 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:

    <?php wp_list_pages( ‘post_status=publish&post_type=board-meetings&title_li=’ ); ?>

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    That function returns the result as an HTML list, so no, not really. However, you can hook a filter to its output, so you can look up the page, use wp_get_post_categories to get the categories, and rebuild the HTML.

    https://developer.www.ads-software.com/reference/hooks/wp_list_pages/

    It might be more efficient to use get_pages() to get the pages in the first place https://codex.www.ads-software.com/Function_Reference/get_pages

    Hi, I’m also working on something like this. Would anyone be able to give an example of how the hooks would work? I’m having trouble figuring it out.

    Moderator bcworkz

    (@bcworkz)

    The wp_list_pages filter is fine for minor alterations to the output, but for major changes requiring getting categories or what not, you are better off using a custom walker class that does the heavy lifting for every page in turn. You would extend Walker_Page class and override the various methods as required. Walkers are a more advanced coding concept, but if you can make sense of them, they can save a lot of work.

    The alternative like Steve says is to simply get_pages() and loop through the results to generate the output you desire. This approach can be strictly procedural and an easier concept to grasp. The drawback is dealing with child pages can get pretty involved if you want a hierarchical list. I’d consider diving into walkers if that’s the case.

    Neither of these involve using filters. You need to be able to alter the calling code to implement these.

    Hi @bcworkz,

    I looked at using get_pages() before, but the problem is that the list needs to be hierarchal. I’ve included a CodePen below of how the menu structure is generated:

    https://codepen.io/anon/pen/XGzLyd

    The main thing I want to do is display an icon based on the page category, this would be most easily produced by adding a class based on the pages category. Would there be a way to do this through using walker_page?

    Moderator bcworkz

    (@bcworkz)

    Sure there is. Walker_Page is at the heart of how wp_list_pages() generates each link, so changing the walker class can change each page link’s content.

    Extend Walker_Page and copy over the method that generates the link itself. Alter as required so the proper class gets into the link for each page. Reference your extended class when calling wp_list_pages().

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Is there a way to fetch the category of a page when using wp_list_pages?’ is closed to new replies.