sábado, 14 de septiembre de 2013

Crear Custom Post Types en Wordpress

Los desarrolladores de Wordpress necesitan crear en multitples ocasiones diferentes tipos de post, para que el administrador pueda manipular las entradas de diferentes secciones del sitio de una forma ordenada.

Para ellos en el archivo functions.php de nuestro theme debemos ubicar funciones que le digan al core de Wordpress que necesitamos Custom Post Types.

Para ello simplemente debemos colocar una función de este estilo.

Código:

<?php

// Registramos el Custom Post Type
function artists_post_type() {

$labels = array(
'name'                => _x( 'Artists', 'Post Type General Name', 'text_domain' ),
'singular_name'       => _x( 'Artist', 'Post Type Singular Name', 'text_domain' ),
'menu_name'           => __( 'Artists', 'text_domain' ),
'parent_item_colon'   => __( 'Parent Product:', 'text_domain' ),
'all_items'           => __( 'All Artists', 'text_domain' ),
'view_item'           => __( 'View Artists', 'text_domain' ),
'add_new_item'        => __( 'Add New Artist', 'text_domain' ),
'add_new'             => __( 'New Artist', 'text_domain' ),
'edit_item'           => __( 'Edit Artist', 'text_domain' ),
'update_item'         => __( 'Update Artist', 'text_domain' ),
'search_items'        => __( 'Search artist', 'text_domain' ),
'not_found'           => __( 'No artists found', 'text_domain' ),
'not_found_in_trash'  => __( 'No artists found in Trash', 'text_domain' ),
);
   
    $rewrite = array(
    'slug'                => 'artist',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
    );  
   
$args = array(
'label'               => __( 'Artists', 'text_domain' ),
'description'         => __( 'Artists list', 'text_domain' ),
'labels'              => $labels,
'supports'            => array( 'title', 'editor', 'thumbnail', ),
'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'           => 'icon url',
'can_export'          => true,
'has_archive'         => true,
'exclude_from_search' => false,
'publicly_queryable'  => true,
'query_var'           => 'artist',
        'rewrite'             => $rewrite,
'capability_type'     => 'post',
);
 
register_post_type( 'Artists', $args );


}

// Llamador de la función
add_action( 'init', 'artists_post_type', 0 );

?>

Como se puede ver en el código es una función para una supuesta sección de "Artistas", éste código permite crear el tipo de post "Artists". Y de esa manera organizar mejor tu sitio Wordpress.

Como ven se le envían parámetros a la función  register_post_type();

Recomiendo utilizar esta herramienta online y gratuita para generar el código solo llenando campos en un formulario. Aquí el link: http://generatewp.com/post-type/

Cualquier duda a consulta no dudes en dejar tu comentario en el post con gusto te ayudaré.


No hay comentarios:

Publicar un comentario