Wordpress header external php file - change title?

2019-07-20 15:38发布

问题:

i have an external php file which i m loading the wordpress header and footer into which works fine but does anyone have any ideas how you can change the page title?

/* Short and sweet */
define('WP_USE_THEMES', false);
require('/home/reboot/public_html/wp-blog-header.php');

// get wordpress header
get_header();

回答1:

Applying wp_title filter in the file works for me:

define( 'WP_USE_THEMES', false );
require( $_SERVER['DOCUMENT_ROOT'] .'/wp-load.php' );

add_filter( 'wp_title', 'wp_title_so_18381106', 10, 3 );

function wp_title_so_18381106( $title, $sep, $seplocation ) {
    return 'Embeded WordPress';
}

// get wordpress header
get_header();

See: What is the constant WP_USE_THEMES for? and What is the correct way to use wordpress functions outside wordpress files?



回答2:

add_filter( 'wp_title', 'title_you_want',10);
function title_you_want(){
   return "my custom title";
}


回答3:

Just below require add the code like:

require('../wp-blog-header.php');
add_filter( 'wp_title', 'wp_title_so_18381106', 10, 3 );

function wp_title_so_18381106( $title, $sep, $seplocation ) {
    return 'Embeded WordPress | ';
}

And this will work!

Note: You'll need to add the separator and space, otherwise the thing will join with your site title.



回答4:

add_filter( 'wp_title', 'wp_title_so_18381106', 10, 3 );

It did not work for me. I fixed the problem by using the following:

<?php 
require('../wp-blog-header.php');

add_filter('pre_get_document_title', 'change_the_title');

function change_the_title() {
    return "The title that I'm looking for";
}

get_header();

echo "Here is the content!";

get_footer();
?>


标签: php wordpress