admin管理员组

文章数量:1122846

I have read that the old pages widget allowed you to use a filter to include or exclude specific pages, but I can't figure out how to do this with the new block page-list widget.

I'm doing this on the sidebar and there are only certain pages I want to show.

I have read that the old pages widget allowed you to use a filter to include or exclude specific pages, but I can't figure out how to do this with the new block page-list widget.

I'm doing this on the sidebar and there are only certain pages I want to show.

Share Improve this question asked Oct 5, 2024 at 21:34 Scott C WilsonScott C Wilson 1138 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I had a look at the wp-block-page-list block rendering callback and it doesn't seem to provide any nice filters for this. The callback uses get_pages() for getting the list of pages, which means you can use get_pages_query_args and get_pages filter hooks to modify the pages query and the pages list. The difficult part here is to determine, when and where this filtering should happen - i.e. is the block in a sidebar or not.

One option to conditionally set the filter is to wrap the the default rendering callback in a helper function and handle the filtering logic in the helper. I haven't tested this, but something along these lines should do the trick.

add_filter( 'block_type_metadata_settings', 'wpse_427144_filter_render_block_core_page_list', 10, 2 );
function wpse_427144_filter_render_block_core_page_list( array $settings, array $metadata ): array {
    if ( 'core/page-list' === $metadata['name'] ) {
        $settings['render_callback'] = 'wpse_427144_filtered_core_block_page_list_renderer';
    }

    return $settings;
}

function wpse_427144_filtered_core_block_page_list_renderer( array $attributes, string $content, WP_Block $block ): string {
    // use another helper function to determine, if the filtering should happen or not
    $should_filter_args = wpse_427144_should_filter_pages_query_args( $attributes, $content, $block );

    // add filtering for this case
    if ( $should_filter_args ) {
        add_filter( 'get_pages_query_args', 'wpse_427144_filtered_pages_query_args', 10, 2 );
    }

    // let the default rendering callback do its thing
    $block_html = render_block_core_page_list( $attributes, $content, $block );

    // remove filtering to prevent modifying later get_pages() calls, if any
    if ( $should_filter_args ) {
        remove_filter( 'get_pages_query_args', 'wpse_427144_filtered_pages_query_args', 10, 2 );
    }

    return $block_html;
}

function wpse_427144_should_filter_pages_query_args( array $attributes, string $content, WP_Block $block ): bool {

    $conditions_met = false;

    // if ( some logic here... ) {
    //     $conditions_met = true;
    // }

    return $conditions_met;
}

function wpse_427144_filtered_pages_query_args( array $query_args, array $parsed_args ): array {

    // modify $query_args as needed

    return $query_args;
}

本文标签: widgetsHow do you add a filter to wpblockpagelist