0

What I need basically is to display child pages on a parent. But I want to split the list of childs to, lets say, 'important' and 'non-important' to display it at the beginning and at the end of a parent page.

I turned on TAGs for pages in function.php like:

function tags_support_all() {
    register_taxonomy_for_object_type('post_tag', 'page');
}

// ensure all tags are included in queries
function tags_support_query($wp_query) {
    if ($wp_query->get('tag')) $wp_query->set('post_type', 'any');
}

// tag hooks
add_action('init', 'tags_support_all');
add_action('pre_get_posts', 'tags_support_query');

And here's my function and shortcode to display ALL childs on a parent:

function my_list_child_pages() { 
global $post; 
if ( is_page() && $post->post_parent )
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
if ( $childpages ) {
    $string = '<ul class="list-in-page-menu">' . $childpages . '</ul><div style="clear:both">';
}
return $string;
}

add_shortcode('my_childpages', 'my_list_child_pages');

But I can't figure out how to sort out child pages with tag 'important' in this function.

Is it possible at all?

0

3 Answers 3

1

Thanks @PieterGoosen and @realloc.

I came up with this solution:

Added meta_key=important&meta_value=1 in wp_list_pages(). And gonna use custom field for pages important: 1.

This is for list of IMPORTANT child pages.

function important_list_child_pages() { 
global $post; 
if ( is_page() && $post->post_parent )
    $childpages = wp_list_pages( 'meta_key=important&meta_value=1&sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
    $childpages = wp_list_pages( 'meta_key=important&meta_value=1&sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
if ( $childpages ) {
    $string = '<ul class="list-in-page-menu">' . $childpages . '</ul></div></div><div style="clear:both">';
}
return $string;
}

And this is for child pages excluded IMPORTANT ones

function non_important_child_pages() { 

    global $post;
    $args=array(
      'post_type' => 'page',
      'meta_key' => 'important',
      'meta_compare' => '=',
      'meta_value' => '1'
    );
    $pages = get_posts($args);

        if ($pages) {
      $pageids = array();
      foreach ($pages as $page) {
        $pageids[]= $page->ID;
      }
            $excluded = 'exclude='.implode(",", $pageids);
    }
        if ( is_page() && $post->post_parent )
        $childpages = wp_list_pages( '' . $excluded . '&sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0');
            else
        $childpages = wp_list_pages( '' . $excluded . '&sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0');

            if ( $childpages ) {
        $string = '<ul class="list-in-page-menu">' . $childpages . '</ul></div></div><div style="clear:both">';
    }
    return $string;
    }
0

You'll have to drop all page related functions if you need this type of functionality. Pages was never meant to have taxonomies, and therefor none of the page related functions was build to offer any kind of support for taxonomies

Rethink your structures and look into the use of custom post types and custom taxonomies. You can also look into using custom fields for certain functionalities.

If you really need to stick with the current setup you will most probably write your own set of functions or custom filters if the page related functions doesn't have filters to filter the relevant data. You might also look into more generic functions that have a more global support for your issue, but I don't think there is too many out there

0

The function wp_list_pages() has a filter that can come in handy here:

/**
 * Filter the array of pages to exclude from the pages list.
 *
 * @since 2.1.0
 *
 * @param array $exclude_array An array of page IDs to exclude.
 */
$r['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) );

Personally I would rethink your structures - Pieter already suggested that, but you can try to use it in this way:

function my_wp_list_pages_excludes( $exclude_array ) {
    // $exclude_array = get_all_your_non_important_pages_here(); 
    return $exclude_array;
}
add_filter( 'wp_list_pages_excludes', 'my_wp_list_pages_excludes' );

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.