简体   繁体   English

在WordPress中为自定义帖子类型档案添加元描述

[英]Add a meta description for a custom post type archive in WordPress

Here's my issue : 这是我的问题:
I have a WordPress site where I created a custom post type (named " collections ") with an article for each collection. 我有一个WordPress网站,我在其中创建了一个自定义帖子类型(名为“ collections ”),并为每个collection撰写了一篇文章。
I'd like to add a meta description to the archive page of the custom post type 我想向自定义帖子类型的存档页面添加元描述

www.thesitename.com/collections

I have tried with this bit in header.php : 我已经尝试过header.php这一点:

<?php
if (is_category('collections'))
{
    ?> 
    <meta name="description" content="the description">
<?php } ?>

but there's nothing in the source code... 但是源代码中什么都没有...
I have looked everywhere in Yoast SEO but I can't find a solution, not even on google. 我在Yoast SEO中到处都看过,但找不到解决方案,甚至在Google上也找不到。 Do you have ideas ? 你有想法吗?

If you want to check post_type archive page then you have to use is_post_type_archive() and if you want to check the archive page of custom post_type category then you have to use is_tax() 如果要检查post_type存档页面,则必须使用is_post_type_archive() ,如果要检查自定义post_type类别的存档页面,则必须使用is_tax()

if (is_post_type_archive('collections'))
{
    echo 'This is the collections post_type archive page';
}

Addition info but not directly related to your question. 添加信息,但与您的问题没有直接关系。

if (is_tax('collections_cat', 'cat-1'))//<-- Replace it with your taxonomy, term name
{
    echo 'This is the collections post_type, category cat 1 archive page';
}

Hope this helps! 希望这可以帮助!

Wordpress is written in PHP , which means that HTML like that will not work by itself. WordPress是用PHP ,这意味着类似的HTML本身将无法运行。 It is for this reason that in most tracking codes are established by including something along the lines of <?php include_once("analytics.php"); ?> 出于这个原因,在大多数跟踪代码中,都通过包含类似于<?php include_once("analytics.php"); ?> <?php include_once("analytics.php"); ?>

In your case, you would have to echo any HTML elements like this. 在您的情况下,您将必须echo任何这样的HTML元素。 Like so: 像这样:

  <?php if (is_category('collections')) {

    echo"<meta name='description' content='the description'>";

     }
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM