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

private - How do I remove unwanted pages like archive, search, etc.?

programmeradmin0浏览0评论

By default WordPress pushes out a whole array of pages I don’t want or need - archive, author, blog, monthly, recent posts, categories, comments, attachment, taxenomies, search, search results, and probably a few others I’ve missed.

Most of the time I’m building regular broshure websites with no need for anything except a few fairly static pages. How do I get rid of all the other pages? Is there a plugin that will do this? Do I have to set up a dozen redirects in the .htaccess? I’ve tried searching, but all I find is how to hide parts of a page, or customise the sitemap to hide from searches. But I don't want those pages at all, so even entering the direct URL shouldn't work.

By default WordPress pushes out a whole array of pages I don’t want or need - archive, author, blog, monthly, recent posts, categories, comments, attachment, taxenomies, search, search results, and probably a few others I’ve missed.

Most of the time I’m building regular broshure websites with no need for anything except a few fairly static pages. How do I get rid of all the other pages? Is there a plugin that will do this? Do I have to set up a dozen redirects in the .htaccess? I’ve tried searching, but all I find is how to hide parts of a page, or customise the sitemap to hide from searches. But I don't want those pages at all, so even entering the direct URL shouldn't work.

Share Improve this question asked Apr 15, 2013 at 17:02 EysteinEystein 2351 gold badge2 silver badges10 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 7

You could redirect anything that's not a page or admin to home via the parse_query action:

function wpa_parse_query( $query ){
    if( ! is_admin() && ! $query->is_page() ) {
        wp_redirect( home_url() );
        exit;
    }
}
add_action( 'parse_query', 'wpa_parse_query' );

If it's not an admin screen or a query for a page, it'll redirect. You can see all of the types of pages this will remove under the Conditional Tags page in Codex.

Joost de Valk's WordPress SEO plugin is capable of disabling most, if not all, archives you mention:

You can user another small script, without adding any plugin. There is a post here and the code to add in the index.php of your theme is this:

if(is_archive()) {
    // force 404
    $wp_query->set_404();
    status_header( 404 );
    nocache_headers();
    include("404.php");
    die;
}

Hope you find it useful.

For anyone wondering, I ended up using .htaccess 301 redirects.

# Redirect useless pages
Options +FollowSymlinks
RewriteEngine on
RedirectMatch 301 ^/portfolio/.*$ /gallery/
RedirectMatch 301 ^/author/.*$ /
RedirectMatch 301 ^/category/.*$ /
RedirectMatch 301 ^/tag/.*$ /
RedirectMatch 301 ^/20.*$ /

The blogpost archive ^/20.*$ isn't ideal, but it will have to do for now. Also don't know what other pages I might've missed.

It can also be achieved using the template_redirect hook.

You can check for each type of archives pages, or "disable" them altogether.

  • Return a 404error or redirect to another page, I prefer the 404 approach-

In functions.php :

/* Disable archives pages */
add_action('template_redirect', 'my_disable_archives_function');

function my_disable_archives_function()
{
  /* Conditional checks examples:
      is_category() 
      is_tag()
      is_date()
      is_author()
      is_tax()
      is_search() ... */

    // Return a 404 for all archive types, except the my_custom_post_type archive.
    $post_types = array('my_custom_post_type');

  if ( (is_archive() && !is_post_type_archive( $post_types )) )
  {
      global $wp_query;
      $wp_query->set_404();
      status_header(404);
  }
}
发布评论

评论列表(0)

  1. 暂无评论