By Dave


2012-08-21 03:49:34 8 Comments

I registered a new post type, and I was wondering what the difference was between the $args and labels array. Read more about it in the codex (can't have more than two links, sorry).

For example, labels (code from this CPT tutorial)

'labels' => array(
    'name' => __( 'Super Dupers' ),
    'singular_name' => __( 'Super Duper' ),
    'add_new' => __( 'Add New' ),
    'add_new_item' => __( 'Add New Super Duper' ),
    'edit' => __( 'Edit' ),
    'edit_item' => __( 'Edit Super Duper' ),
    'new_item' => __( 'New Super Duper' ),
    'view' => __( 'View Super Duper' ),
    'view_item' => __( 'View Super Duper' ),
    'search_items' => __( 'Search Super Dupers' ),
    'not_found' => __( 'No super dupers found' ),
    'not_found_in_trash' => __( 'No super dupers found in Trash' ),
    'parent' => __( 'Parent Super Duper' ),
),

VS $args (code from this portfolio tutorial)

$args = array(  
        'label' => __('Portfolio'),  
        'singular_label' => __('Project'),  
        'public' => true,  
        'show_ui' => true,  
        'capability_type' => 'post',  
        'hierarchical' => false,  
        'rewrite' => true,  
        'supports' => array('title', 'editor', 'thumbnail')  
       ); 

What are the advantages of each? I noticed that if I tried to use some of the labels in the $args array, nothing happened or didn't happen as expected. For example, 'add_new_item' => __( 'Add New Super Duper' ), doesn't work if I include it in the $args array. I would like for those labels to work as well as all existing code in the $args array (i don't want anything to break).

1 comments

@Milo 2012-08-21 04:17:43

The code you posted is an incomplete copy/paste of the code from the tutorial you linked. labels is a parameter within the $args array:

$labels_array = array(
    'name' => _x('Books', 'post type general name')
);

$args = array(
    'labels' => $labels_array
);

register_post_type( 'book', $args );

$args is an array that contains an array of label parameters.

@Anh Tran 2012-08-21 06:36:44

That's right. You should always double-check the official documentation, too.

@Dave 2012-08-21 23:13:15

Hmm...the tutorial I linked doesn't have 'args' in any of the code at all. I'll try the code you posted and see if I get a result.

@Dave 2012-08-21 23:44:34

Look at the "Elaborate" example at the bottom. codex.wordpress.org/Function_Reference/register_post_type

@Milo 2012-08-22 15:34:07

Correct, there is no 'args' parameter, note in my example that $args is the variable name I've given to the function's arguments. "args" or "arguments" is a generic term for data passed to any function, that variable could be named $portfolio_data or whatever is meaningful to you, the important part is the contents of that array. Have a read through the PHP documentation on variables, data types, and functions to understand this further.

Related Questions

Sponsored Content

2 Answered Questions

1 Answered Questions

[SOLVED] Custom Post-Type not in admin menu

1 Answered Questions

2 Answered Questions

[SOLVED] Custom Post Type and Labels

1 Answered Questions

[SOLVED] ACF checkbox array is returning numbers, not labels

0 Answered Questions

"Post name" permalink returns home page for custom posts

2 Answered Questions

[SOLVED] Hierarchical Custom Post Types in Array

0 Answered Questions

CPT Parent as a page and CPT Child as a posts

1 Answered Questions

[SOLVED] Remove Post Custom Meta Box

2 Answered Questions

Sponsored Content