如何在WP<head>中更改HTML结构


How to change HTML structure inside WP <head>

我正在清理一些HTML代码,我想要一个看起来像这样的HTML结构:

<!DOCTYPE html>
<html >
<head>
<title>...</title>
<meta name="description" content="..." />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> ...

所以基本上我希望"标题"和"元描述"出现在"头"标签之后。网站正在运行 Wordpress ,所以我已经查看了通用模板.php文件,但没有找到"元描述"的输出。

我是否需要安装一个用于元描述的插件(例如 Yoast),然后更改 wp_head() 的位置?还是我需要以某种方式编辑 wp_head() 函数?

编辑主题标题.php,简单:)确保<?php wp_head();?>在所有这些之下,如果它存在......

有很多清洁头解决方案,例如添加钩子来清理大多数默认功能(RSD,RSS链接等)。在主题函数中尝试以下钩子.php:

/*remove actions to clean wp_head*/
remove_action('wp_head', 'wlwmanifest_link'); // remove wlwmanifest.xml (needed to support windows live writer)
remove_action('wp_head', 'wp_generator'); // remove wordpress version
remove_action( 'wp_head', 'wp_resource_hints', 2 );//remove dns prefech
remove_action('wp_head', 'rsd_link'); // remove really simple discovery link
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 ); // remove shortlink
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );  // remove emojis
remove_action( 'wp_print_styles', 'print_emoji_styles' );   // remove emojis
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); // remove the / and previous post links
remove_action('wp_head', 'feed_links', 2); // remove rss feed links
remove_action('wp_head', 'feed_links_extra', 3); // removes all extra rss feed links
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); // remove the REST API link
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); // remove oEmbed discovery links
remove_action( 'template_redirect', 'rest_output_link_header', 11, 0 ); // remove the REST API link from HTTP Headers
remove_action( 'wp_head', 'wp_oembed_add_host_js' ); // remove oEmbed-specific javascript from front-end / back-end
remove_action('rest_api_init', 'wp_oembed_register_route'); // remove the oEmbed REST API route
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10); // don't filter oEmbed results

您还可以将此代码段包装到函数中,以使其after_theme_setupinit

其他标签(如 charsethttp-equiv 属性)可以在 header.php 中的 wp_head(); 操作之后插入。另请查看 wp-include 模板文件夹文件以查找 wp_head() 操作并删除/注释不必要的操作

只要确保<?php wp_head();?>在你的head标签之间。这样,当您安装 Yoast SEO 或任何插件时,将在此处插入适当的内容。