如何替换 wordpress 中的元标题和元描述?

How to replace meta title and meta description in wordpress?

抱歉,如果我的问题很简单或愚蠢,但请帮助我解决这个问题。我正在尝试在 wordpress 中动态更改 <title><meta name="description" > 标签。所以这就是我在 function.php 文件中尝试的内容。

function changeMeta_2(){
    global $wpdb;
    $cur_url = $_SERVER['REQUEST_URI']; 
    $basename = pathinfo($cur_url);
    $ebasename = $basename['filename'];
    if(is_numeric($ebasename)) {
    $url = explode('/', $basename['dirname']);
    $basename = explode('.', $url[count($url)-2]);
    $ebasename = $basename[0];
    }
    $pageName = $ebasename;



    $arraylist_subcat  = array("car","bike","boat","xxxx","yyyy","zzz","mmmm");
    $arraylist_maincat = array("aus","ind","usa","uae");


    $category_id = get_term_by('slug',$pageName, 'category');   
    $category_parentid  = get_term_by('id', $category_id->parent, 'category');   
    $parent_slug =  $category_parentid->slug;



   if ( is_page()) {        
        if ( in_array($pageName,$arraylist_maincat) ) {         
                $metaTitle = 'Browse  '.$pageName.' | Some txt title | mysite.com';
                $metaDescription = 'some of custome blablaaaaa text description  '.$pageName.' some of custome blablaaaaa text description ';                               
                echo '<title>'.$metaTitle.'</title>';
                echo '<meta name="description" content="'.$metaDescription.'"/>';                   
        }
    }
}
add_action( 'wp_head', 'changeMeta_2' );

在上面的代码中,我试图更改与数组值(in_array 条件)匹配的术语 ID 的标题标签和元描述。

一切正常,但问题不是覆盖(替换)<title> 标签附加在头部。它没有改变它附加。请有人帮我解决这个问题。

对于以后遇到这个问题的任何人:可以使用 Yoast SEO plugin.

来完成此功能

但是,如果您仍然想自己做......

为了修改标题,而不是 wp_head 挂钩,您需要使用实际允许您 修改 标题的过滤器:wp_title

并且您可以/应该使用 wp_head 以便 添加 元描述(请参阅此处的文档:http://codex.wordpress.org/Meta_Tags_in_WordPress

另请注意获取页面标题的方法更简单,如下所述...

对于标题,您的代码如下所示:

function changeTitle($title, $sep, $seplocation){
    global $wpdb;

    // NOTE: This is the HARD way to get the page title, and is unreliable...
    $cur_url = $_SERVER['REQUEST_URI']; 
    $basename = pathinfo($cur_url);
    $ebasename = $basename['filename'];

    if(is_numeric($ebasename)) {
        $url = explode('/', $basename['dirname']);
        $basename = explode('.', $url[count($url)-2]);
        $ebasename = $basename[0];
    }

    $pageName = $ebasename;

    // NOTE: Why not get pagename this way?
    global $post;
    $pageName = $post->post_title;

    // or if you need the slug...
    $pageName = $post->post_slug;

    $arraylist_subcat  = array("car","bike","boat","xxxx","yyyy","zzz","mmmm");
    $arraylist_maincat = array("aus","ind","usa","uae");


    $category_id = get_term_by('slug',$pageName, 'category');   
    $category_parentid  = get_term_by('id', $category_id->parent, 'category');   
    $parent_slug =  $category_parentid->slug;



   if ( is_page()) {        
        if ( in_array($pageName,$arraylist_maincat) ) {         
                $title = 'Browse  '.$pageName.' | Some txt title | mysite.com';                 
        }
    }

    return $title;
}

add_action( 'wp_title', 'changeTitle', 10, 3 );

自 Wordpress v4.4.0 以来,文档标题的生成方式发生了变化。现在 wp_get_document_title 指示如何生成标题:

/**
 * Displays title tag with content.
 *
 * @ignore
 * @since 4.1.0
 * @since 4.4.0 Improved title output replaced `wp_title()`.
 * @access private
 */
function _wp_render_title_tag() {
    if ( ! current_theme_supports( 'title-tag' ) ) {
        return;
    }

    echo '<title>' . wp_get_document_title() . '</title>' . "\n";
}

这是来自 v5.4.2 的代码。以下是可用于操作标题标签的过滤器:

function wp_get_document_title() {
    /**
    * Filters the document title before it is generated.
    *
    * Passing a non-empty value will short-circuit wp_get_document_title(),
    * returning that value instead.
    *
    * @since 4.4.0
    *
    * @param string $title The document title. Default empty string.
    */
    $title = apply_filters( 'pre_get_document_title', '' );
    if ( ! empty( $title ) ) {
        return $title;
    }
    // --- snipped ---
    /**
    * Filters the separator for the document title.
    *
    * @since 4.4.0
    *
    * @param string $sep Document title separator. Default '-'.
    */
    $sep = apply_filters( 'document_title_separator', '-' );

    /**
    * Filters the parts of the document title.
    *
    * @since 4.4.0
    *
    * @param array $title {
    *     The document title parts.
    *
    *     @type string $title   Title of the viewed page.
    *     @type string $page    Optional. Page number if paginated.
    *     @type string $tagline Optional. Site description when on home page.
    *     @type string $site    Optional. Site title when not on home page.
    * }
    */
    $title = apply_filters( 'document_title_parts', $title );
    // --- snipped ---
    return $title;
}

所以这里有两种方法可以做到这一点。

第一个使用 pre_get_document_title 过滤器,它 short-circuits 生成标题,因此如果您不打算对当前标题进行更改,则性能更高:

function custom_document_title( $title ) {
    return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );

第二种方法使用 document_title_separatordocument_title_parts 挂钩标题以及稍后在函数中执行的标题分隔符,在使用 single_term_title 或 [= 等函数生成标题之后19=] 取决于页面即将输出:

// Custom function should return a string
function custom_seperator( $sep ) {
   return '>';
}
add_filter( 'document_title_separator', 'custom_seperator', 10 );

// Custom function should return an array
function custom_html_title( $title ) {
   return array(
     'title' => 'Custom Title',
     'site'  => 'Custom Site'
    );
}
add_filter( 'document_title_parts', 'custom_html_title', 10 );