最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

404 error - Exclude from wp_list_pages by template

programmeradmin2浏览0评论

I have a series of "Thank You" pages which result from different form submissions. I would like to be able to exclude these from sitemaps based on their template. Specifically, and as an easy example, excluding them from being listed on the basic 404 page which utilizes the wp_list_pages.

I know how to manually exclude by ID but am looking for a better way to do this on scale and a more simplified approach--rather than having to edit code.

Is this approachable from a template perspective (code in template) or from the hook perspective (exclude by template vs exclude by ID). Essentially, the goal is to make this easier for front end content creators so as to avoid having to include multiple people in the process of publishing a single page.

'exclude'      => 'template-name',   versus the standard    'exclude'      => '17',

I have a series of "Thank You" pages which result from different form submissions. I would like to be able to exclude these from sitemaps based on their template. Specifically, and as an easy example, excluding them from being listed on the basic 404 page which utilizes the wp_list_pages.

I know how to manually exclude by ID but am looking for a better way to do this on scale and a more simplified approach--rather than having to edit code.

Is this approachable from a template perspective (code in template) or from the hook perspective (exclude by template vs exclude by ID). Essentially, the goal is to make this easier for front end content creators so as to avoid having to include multiple people in the process of publishing a single page.

'exclude'      => 'template-name',   versus the standard    'exclude'      => '17',
Share Improve this question asked Oct 21, 2015 at 20:13 Carlos EspinosaCarlos Espinosa 11 bronze badge 2
  • 1 by template, do you mean a template selected via the metabox when editing a page? – Milo Commented Oct 21, 2015 at 20:19
  • I would like content creators to be able to select a page template when editing the page that indicates the page should not be included in the sitemap--that would be ideal. If not that, then perhaps excluding a parent and all children? They could then just be sure to set as a child of specific parent in order to prevent the wp_list_pages from displaying. – Carlos Espinosa Commented Oct 22, 2015 at 18:35
Add a comment  | 

1 Answer 1

Reset to default 0

I would like content creators to be able to select a page template when editing the page that indicates the page should not be included in the sitemap

This seems a bit awkward to me. I would suggest instead to add a meta box that enables content creators to toggle an "include in sitemap" setting for each page. That said, the mechanism for excluding those pages from wp_list_pages would be the same, as the selected template is also stored in post meta data under the key _wp_page_template.

The first step is to get all of the page IDs that satisfy our meta query, whether that be checking for a page template or a flag you set in your meta box handler to denote that page is to be excluded from the sitemap. We also set the fields argument so the returned posts contains only an array of IDs. This will save memory versus querying entire page objects.

$args = array(
    'fields' => 'ids',
    'post_type' => 'page',
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
            'key' => '_hide_from_sitemap',
            'compare' => 'EXISTS',
        )
    )
);
$pages_to_exclude = new WP_Query( $args );

After this is run, $pages_to_exclude->posts will be an array of page IDs that you can pass directly as the exclude argument of wp_list_pages.

发布评论

评论列表(0)

  1. 暂无评论