首页 文章

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

提问于
浏览
0

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

www.thesitename.com/collections

我在 header.php 尝试了这个位:

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

但源代码中没有任何内容......
我在Yoast搜索引擎优化中随处可见,但我找不到解决方案,甚至不在谷歌上 . 你有想法吗?

2 回答

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

    希望这可以帮助!

  • 0

    Wordpress是在 PHP 中编写的,这意味着像这样的 HTML 本身不会起作用 . 出于这个原因,大多数跟踪代码都是通过包含 <?php include_once("analytics.php"); ?> 的内容来 Build 的 .

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

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

相关问题