使用函数向自定义帖子类型添加分类的问题

问题描述 投票:0回答:1

使用 WordPress 3.7.1 和 PHP 5.4.12,我尝试将自定义帖子类型和分类法添加到我的主题中,到目前为止,自定义帖子类型方法有效,但分类法未添加到管理仪表板。

这是我的代码:

<?php
 function add_post_type($name, $args = array()) {
    add_action('init', function() use($name, $args) {
            $upper = ucwords($name);
            $name = strtolower(str_replace(' ','_',$name));
            $args = array_merge(
            array(
            'public'=> true,
            'label' => "All $upper" . 's',
            'labels' => array('add_new_item' => "Add New $upper"),
            'support' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments')
            ),
            $args
            );
            register_post_type('$name', $args);
        });
}

function add_taxonomy($name, $post_type, $args = array()) {
    $name = strtolower($name);
    add_action('init', function() use($name, $post_type, $args) {
            $args = array_merge(
                array(
                'label' => ucwords($name),
                ),
                $args
            );
                register_taxonomy($name, $post_type, $args);
    }); 
}

add_post_type('book', array(
            'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments')
));
add_taxonomy('fun', 'book');
?>

你能让我知道我做错了什么吗?

php wordpress wordpress-theming
1个回答
0
投票

$name
变量未解析,将其放在双引号之间:

register_post_type( "$name", $args );

编辑

add_action( 'init', 'so19966809_init' );
function so19966809_init()
{
    register_post_type( 'book', array(
        'public'=> true,
        'label' => 'All Books',
        'labels' => array( 'add_new_item' => 'Add New Book' ),
        'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
        'taxonomies' => array( 'fun' )
    ) );
    register_taxonomy( 'fun', 'book', array(
        'label' => 'Fun',
    ) );
}
© www.soinside.com 2019 - 2024. All rights reserved.