很抱歉,如果我的问题是基本的或愚蠢的,请帮我解决这个问题。我正在尝试在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' );
在上面的代码中,我试图更改与数组值匹配的term id的标题标签和元描述(in_array条件)。
一切正常,但问题是覆盖(替换)<title>
标记附加在头部。它没有改变它附加。请有人帮我解决这个问题。
答案 0 :(得分:1)
对于将来提出此问题的任何人:可以使用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 );
答案 1 :(得分:0)
自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
过滤器,它可以缩短标题的产生,因此如果您不打算对当前标题进行更改,则可以提高性能:
function custom_document_title( $title ) {
return 'Here is the new title';
}
add_filter( 'pre_get_document_title', 'custom_document_title', 10 );
第二种方法是,在使用document_title_separator
或{{1}之类的函数生成标题之后,使用document_title_parts
和single_term_title
钩子为标题和标题分隔符添加钩子,稍后在函数中执行},具体取决于页面和即将输出的内容:
post_type_archive_title