php - Automatically updating page list

admin2025-06-03  2

I'm building a WP website for a client that require location pages to be added on a frequent basis. I wanted to created a parent location page that would have the full list of locations that would automatically update each time a child page is added. Currently I'm using a custom menu, however I dont think this will automatically update each time a [location] page is added. Ideally, I would like to have an accordion on the parent page with the UK split into North East, North West etc, then under each accordion the list of the location pages that fall under that catergory. Does anyone know of a way to get these to automatically update?

Thanks

I'm building a WP website for a client that require location pages to be added on a frequent basis. I wanted to created a parent location page that would have the full list of locations that would automatically update each time a child page is added. Currently I'm using a custom menu, however I dont think this will automatically update each time a [location] page is added. Ideally, I would like to have an accordion on the parent page with the UK split into North East, North West etc, then under each accordion the list of the location pages that fall under that catergory. Does anyone know of a way to get these to automatically update?

Thanks

Share Improve this question edited Jan 29, 2019 at 12:07 Steve Kondiaynnis asked Jan 29, 2019 at 11:56 Steve KondiaynnisSteve Kondiaynnis 231 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You can use the function wp_list_pages. If the default output isn't enough you can write a custom walker for that function.

You can do this by adding a custom taxonomy to your [location] pages. You can then use this custom taxonomy to put your posts in the appropriate sections of the accordion.

$args = array(                                      // Array can be extended with additional arguments
   'tax_query' => array(
       array(
           'taxonomy' => 'your-location-taxonomy',  // Name of your custom taxonomy
           'field' => 'location',                   // Name of the field in your taxonomy
           'terms' => 'North East',                 // Value of the field in your taxonomy
       )
    )
);

// Execute your query with the given arguments.
$query = new WP_Query( $args );

// Check that we have query results.
if ( $query->have_posts() ) {

    // Start looping over the query results.
    while ( $query->have_posts() ) {

        $query->the_post();

        // Build your accordion elements here

    }

}

// Restore original post data.
wp_reset_postdata();

You could wrap above code in a loop through all potential values of the location taxonomy field.

More on Taxonomies and WP_Query can be found at the respective links

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748965807a315220.html

最新回复(0)