English 中文(简体)
custom pages give 404 error title in Wordpress
原标题:

I m running a site powered by WordPress with extra pages... To integrate these pages with the WordPress theme I use this code:

<?php
$blog_longd= Title ; // page title
define( WP_USE_THEMES , false);
require( wp-blog-header.php );
get_header();
?>

html code

<?php
get_sidebar();
get_footer();
?>

This works fine, however page title shows always 404 Error Page (not "Title").

It seems that $wp-query->is_404 is always set to true. I tried overriding this value but it doesn t seem to work. I tried fixing it by putting header status 200 above function get_header()..also it doesn t work.

Any suggestions? Thanks

问题回答

I know it has been a long time since you asked but I had the problem and here is the solution.

<?php
require( ./wp-config.php );

$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
$wp->send_headers();

get_header();

echo "HELLO WORLD";

get_footer();
?>

Maybe clumsy, but if you implement the wp_title filter, you can change the title to what you want. You can add this code to the header of each custom page:

add_filter( wp_title ,  replace_title );
function replace_title() {
   return  My new title ;
}

If you want it a bit cleaner, use a smarter version of this filter to a plugin, and set only the global variable (here $override_title) in your page:

add_filter( wp_title ,  replace_title_if_global );
function replace_title_if_global($title) {
   global $override_title;
   if ($override_title) {
      return $override_title;
   }
   return $title;
}

There is code in the file class-wp.php:

function handle_404() {
...
    // Don t 404 for these queries if they matched an object.
    if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() ) {
        status_header( 200 );
        return;
    }
...
}

that handles 404 status for various of pages.

The stack of functions of this code is:

1) wp-blog-header.php:14, require()
2) function.php:775, wp()
3) class-wp.php:525, WP->main()
4) class-wp.php:491, handle_404()

So you have two ways to handle the situation:

1)

require( wp-blog-header.php );
function status_header( 200 ); 

2) more correct would be insert your own function here

if ( your_own_function() || ((is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object()) ) {

that returns true when your custom page is requested





相关问题
Wrap stray text in <p> tags

Wordpress issue.. how do I wrap stray text in P tags? Example: Before- <div class = "content"> <img src = "hello.jpg"/> <h1>Introduction</h1> Hello! this is ...

Using jQuery Plugins with Wordpress

Having a bit of trouble using jQuery plugins (Superfish, jQuery UI, etc) using Wordpress. Everything works fine in my plain non-Wordpress site, but Wordpress seems to conflict with JQuery. There must ...

WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Why can t I properly style a blockquote in Wordpress?

On the design I just created for my website, I have a blockquote styled with two quote images using the span technique in css: blockquote { background-image: url(images/openquote.jpg); background-...

How does the WordPress <!--nextpage--> tag actually work?

What happens? I m guessing that somehow the post or page is parsed before displaying, and then just split into two methods? I can t seem to find any documentation on how the underlying <?php ...

Wordpress Plug-ins: How-to add custom URL Handles

I m trying to write a Wordpress Plug-in but can t seem to figure out how you would modify how a URL gets handled, so for example: any requests made for: <url>/?myplugin=<pageID> will ...

热门标签