【问题标题】:Wordpress header external php file - change title?Wordpress 标头外部 php 文件 - 更改标题?
【发布时间】:2017-10-15 02:09:44
【问题描述】:

我有一个外部 php 文件,我正在加载 wordpress 页眉和页脚,它可以正常工作,但有人知道如何更改页面标题吗?

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

// get wordpress header
get_header();

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    在文件中应用wp_title 过滤器对我有用:

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

    请参阅:What is the constant WP_USE_THEMES for?What is the correct way to use wordpress functions outside wordpress files?

    【讨论】:

    • 它没有用,但我删除了 wp-blog-header 并且刚刚 wp-load 并工作了,非常感谢!!
    【解决方案2】:
    add_filter( 'wp_title', 'title_you_want',10);
    function title_you_want(){
       return "my custom title";
    }
    

    【讨论】:

    • 尝试将优先级从 10 改为 1
    【解决方案3】:

    下面需要添加如下代码:

    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 | ';
    }
    

    这会奏效的!

    注意:您需要添加分隔符和空格,否则事物将与您的站点标题连接。

    【讨论】:

      【解决方案4】:
      add_filter( 'wp_title', 'wp_title_so_18381106', 10, 3 );
      

      它对我不起作用。我使用以下方法解决了这个问题:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-07-06
        • 2015-10-05
        • 2012-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-16
        • 1970-01-01
        相关资源
        最近更新 更多