sábado, 14 de septiembre de 2013

Crear Menu para Custom Post Types en Wordpress

Para darles un ejemplo podemos crear esto:


Como pueden observar, es el menú del administrador de Wordpress, pero en el figuran los tipos de post en diferentes opciones "Artists, Exhibitions, News". Son los tipos de post que necesita nuestro sitio de ejemplo.

Vayamos al código, para crear las opciones del menú haremos lo siguiente:

Nota "Debes insertar éste código en el archivo functions.php de tu theme, de lo contrario no funcionará"

<?php

// Registramos Custom Post Type
function news_post_type() {

$labels = array(
'name'                => _x( 'News', 'Post Type General Name', 'text_domain' ),
'singular_name'       => _x( 'News', 'Post Type Singular Name', 'text_domain' ),
'menu_name'           => __( 'News', 'text_domain' ),
'parent_item_colon'   => __( 'Parent News:', 'text_domain' ),
'all_items'           => __( 'All News', 'text_domain' ),
'view_item'           => __( 'View News', 'text_domain' ),
'add_new_item'        => __( 'Add New News', 'text_domain' ),
'add_new'             => __( 'New News', 'text_domain' ),
'edit_item'           => __( 'Edit News', 'text_domain' ),
'update_item'         => __( 'Update News', 'text_domain' ),
'search_items'        => __( 'Search News', 'text_domain' ),
'not_found'           => __( 'No News found', 'text_domain' ),
'not_found_in_trash'  => __( 'No News found in Trash', 'text_domain' ),
);
   
    $rewrite = array(
    'slug'                => 'new',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
    );  
$args = array(
'label'               => __( 'News', 'text_domain' ),
'description'         => __( 'News information pages', 'text_domain' ),
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'thumbnail'),
        'taxonomies'          => array('category'),
'hierarchical'        => false,
'public'              => true,
'show_ui'             => true,
'show_in_menu'        => true,
'show_in_nav_menus'   => true,
'show_in_admin_bar'   => true,
'menu_position'       => 5,
'menu_icon'           => '/wp-content/themes/Solomon/img/icon.png',
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'query_var'           => 'News',
        'rewrite'             => $rewrite,
'capability_type'     => 'post',
);
register_post_type( 'news', $args );

}

add_action( 'init', 'exhibition_post_type', 0 );

?>

Como pueden observar he resaltado en negrita la parte del código de registro de custom post types, que está encargada de definir la opción que figura en nuestro menú.

menu_name: para darle un nombre a nuestro menu
'supports': para definir las opciones que tendrá el editor de nuestro custom post type. En se le pasa un array que contiene array( 'title', 'editor', 'thumbnail'), eso quiere decir que el editor permitirá al usuario ingresar el título del post, el contenido y la imagen destacada o featured image.

Por último llamamos a add_action( 'init', 'exhibition_post_type', 0 ); para ejecutar la función.

Podemos utilizar una herramienta online y gratuita que genera el código que necesitamos llenando algunos campos de formulario. Aquí el enlace: http://generatewp.com/post-type/

No hay comentarios:

Publicar un comentario