php Wordpress-如何仅将代码插入特定页面的页眉?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16353441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:57:53  来源:igfitidea点击:

Wordpress- How to insert code to header for a specific page only?

phpwordpressheaderthemes

提问by david

I use wordpress and would like to add 2 lines of code to the header of one page only.

我使用 wordpress 并且只想在一页的页眉中添加 2 行代码。

The problem is that header.php will change all the site's headers and I want it to change only the header of one specific page.

问题是 header.php 将更改所有站点的标题,我希望它只更改一个特定页面的标题。

The only thing I want to do is add this 1 line :

我唯一想做的就是添加这一行:

<META name="robots" content="noindex, nofollow"/>

回答by nouveau

If you want to have a different header for a certain page you need to download your header.php from FTP, rename it to header-new.php (replace "new" with whatever you want), re-upload header-new.php to the same directory as your original header. - On the page template you want the new header to show up on

如果您想为某个页面使用不同的标题,您需要从 FTP 下载您的 header.php,将其重命名为 header-new.php(将“new”替换为您想要的任何内容),重新上传 header-new.php到与原始标题相同的目录。- 在您希望新页眉显示的页面模板上

replace:

代替:

<?php get_header(); ?> 

with

<?php get_header('new'); ?>

and now your new header will show up only on that specific page template

现在您的新标题将仅显示在该特定页面模板上

回答by kuldip Makadiya

you have to just add your pageid on your header file like this

你只需要像这样在你的头文件中添加你的 pageid

global $post;
if($post->post_type == 'page' && $post->ID == 'whatever page id'){
   echo '<meta name="robots" content="noindex, nofollow" />';
}

it will just display meta on specific page which you want.

它只会在您想要的特定页面上显示元数据。

suppose we have add just pageid == '5' it will display meta tag in just page id = '5'.

假设我们只添加了 pageid == '5',它将在页面 id = '5' 中显示元标记。

回答by david

Just correcting the answer of FDL, use this:

只是更正FDL的答案,使用这个:

global $post;
if($post->post_type == 'page' && $post->ID == 'yourid'){
   echo '<meta name="robots" content="noindex, nofollow" />';
}

回答by ingvar3000

An easier, non-coding solution to changing the robots meta tag on a per-page basis is by using the Yoast SEO plugin. https://yoast.com/wordpress/plugins/seo/You can set individual pages (such as a form thank-you page) as noindex and even nofollow if you're so inclined. If you're using Yoast to generate your sitemap as well, then you can exclude that page from the sitemap at the same time you noindex it, which will prevent errors in Search Console.

在每页的基础上更改机器人元标记的更简单的非编码解决方案是使用 Yoast SEO 插件。 https://yoast.com/wordpress/plugins/seo/如果您愿意,您可以将单个页面(例如表单感谢页面)设置为 noindex 甚至 nofollow。如果您也使用 Yoast 生成站点地图,那么您可以在不索引该页面的同时从站点地图中排除该页面,这将防止 Search Console 中出现错误。

If you want to have the ability to add some other tags or esoteric syntax to the then you can use the Per Page Add to Head plugin https://wordpress.org/plugins/per-page-add-to/Which will allow you to be very granular about which page gets which code.

如果您想向其中添加一些其他标签或深奥的语法,那么您可以使用 Per Page Add to Head 插件 https://wordpress.org/plugins/per-page-add-to/这将允许您非常详细地了解哪个页面获取哪个代码。

回答by marklchaves

This workedfor me. I didn't feel I needed to get the post type. I just needed the ID.

这个工作对我来说。我觉得我不需要获得帖子类型。我只需要ID

<?php
$post = get_post();
if ( $post->ID == 1234 ){
    echo '<!-- Point to the AMP version of this page. -->';
    echo '<link rel="amphtml" href="/amp/amp-story">';
}
?>

This did not workfor me as spec'd in the Codex(https://developer.wordpress.org/reference/functions/is_page/).

这对我不起作用,因为Codex( https://developer.wordpress.org/reference/functions/is_page/)中的规范。

is_page( 1234 )

is_page( 1234 )

回答by upss1988

Try with this:

试试这个:

if (is_page('page_slug') { ?>
   <META name="robots" content="noindex, nofollow"/>
<?php }

You can check the reference here: https://developer.wordpress.org/reference/functions/is_page/

您可以在此处查看参考:https: //developer.wordpress.org/reference/functions/is_page/

回答by ArleyM

Custom Fieldsare a great way to allow you to have page specific meta data that you can create logic on in your template files. The Codex link gives great examples of this.

自定义字段是一种很好的方式,它允许您拥有可以在模板文件中创建逻辑的页面特定元数据。Codex 链接给出了很好的例子。

回答by Chetan

<?php global $post; ?>
<?php if(is_page('pagename')){ ?>
      <meta name="robots" content="noindex, nofollow" />
<?php } ?>

回答by Igor

There's a plugin to do exactly the thing requested.
HiFi (Head Injection, Foot Injection)

有一个插件可以完全满足要求。
HiFi(头部注射、足部注射)

There's a "This plugin hasn't been updated in over 2 years...." notification, but I've just tested it on WP 3.5.2 - it works.

有一个“这个插件已经超过 2 年没有更新......”通知,但我刚刚在 WP 3.5.2 上对其进行了测试 - 它有效。

回答by dewaz

 <?php if (is_home()) { ?>
    <META name="robots" content="noindex, nofollow"/>
 <?php } ?>

this code will put meta only for home page, you may use is_single, is_archive, is_404 etc.

这段代码将只为主页放置元数据,您可以使用 is_single、is_archive、is_404 等。