【问题标题】:Remove <title> wordpress tag or?删除 <title> wordpress 标签或?
【发布时间】: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', ' &ndash; ' );

    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 - 因为它让我可以选择更改标题格式并让我能够为每个页面添加自定义页面标题。随意提出替代方案?

标签: php html wordpress


【解决方案1】:

您应该使用wp_title 挂钩 像这样:

add_filter( 'wp_title', 'my_custom_title_function', 20 );

然后只定义函数

function my_custom_title_function( $title ) {
   // use own function to produce title, for example:
   //  return str_replace('Old title', 'New title', $title); 
}

或者在您的情况下,例如:

function my_custom_title_function( $title ) {
return the_field('seo_page_title');
}

(假设the_field在那个阶段是一个有效且可用的函数。)

如果你想完全删除&lt;title&gt; 标签:

remove_action( 'wp_head', '_wp_render_title_tag', 1 );

或者以更简单的方式(子主题)

remove_theme_support( 'title-tag' )

您也可以使用wp_headob_start 等,但不推荐..

操作编辑后:

如果有其他过滤器,只需将其删除..

remove_filter( 'wp_title', 'kalium_wp_title_parts', 99); / or 1 

我还建议您阅读一些关于 wp 操作、过滤器和钩子的内容。它是 wp 开发的基础..

【讨论】:

  • 太好了,谢谢!在删除它之前,我会先尝试对其进行自定义。我想这一切都会在functions.php文件中出现?
  • 默认情况下是的,但它实际上可以去任何地方(例如插件..)。此外,如果这已经解决了您的问题,最好接受以某种方式标记为已解决的答案..
  • 嗨,当然,这都是正确的代码,但恐怕还是不行。如果你看上面我基于我认为生成标题标签的函数,这是父主题的函数文件中的一个包含,在另一个名为“inc”的文件夹中 - 所以我假设我可以在我的函数文件中放入一些东西禁用该功能?
猜你喜欢
  • 2011-11-12
  • 2013-01-07
  • 2013-07-16
  • 2014-01-31
  • 2017-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-06
相关资源
最近更新 更多