【问题标题】:Wordpress function.php add code for custom-post-typesWordpress function.php 为自定义帖子类型添加代码
【发布时间】:2018-04-11 08:29:10
【问题描述】:

此代码在每个页面的<body> 中添加类别和标签名称。

很遗憾,此代码仅适用于普通页面/帖子,不适用于自定义帖子类型。

我必须添加什么才能使其适用于自定义帖子类型?

<?php

/**
 * categorys and tags in body
 * 
 * @param string $classes
 * @return array|string
 */
function add_categories_and_tags($classes = '')
{
    if (is_page()) {
        // categories
        $categories = get_the_category();
        if (!empty($categories)) {
            foreach ($categories as $category) {
                $classes[] = 'category-' . $category->slug;
            }
        }

        // tags
        $tags = get_the_tags();
        if (!empty($tags)) {
            foreach ($tags as $tag) {
                $classes[] = 'tag-' . $tag->slug;
            }
        }
    }
    return $classes;
}

add_filter('body_class', 'add_categories_and_tags');

function getTagList($classes = '')
{
    $tags = get_the_tags($post->ID);
    $tagOutput = [];

    if (!empty($tags)) {
        array_push($tagOutput, '<ul class="tag-list ' . $classes . '">');
        foreach ($tags as $tag) {
            array_push($tagOutput, '<li>' . $tag->name . '</li>');
        }
        array_push($tagOutput, '</ul>');
    }

    return implode('', $tagOutput);
}

add_shortcode('tagsList', 'getTagList');

提前谢谢你!

【问题讨论】:

    标签: php wordpress wordpress-theming custom-wordpress-pages


    【解决方案1】:
    if( is_page() || is_single() || is_post_type('post_type') ) {
    

    ...

    会做,但不够具体

    【讨论】:

      【解决方案2】:

      if (is_page()) 检查就是这样 - 它检查您是否正在查看页面,因此它永远不会在自定义帖子上运行。只需删除该检查,它将在每个页面、帖子等上运行。

      【讨论】:

        猜你喜欢
        • 2018-02-23
        • 1970-01-01
        • 2011-11-19
        • 1970-01-01
        • 2013-08-04
        • 2014-11-02
        • 1970-01-01
        • 2015-10-04
        • 2016-04-04
        相关资源
        最近更新 更多