【发布时间】:2018-01-18 13:20:25
【问题描述】:
我在这里使用了一个名为 Kalium 的 wordpress 主题以及一些修改过的部分:https://www.idee-creative.co.uk
我在每种页面类型上都添加了自定义字段,因此我可以轻松添加自己的标题和描述标签,以便在每个页面上自定义它们。我使用的代码在这里:
<title><?php the_field('seo_page_title'); ?></title>
<meta name="description" content="<?php the_field('seo_page_description'); ?>"/>
拉入自定义字段并将它们显示在我的页面标题中。
我遇到的麻烦是,当我查看页面的源代码时,Wordpress 似乎添加了它自己的标签。所以我的网站有两个标签。我宁愿保留自己的自定义版本并删除 Wordpress 版本。
我似乎找不到它们来自哪里,我检查了我的 header.php 文件,除了上面我自己的自定义代码之外,似乎没有任何东西可以拉入标题标签......这是如果有帮助,完整的 header.php 代码:
<?php
/**
* Kalium WordPress Theme
*
* Laborator.co
* www.laborator.co
*/
// Get Menu Type To Use
$main_menu_type = get_data( 'main_menu_type' );
?>
<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" <?php language_attributes(); ?>> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" <?php language_attributes(); ?>> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" <?php language_attributes(); ?>> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html <?php language_attributes(); ?>> <!--<![endif]-->
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php the_field('seo_page_title'); ?></title>
<meta name="description" content="<?php the_field('seo_page_description'); ?>"/>
<!-- Inclide Schema Markup File
–––––––––––––––––––––––––––––––––––––––––––––––––– -->
<?php include('json-ld.php'); ?><script type="application/ld+json"><?php echo json_encode($payload); ?></script>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php
if ( apply_filters( 'kalium_show_header', true ) ) :
// Theme Borders
if ( get_data( 'theme_borders' ) ) :
get_template_part( 'tpls/borders' );
endif;
// Mobile Menu
include locate_template( 'tpls/menu-mobile.php' );
// Top Menu
if ( $main_menu_type == 'top-menu' || get_data( 'menu_top_force_include' ) ) {
include locate_template( 'tpls/menu-top.php' );
}
// Sidebar Menu
if ( $main_menu_type == 'sidebar-menu' || get_data( 'menu_sidebar_force_include' ) ) {
include locate_template( 'tpls/menu-sidebar.php' );
}
endif;
?>
<div class="wrapper" id="main-wrapper">
<?php
// Kalium Start Wrapper
do_action( 'kalium_wrapper_start' );
// Show Header
if ( apply_filters( 'kalium_show_header', true ) ):
// Main Header
get_template_part( 'tpls/header-main' );
endif;
?>
++++++++++++++更新++++++++++++
这是我发现的创建标题的函数...它在父主题中隐藏的包含文件中...
// Open Graph Meta
function kalium_wp_head_open_graph_meta() {
global $post;
// Only show if open graph meta is allowed
if ( ! apply_filters( 'kalium_open_graph_meta', true ) ) {
return;
}
// Do not show open graph meta on single posts
if ( ! is_singular() ) {
return;
}
$featured_image = $post_thumb_id = '';
if ( has_post_thumbnail( $post->ID ) ) {
$post_thumb_id = get_post_thumbnail_id( $post->ID );
$featured_image = wp_get_attachment_image_src( $post_thumb_id, 'original' );
}
// Excerpt, clean styles
$excerpt = kalium_clean_excerpt( get_the_excerpt(), true );
?>
<meta property="og:type" content="article"/>
<meta property="og:title" content="<?php echo esc_attr( get_the_title() ); ?>"/>
<meta property="og:url" content="<?php echo esc_url( get_permalink() ); ?>"/>
<meta property="og:site_name" content="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>"/>
<meta property="og:description" content="<?php echo esc_attr( $excerpt ); ?>"/>
<?php if ( is_array( $featured_image ) ) : ?>
<meta property="og:image" content="<?php echo $featured_image[0]; ?>"/>
<link itemprop="image" href="<?php echo $featured_image[0]; ?>" />
<?php if ( apply_filters( 'kalium_meta_google_thumbnail', true ) ) : $thumb = wp_get_attachment_image_src( $post_thumb_id, 'thumbnail' ); ?>
<!--
<PageMap>
<DataObject type="thumbnail">
<Attribute name="src" value="<?php echo $thumb[0]; ?>"/>
<Attribute name="width" value="<?php echo $thumb[1]; ?>"/>
<Attribute name="height" value="<?php echo $thumb[2]; ?>"/>
</DataObject>
</PageMap>
-->
<?php endif; ?>
<?php endif;
}
add_action( 'wp_head', 'kalium_wp_head_open_graph_meta', 5 );
++++++++++++++更新2 ++++++++++++
很抱歉不断更新,但我在主题的另一部分也有这个:
// Title Parts
function kalium_wp_title_parts( $title, $sep, $seplocation ) {
$kalium_separator = apply_filters( 'kalium_wp_title_separator', ' – ' );
if ( empty( $sep ) ) {
return $title;
}
$title_sep = explode( $sep, $title );
if ( ! is_array( $title_sep ) ) {
return $title;
}
if ( $seplocation == 'right' ) {
$title = str_replace( $sep . end( $title_sep ), $kalium_separator . end( $title_sep ), $title );
} else {
$title = str_replace( reset( $title_sep ) . $sep, reset( $title_sep ) . $kalium_separator, $title );
}
return $title;
}
add_filter( 'wp_title', 'kalium_wp_title_parts', 10, 3 );
【问题讨论】:
-
注释掉
-
为什么首先要为页面标题使用自定义字段?为什么要复制已经存在的字段?
-
你试过
add_filter( 'wp_title',吗? -
您的方式似乎是操作页面标题的正确方式。但是您可以尝试从 wp-includes/general-template.php 中的 wp_title 函数返回空字符串
-
CBroe - 因为它让我可以选择更改标题格式并让我能够为每个页面添加自定义页面标题。随意提出替代方案?