phpwordpresswordpress-theming

Issue on Adding Taxonomy to Custom Post Type Using Function


Using WordPress 3.7.1 and PHP 5.4.12, I am trying to add Custom Post Type and Taxonomy to my Theme, so far the custom post type method works but the Taxonomy is not adding to the Admin Dashboard.

Here is the code I have:

<?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');
?>

Can you please let me know what part I am doing wrong?


Solution

  • The $name variable is not parsed, put it between double quotes:

    register_post_type( "$name", $args );
    

    edit

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