My english is not very good looking.
I have several custom post types, 'noticias' (news) 'articulos' (articles), etc.
In my functions.php i enqueue scripts and styles by post_id or post_type using a code like this:
function mahg_scripts_by_page() {
global $post;
$page_id = $post->ID;
$post_type = $post->post_type; // var_dump($post_type);
$noti = 'noticia'; // POST-TYPE noticia
$arti = 'articulo'; // POST-TYPE articulo
$com1 = 4; // that page id
$com2 = 6; // that other page id
// $etc if i needed
$front_commons = array($noti, $arti, $com1, $com2, $etc);
if ( is_home() || is_404() || is_single() || in_array($page_id, $front_commons, true) ) {
wp_register_style('uikit_css', get_template_directory_uri().'/css/uikit.css', array(), '', 'all' );
wp_enqueue_style('uikit_css');
wp_register_script('uikit_js', get_template_directory_uri().'/js/uikit.min.js','','', true );
wp_enqueue_script('uikit_js');
if (is_home() ) {
wp_register_style('slideshow_css', get_template_directory_uri().'/css/components/slideshow.css', array(), '', 'all' );
wp_enqueue_style('slideshow_css');
wp_register_script('slideshow_js', get_template_directory_uri().'/js/components/slideshow.min.js','','', true );
wp_enqueue_script('slideshow_js');
}
// more scripts or styles from here
}
add_action('wp_enqueue_scripts', 'mahg_scripts_by_page' );
I created the archive-noticia.php
and archive-articulo.php
in my theme folder. But when run the website the browser load the scripts and styles in articulo archive page but in noticia archive page don't.
When i do var_dump($post_type);
shows 'articulo' (length=8)
in articulo archive page. But shows null
in noticia archive page and the others custom post types.
When i did the register_post_type( 'articulo', $args);
, has_archive was set true. Same in noticia.
$singular = 'Artículo';
$plural = 'Artículos';
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'add_new' => 'Nuevo ' . $singular,
'add_new_item' => 'Agregar nuevo ' . $singular,
'edit' => 'Editar',
'edit_item' => 'Editar ' . $singular,
'new_item' => 'Nuevo ' . $singular,
'view' => 'Ver ' . $singular,
'view_item' => 'Ver ' . $singular,
'search_term' => 'Buscar ' . $plural,
'parent' => 'Parent ' . $singular,
'not_found' => 'No se han encontrado ' . $plural,
'not_found_in_trash' => 'No hay ' . $plural .' en la papelera'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_in_nav_menus' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => false,
'menu_position' => 7,
'menu_icon' => 'dashicons-media-spreadsheet',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => true,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
'rewrite' => true,
'rewrite' => array(
'slug' => 'articulos',
'with_front' => true,
'pages' => true,
'feeds' => false,
),
'supports' => array('title','editor','thumbnail','excerpt')
);
register_post_type( 'articulo', $args);
$singular = 'Noticia';
$plural = 'Noticias';
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'add_new' => 'Nueva ' . $singular,
'add_new_item' => 'Agregar nueva ' . $singular,
'edit' => 'Editar',
'edit_item' => 'Editar ' . $singular,
'new_item' => 'Nueva ' . $singular,
'view' => 'Ver ' . $singular,
'view_item' => 'Ver ' . $singular,
'search_term' => 'Buscar ' . $plural,
'parent' => 'Parent ' . $singular,
'not_found' => 'No se han encontrado ' . $plural,
'not_found_in_trash' => 'No hay ' . $plural .' en la papelera'
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => false,
'show_in_menu' => true,
'show_in_admin_bar' => false,
'menu_position' => 6,
'menu_icon' => 'dashicons-media-document',
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => true,
'supports' => array('title','editor','thumbnail','excerpt'),
'has_archive' => true,
'rewrite' => true,
'rewrite' => array(
'slug' => 'noticias',
'with_front' => true,
'pages' => true,
'feeds' => false,
),
'query_var' => true,
'can_export' => true,
'delete_with_user' => false,
);
register_post_type( 'noticia', $args);
Why wordpress do that? What im doing wrong?
get_queried_object()
instead and see what that returns. It's best to avoid globals whenever possible.