0

I have an existing shortcode that generates a list of child pages/posts. I add the shortcode to the parent page, and then it lists out all of the children.

HOWEVER, what I really want is to be able to control what children pages actually get listed — based on specific text within the slug.

For example, I really only want to list children pages that contain "website-design-" within the slug.

So, the shortcode would be added to the parent page, and would list these example page URLs (as linked pages titles):

But NOT:

This is the current shortcode function I have, but obviously it doesn't have any functionality yet to check for text within the slug:

// create shortcode to display child pages by using the [my_childpages] shortcode
// https://developer.wordpress.org/reference/functions/wp_list_pages/
function my_list_child_pages() { 
    global $post;
    if ( is_page() && $post->post_parent ) {
        $childpages = wp_list_pages( 'sort_column=post_title&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    }
    else {
        $childpages = wp_list_pages( 'sort_column=post_title&title_li=&child_of=' . $post->ID . '&echo=0' );
    }
    if ( $childpages ) {
        $string = '<ul>' . $childpages . '</ul>';
    }
    return $string;
}
add_shortcode('my_childpages', 'my_list_child_pages');
  • It sounds like a 'page category' would do the job significantly better, or page tags, have you considered a custom taxonomy instead? The queries would also be better, whereas what you ask will probably require raw SQL, which would bypass all the optimisation and caching WP provides (what you seek isn't an option provided by WP_Query and its wrappers ) – Tom J Nowell Aug 30 '17 at 21:56
  • Not opposed to adding a taxonomy to the pages, I suppose. Was hoping some sort of query would be feasible as-is though. I have about 300 pages I would need to add the taxonomy term to. And then another 300 for a similar (but different slug text) that would be a different taxonomy term. – Garconis Aug 31 '17 at 0:00
  • What are other examples (besides the "website-design" example)? If there's some reliable pattern that delineates the root words/phrases, then you could probably use some string-comparison/regex tests to filter the child pages you're looking for. For example, is the "root" phrase always a two-word phrase separated by a hyphen? – Jeff Cohan Aug 31 '17 at 22:46
  • @JeffCohan, the "root" phrase is NOT always two-words. But it would always occur at the start of the page slug. For example, I would like a shortcode to list "wordpresss-" child pages and another one to list "website-design-" child pages. See these example URLs: List of Website Design pages: https://www.example.com/parent/website-design-mumbo-jumbo/ https://www.example.com/parent/website-design-blah/ List of WordPress pages: https://www.example.com/parent/wordpress-heythere/ https://www.example.com/parent/wordpress-this-is-test/ – Garconis Sep 1 '17 at 16:16
  • Without a definable pattern for the root word or phrase, I can't think of a way to automatically and programmatically identify related pages based on page slugs. – Jeff Cohan Sep 1 '17 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.