Wordpress header external php file - change title?

2019-07-20 15:07发布

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();

标签: php wordpress
4条回答
We Are One
2楼-- · 2019-07-20 15:46
add_filter( 'wp_title', 'title_you_want',10);
function title_you_want(){
   return "my custom title";
}
查看更多
▲ chillily
3楼-- · 2019-07-20 15:55

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楼-- · 2019-07-20 16:12
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();
?>
查看更多
走好不送
5楼-- · 2019-07-20 16:13

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?

查看更多
登录 后发表回答