全局$post在我的一些自定义帖子类型存档页面中显示空



我的英语不是很好看。
我有几种自定义帖子类型,"noticias"(新闻("articulos"(文章(等。在我的函数中.php我使用如下代码按post_id或post_type对脚本和样式进行排队:

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' );

我在主题文件夹中创建了archive-noticia.phparchive-articulo.php。但是当运行网站时,浏览器会在 articulo 存档页面中加载脚本和样式,但在 noticia 存档页面中不会。当我这样做时var_dump($post_type);在 articulo 存档页面中显示'articulo' (length=8)。但在 noticia 存档页面和其他自定义帖子类型中显示null
当我做register_post_type( 'articulo', $args);时,has_archive被设定为真实。在通知中也一样。

$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);

为什么WordPress这样做?我做错了什么?

请转到settings->permalinks并将其设置为"帖子名称"。如果已设置,只需单击"保存更改"按钮,htaccess文件将重写。

相关内容

  • 没有找到相关文章