Tipo de publicación personalizada single- {custom} .php no funciona

12

Hice un tipo de publicación personalizada con el nombre de máquina special_media_post y wordpress simplemente no ve el single-special_media_post.php. Estoy completamente perdido. Se mantiene por defecto al index.php

Aquí está mi código para mi tipo de publicación personalizada y sus taxonomías:

//Post and Taxonomy stuff
//Register Custom Post Type
function special_media_post() {
$labels = array(
    'name'                => _x( 'Media Posts', 'Post Type General Name', 'text_domain' ),
    'singular_name'       => _x( 'Media Post', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'           => __( 'Media Post', 'text_domain' ),
    'parent_item_colon'   => __( 'Media Post:', 'text_domain' ),
    'all_items'           => __( 'All Media Posts', 'text_domain' ),
    'view_item'           => __( 'View Media Post', 'text_domain' ),
    'add_new_item'        => __( 'Add New Media Post', 'text_domain' ),
    'add_new'             => __( 'New Media Post', 'text_domain' ),
    'edit_item'           => __( 'Edit Media Post', 'text_domain' ),
    'update_item'         => __( 'Update Media Post', 'text_domain' ),
    'search_items'        => __( 'Search Media Posts', 'text_domain' ),
    'not_found'           => __( 'No media posts found', 'text_domain' ),
    'not_found_in_trash'  => __( 'No media posts found in Trash', 'text_domain' ),
);

$rewrite = array(
    'slug'                => 'mediapost',
    'with_front'          => true,
    'pages'               => true,
    'feeds'               => true,
);

$args = array(
    'label'               => __( 'mediapost', 'text_domain' ),
    'description'         => __( 'Post Type for Media', 'text_domain' ),
    'labels'              => $labels,
    'supports'            => array( 'title', 'editor', 'custom-fields', ),
    'taxonomies'          => array( 'year', 'type' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'query_var'           => 'mediapost',
    'rewrite'             => $rewrite,
    'capability_type'     => 'page',
);

register_post_type( 'special_media_post', $args );
}

// Register Custom Taxonomy
function media_year()  {
$labels = array(
    'name'                       => _x( 'Years', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Year', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Year', 'text_domain' ),
    'all_items'                  => __( 'All Years', 'text_domain' ),
    'parent_item'                => __( 'Parent Year', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Year:', 'text_domain' ),
    'new_item_name'              => __( 'New Year Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Year', 'text_domain' ),
    'edit_item'                  => __( 'Edit Year', 'text_domain' ),
    'update_item'                => __( 'Update Year', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate years with commas', 'text_domain' ),
    'search_items'               => __( 'Search years', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove years', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used yearss', 'text_domain' ),
);

$rewrite = array(
    'slug'                       => 'year',
    'with_front'                 => true,
    'hierarchical'               => true,
);

$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => 'year',
    'rewrite'                    => $rewrite,
    'capabilities'               => $capabilities,
);

register_taxonomy( 'year', 'special_media_post', $args );
}

// Register Custom Taxonomy
function media_type()  {
$labels = array(
    'name'                       => _x( 'Types', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Type', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Type', 'text_domain' ),
    'all_items'                  => __( 'All Types', 'text_domain' ),
    'parent_item'                => __( 'Parent Type', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Type:', 'text_domain' ),
    'new_item_name'              => __( 'New Type Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Type', 'text_domain' ),
    'edit_item'                  => __( 'Edit Type', 'text_domain' ),
    'update_item'                => __( 'Update Type', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate types with commas', 'text_domain' ),
    'search_items'               => __( 'Search types', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove types', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used types', 'text_domain' ),
);

$rewrite = array(
    'slug'                       => 'type',
    'with_front'                 => true,
    'hierarchical'               => true,
);

$capabilities = array(
    'manage_terms'               => 'manage_categories',
    'edit_terms'                 => 'manage_categories',
    'delete_terms'               => 'manage_categories',
    'assign_terms'               => 'edit_posts',
);

$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => 'media_type',
    'rewrite'                    => $rewrite,
    'capabilities'               => $capabilities,
);

register_taxonomy( 'type', 'special_media_post', $args );
}

// Hook into the 'init' action
add_action( 'init', 'special_media_post', 0 );

// Hook into the 'init' action
add_action( 'init', 'media_year', 0 );

// Hook into the 'init' action
add_action( 'init', 'media_type', 0 );

Si hay algo más que necesites ver, puedo ponerlo, pero ni siquiera lo veo si pongo un 'Hola Mundo' de eco ahí. Entonces simplemente no se ve el single-special_media_post.php o el archivo-special_media_post.php

    
pregunta BobbyDank 22.04.2013 - 21:30

4 respuestas

47

Visite la página de enlaces permanentes (que lo limpiará) y verifique nuevamente. Es probable que WordPress solo necesite ser empujado para reconocer su incorporación a la jerarquía.

    
respondido por el vancoder 22.04.2013 - 22:53
4

Cambiar el código

De:

 'has_archive'         => true,

Para:

 'has_archive'         => false,

Y luego vaya a la página de enlace permanente, cambie a la configuración predeterminada y vuelva a su "bonito enlace permanente"

% postname% /

Ahora debería funcionar.

La razón por la que no va a la página single- {custom_post_type} .php es debido a has_archive. Cuando has_archive se establece en verdadero, buscará archive- {custom_post_type} .php en lugar de la página única.

Espero que esto haya funcionado.

    
respondido por el Wesley Cheung 23.04.2013 - 15:06
1

Es una buena práctica, también utiliza register_activation_hook() y register_deactivation_hook() al crear nuevos tipos de contenido.

Parece que los nuevos tipos de contenido nuevos siempre no se volverán a escribir. Para evitar esto, ponga en el register_activation_hook () la devolución de llamada flush_rewrite_rules() y su nueva función de registro de contenido. No sé por qué, pero hacer esto parece evitar este problema. Mira:

register_activation_hook( __FILE__, 'your_active_hook' );

function your_active_hook() {
    special_media_post();
    flush_rewrite_rules();
}
    
respondido por el danilocgsilva 22.08.2017 - 13:36
0

Copié su código, borré las reglas de reescritura a través del administrador y ahora el tema usa las plantillas correctas cuando visito una publicación de medios.

Debe eliminar las reglas de reescritura una vez utilizando el gancho after_switch_theme. Esto asegurará que las reglas de reescritura se borren automáticamente después de que el usuario haya activado el tema.

Puede usar este código (directamente del Códice):

add_action( 'init', 'theme_prefix_cpt_init' );
function theme_prefix_cpt_init() {
    register_post_type( ... );
}

function theme_prefix_rewrite_flush() {
    flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'theme_prefix_rewrite_flush' );

Por favor, consulte el Codex de WordPress para obtener más información: enlace

EDITAR: En tal caso, el Complemento de Inspección de Reglas de Reescritura es muy útil, ya que le permite ver las reglas relacionadas con su tipo de publicación personalizada: enlace

En una nota al margen también tenga en cuenta que el lugar recomendado para colocar tipos de publicaciones personalizados es un complemento, no un tema.

    
respondido por el Fränk 23.04.2013 - 16:58

Lea otras preguntas en las etiquetas