• Hi
    Thanks for the great plugin!
    Will there be an option for displaying a list of child pages of a specific parent page?
    Or – automatically display sibling pages of the current page or child pages if it’s a parent page.

    Thanks!
    Dan

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi,

    I have made some customisations to the Plugin code that allowed me to achieve this (well, the “display child pages” bit) via a simple filter and a shortcode argument. You could certainly pretty easily use it to show sibling pages too.

    I made the following adjustment to wp-show-posts.php – line 384:

    $query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args ) );

    is now:

    $query = new WP_Query( apply_filters( 'wp_show_posts_shortcode_args', $args, $settings ) );

    This small change makes it so the $settings are passed through to the ‘wp_show_posts_shortcode_args’ filter, which means then you can make adjustments to the WP Query based on custom settings that you choose to implement via the shortcodes “settings” argument.

    In my functions.php I then added the following:

    add_filter( 'wp_show_posts_shortcode_args', 'hvydev_display_child_pages_only', 10, 2);
    
    function hvydev_display_child_pages_only( $args, $settings ){
    	
        if( is_page() && $settings['show_children'] == true ){
    
            $args['post_parent'] = get_the_ID($post);
            $args['orderby'] = 'menu_order title';	
        }
    
        return $args;
    }

    Using the above filter, I check that we are looking at a page (using is_page() ) and also check the “settings” for the value of “show_children”. If both are found to be true, I set a new argument on the WP_Query of “post_parent” equal to the ID of the $post (which is the page that the shortcode is called from). For good measure, I have also adjusted the “orderby” parameter of the $args array to be “menu_order title” which just gives me a little more control over how the Child Pages are ordered.

    The last piece of the puzzle is adding a custom “settings” argument to my shortcodes when I want to show only children. This is what the shortcode looks like:

    [wp_show_posts id="123" settings="show_children=true"]

    I hope that all makes sense. Obviously, this solution is a little “hacky” and you will need to be careful when updating the plugin since your change to the apply_filters call on line 384 will be overwritten, but perhaps the plugin developer will see the solution and see it appropriate to update the plugin to include this handy tweak.

    Good Luck.
    Sam

    Plugin Author Tom

    (@edge22)

    Hey Sam,

    Thanks for this! I’ve passed $settings to the filter in the next version: https://github.com/tomusborne/wp-show-posts/commit/286caf1164db8b6b6f38b85d3a011b519a27f4de

    Thread Starter Dan Stramer

    (@danstramer)

    Thanks Sam, Thanks Tom,
    So now with the next version, adding a function which taps into the shortcode can achieve all sorts of filters.
    Will this be available also in the UI of the plugin (adding some more selections: Siblings, child pages etc…).

    Thanks,
    Dan

    Plugin Author Tom

    (@edge22)

    It likely won’t be available within the UI, but it does make all of the WP_Query args available to be filtered ??

    Hey Tom and Dan,

    No worries, glad I could help.

    Dan, I know that not having UI to do this sort of thing might seem an odd choice, but I’d imagine it is a matter of drawing a line in the sand with regards to the level of control you need to give people for general use vs. what you can open up to folks through custom code. The ability to pass custom settings via the shortcode will make complex display scenarios a whole lot easier to achieve and it doesn’t limit those abilities to one use case which is assisted with convenient UI controls. All the best with implementing your desired customisations.

    Tom, thanks for your hard work on this and your other WordPress projects.

    Cheers,
    Sam

    Plugin Author Tom

    (@edge22)

    Thanks, Sam! Appreciate it ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Show pages (Child / Parent / Sibling)’ is closed to new replies.