简体   繁体   English

将自定义正文 class 添加到自定义存档页面

[英]Adding custom body class to the custom archive pages

I have created two custom archive pages: archive-one.php and archive-two.php .我创建了两个自定义存档页面: archive-one.phparchive-two.php Archive pages are placed inside main catalog of my theme.存档页面放置在我的主题的主目录中。 Now what I trying to do is to add a custom body class to each of them like "archive-one" and "archive-two".现在我要做的是向它们中的每一个添加一个自定义主体 class ,例如“archive-one”和“archive-two”。

I was trying to do this with following code but with no luck:我试图用以下代码做到这一点,但没有运气:

function archive_class_1( $classes ) {
    if ( is_page_template( 'archive-one.php' ) ) {
        $classes[] = 'archive-one';
    }
    return $classes;
}
add_filter( 'body_class', 'archive_class_1' );

Confirming this is added to you theme functions.php ?确认这已添加到您的主题functions.php Next you can use the same function for both custom archive files (see below examples).接下来,您可以对两个自定义存档文件使用相同的 function(参见下面的示例)。 Are your custom template files at the root of your theme or a sub folder?您的自定义模板文件是位于主题的根目录还是子文件夹? if in a sub folder you need to add that path to you check like the examples below.如果在子文件夹中,您需要将该路径添加到您的检查中,如下例所示。 Also confirming that your theme is using the <?php body_class(); ?>还要确认您的主题正在使用<?php body_class(); ?> <?php body_class(); ?> function on your themes opening <body> tag (something like <body <?php body_class(); ?>> )? <?php body_class(); ?> function 在您的主题上打开<body>标签(类似于<body <?php body_class(); ?>> )?

Custom files at the root of theme主题根目录下的自定义文件

function my_archive_class( $classes ) {
    if ( is_page_template( 'archive-one.php' ) ) {
        $classes[] = 'archive-one';
    }

    if ( is_page_template( 'archive-two.php' ) ) {
        $classes[] = 'archive-two';
    }
    return $classes;
}
add_filter( 'body_class', 'my_archive_class' );

Custom files in sub folder "templates" theme子文件夹“模板”主题中的自定义文件

function my_archive_class( $classes ) {
    if ( is_page_template( 'templates/archive-one.php' ) ) {
        $classes[] = 'archive-one';
    }

    if ( is_page_template( 'archive-two.php' ) ) {
        $classes[] = 'templates/archive-two';
    }
    return $classes;
}
add_filter( 'body_class', 'my_archive_class' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM