Link category to page, enable parent class in wp_list_pages (5 posts)

  1. a.raffa@bionatitalia.eu
    Posted 6 years ago #

    Hi everyone,
    I'd like to show/display posts of a category, as children of a page (or sub-page), maybe logically linked to that category.

    Here is an example:

    • Page 1
      • Sub-page a
      • Sub-page b
    • Page 2
      • Sub-page X
      • Sub-page Y
    • Page 3
      • Sub-page I
      • Sub-page II
    • Page 4
      • Sub-page i
      • Sub-page ii

    For example I need to link Sub-page II to a specific category named Category XYZ.
    Plus, while displaying a post (or a posts list) of Category XYZ, I need that wp_list_pages appends the current_page_ancestor and/or current_page_parent class to the Sub-page II list item.

    Thanks in advance,
    Alessandro Raffa

  2. MichaelH
    Posted 6 years ago #

    Start by considering the use of a plugin such as Page-Links To to 'link' that page with your category URL.

    Or code your Page Templates to display posts from the appropriate category.

  3. a.raffa@bionatitalia.eu
    Posted 6 years ago #

    Hi MichaelH,
    and thanks for your quick answer.

    I've already coded my page template, so when I visit Sub-page II I can see the_title() and the_content() of the page followed by a list of posts associated with Category XYZ. While visiting Sub-page II, wp_list_pages correctly appends the class current_page_ancestor to Page 3, and current_page_item to Sub-page II, and it's ok. But when I visit a post of Category XYZ, wp_list_pages (as in its nature) does not append classes.

    I think I have to:

    • create page-category pairs (maybe stored in the database or in a text file)
    • modify wp_list_pages, so when WordPress displays a post of a category, it will append the right class to the list of pages

    Also I tried these plugins:

    • Page Links To
    • Page Menu Editor
    • Page Lists Plus
    • Page Link Manager
    • Super Categories
    • Top Level Categories

    They are wonderful, but they can't help me :-)

    Thanks for your help.

  4. a.raffa@bionatitalia.eu
    Posted 6 years ago #

    I started writing a plugin just for this case.

    I'm adding a filter on wp_list_pages to:

    • create an array that links pages and categories
    • check if the current displayed content is a post
    • check if the current post has at least one category
    • retrieve the first cat_ID from the current post
    • extract each li element from wp_list_pages
    • for each li element of the list:

      • retrieve the page_ID
      • check if the current page_ID is linked with the cat_ID (this information is on the array)
      • if yes, replace the class attribute of the li element, adding current_page_parent in the right place
    • rebuild the wp_list_pages output with the replaced strings

    This is just a tweak/fix useful for me. Maybe it's not a good plugin idea. But it seems perfect for my current project. Just for the information architecture I choose.

    If someone needs a plugin like this, maybe I could start write a community plugin with an option page instead of the array inside the code ;-)

  5. MichaelH
    Posted 6 years ago #

    And submit the plugin to the repository...


Topic Closed

This topic has been closed to new replies.

About this Topic