【问题标题】:Add a meta description for a custom post type archive in WordPress在 WordPress 中为自定义帖子类型存档添加元描述
【发布时间】:2017-03-20 05:43:11
【问题描述】:

这是我的问题:
我有一个 WordPress 网站,我在其中创建了一个自定义帖子类型(名为“collections”),每个集合都有一篇文章。
我想在自定义帖子类型的存档页面中添加元描述

www.thesitename.com/collections

我已经在header.php 中尝试过这个位:

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

但源代码中没有任何内容...
我在 Yoast SEO 中到处寻找,但我找不到解决方案,甚至在谷歌上也找不到。 你有什么想法吗?

【问题讨论】:

    标签: php wordpress seo custom-post-type yoast


    【解决方案1】:

    如果你想查看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';
    }
    

    添加信息但与您的问题没有直接关系。

    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';
    }
    

    希望这会有所帮助!

    【讨论】:

    • 我把 is_post_type_archive 和它的作品!谢谢!
    【解决方案2】:

    Wordpress 是用PHP 编写的,这意味着像这样的HTML 不会单独工作。正是由于这个原因,大多数跟踪代码都是通过包含类似于&lt;?php include_once("analytics.php"); ?&gt; 的内容来建立的

    在您的情况下,您必须 echo 任何这样的 HTML 元素。像这样:

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

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 1970-01-01
      • 2016-05-19
      • 1970-01-01
      • 2018-02-23
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多