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 badges1 Answer
Reset to default 0I 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
版权声明:本文标题:widgets - How do you add a filter to wp-block-page-list? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736284520a1927267.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论