1

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?

3
  • Can you share the code for the registration of the custom post types (both)? Commented Nov 17, 2017 at 21:56
  • 1
    Try using get_queried_object() instead and see what that returns. It's best to avoid globals whenever possible.
    – Milo
    Commented Nov 17, 2017 at 23:30
  • Thank you @Milo for your answer but i found a good solution, but no the answer to the question.
    – mhuenchul
    Commented Nov 18, 2017 at 0:00

4 Answers 4

1

The global $post object represents the current post within The Loop and is created by the the_post() function. It enables functions like the_title(), the_content() etc. to show the title and content for the current post without needing to pass IDs or anything.

This is why a post type archive doesn't have a $post object set when you're enqueuing scripts. Why would it? What post would it represent? What would $post->post_author be for a post type archive? It doesn't make sense to have one because an archive is not a post.

The correct solution is the one you found: The is_post_type_archive() function. Under the hood it checks the main Query to see if it is for the archive for a post type.

2
  • Good answer. Thank you a lot! But why when i do var_dump($post_type) still shows 'articulo' (length=8) in articulo archive page? Doesn't make any sense due your explanation
    – mhuenchul
    Commented Nov 18, 2017 at 5:16
  • The global post doesn't have properties relevant to the post type archive, but could be set somewhere else. Maybe from the first post in the query, maybe from a custom query occurring somewhere else before the scripts are enqueued. I'm not sure enough about the order of things right now to be certain, but regardless of whether or not it's set, it's not something that should be used for determining archives for the reasons I outlined in my answer. Commented Nov 18, 2017 at 10:45
0

I 've got similar issue, global $post was null on free theme (astrid) after struggling I disable Yoast SEO and its works.

So I update Yoast SEO and works with. But to be honest I will change Yoast by something else cause Im getting to much trouble with this plugin.

Hope its help :-)

EDIT : After multiple tests, Im not sure the issue comes from Yoast (but I will change it)... if you create you Custom Post Type after enable Yoast, you are getting this issue... disable/enable it and it works ;-)

0

I had the same problem with a custom post type. As suggested here, I had to reset permalink settings to %postname%.

  • Go To WP-Admin / Settings / Permalink
  • Select Post Name option
  • Save

If it is already select Post Name save it again, anyway

-2

Well, i found a solution in order to load scripts and styles in custom post types archive page, using is_post_type_archive()

$front_common = array($com1, $com2, $etc);
if ( is_single() || is_post_type_archive(array('noticia','articulo','acta')) || in_array($page_id, $front_common, true) ) { 
        wp_register_style('uikit_css', get_template_directory_uri().'/css/uikit.css', array(), '', 'all' );
        wp_enqueue_style('uikit_css');
}

But this solution don't answer the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.