WordPress function.php为自定义帖子类型添加代码

此代码在每个页面的<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');

先感谢你!


#1


if( is_page() || is_single() || is_post_type('post_type') ) {

会完成这项工作, 但还不够具体


#2


if(is_page())检查仅仅是-它检查你是否正在查看页面, 因此它永远不会在自定义帖子上运行。只需删除该支票, 它将在所有页面, 帖子等上运行。

来源:

https://www.srcmini02.com/66126.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37144.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?