如何在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>标记。它并没有改变它的附加。请有人帮我解决这个问题。

自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";
}

这是v4.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滤波器,它会缩短标题的生成,因此如果你不想对当前标题进行更改,它会更具性能:

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_titlepost_type_archive_title等函数生成标题并将输出:

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

对于将来遇到这个问题的人:这个功能可以使用Yoast SEO插件来完成。

但是,如果你仍然想自己做。。。。

为了修改标题,而不是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 );