0

When editing a page, instead of the expected content, all the user sees is this:

[child-pages depth="1" sort_column="post_title"]

How can the user find that page in order to edit it?

1 Answer 1

4

This is a shortcode you see there in the post/page content. It's most likely displaying all the child pages (up to depth 1) of the current page. Shortcodes are very common, but you can't see their output in the WordPress post/page editor, only on the front end where it's generated via the do_shortcode() function.

The "hard" way:

So you can try do the following:

  1. Visit All Pages (/wp-admin/edit.php?post_type=page) in the backend.
  2. Locate the page that contains the [child-pages] shortcode.
  3. Locate it's child pages (depth 1). See the screenshot below.
  4. Edit each child page to your needs.
  5. View the shortcode page again.
  6. Done.

Here are the child pages of depth 1 in the All Pages table:

Child pages of depth 1

The "easy" way:

We can use the following plugin to construct our own custom meta box that displays a list of all the current child pages.

Demo plugin:

<?php

/**
 * Plugin Name: Child Pages Meta Box
 * Description: Child pages meta box for hierarchial post types
 * Author:      Birgir Erlendsson (birgire)
 * Plugin URI:  http://wordpress.stackexchange.com/a/158636/26350
 * Version:     0.0.2
 */

function wpse_current_child_pages_meta_box()
{
    $post_types = get_post_types(); 
    foreach ( $post_types as $post_type )
    {
        if( is_post_type_hierarchical( $post_type ) )
        {
            add_meta_box(
                'wpse_child_pages',
                __( 'Current child pages' ),
                'wpse_list_current_child_pages',
                $post_type,
                'side',
                'low'
            );
        }
    }
}
add_action( 'add_meta_boxes', 'wpse_current_child_pages_meta_box' );

function wpse_list_current_child_pages( $post )
{
    $args = array(
        'child_of'      => $post->ID,
        'echo'          => 0,
        'title_li'      => '',
        'post_type'     => $post->post_type,
        'walker'        => new WPSE_EditLinks
    );
    $style = '<style>.wpse_childpages li {margin-left: 15px;}</style>';
    $list = wp_list_pages( $args );
    if( ! $list )
        $list = sprintf( '<li>%s</li>', __( 'No child pages found!' ) );

    printf( '%s<ul class="wpse_childpages">%s</li>', $style, $list  );
}

class WPSE_EditLinks extends Walker_Page 
{
    function end_el( &$output, $page, $depth = 0, $args = array() )
    {       
        $edit_url  = admin_url( 'post.php?action=edit&post=' . $page->ID );
        $edit_link = " <a class='dashicons dashicons-edit' title='edit' href='" . esc_url( $edit_url ) . "' target='_blank'></a> ";
        $output = str_replace( "><a", ">". $edit_link. "<a", $output );
        $output .= "</li>\n";
    }
}

where we use wp_list_pages() to do all the hard work. But we only have to add the edit links, so that's why we construct a custom Page walker.

This plugin will automatically activate the meta box on all post edit screens for every hierarchical post types.

Where do I add this code?

Create this subdirectory:

/wp-content/plugins/wpse-current-child-pages-metabox/

Then save the code snippet to the file:

/wp-content/plugins/wpse-current-child-pages-metabox/wpse-current-child-pages-metabox.php

and activate the plugin from the backend:

/wp-admin/plugins.php

Screenshots:

Here's a screenshot of how it would work:

Current child pages

and with no child pages:

No child pages found!

Now you can just click on the edit link of the corresponding child page that you want to modify.

Much easier ;-)

Gist/GitHub:

The plugin is availabe here on Gist/GitHub.

7
  • Luckily i found the page on page 4 of 300 items, but that select box looks very useful; which file would i need to add that code to? Aug 22, 2014 at 8:18
  • @CeesTimmerman I updated the answer with a plugin, that's also available on Gist/Github here. Just activate the plugin and you're ready to go.
    – birgire
    Aug 22, 2014 at 13:10
  • 1
    I added information to my answer on where to save it - you don't have to (or should) modify core files for that. I hope it will work for you.
    – birgire
    Aug 22, 2014 at 13:13
  • I added a ?>, but it works. Thanks. :) Aug 22, 2014 at 13:46
  • 1
    hmm ... strange, I never use it. According to the PHP manual: "If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script." ...
    – birgire
    Aug 22, 2014 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.