$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Style post archive blog preview based on category|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Style post archive blog preview based on category

matteradmin10PV0评论

0 down vote favorite I have a page showing your typical series of small post teasers - images, excerpt etc which advertise properties for lease.

I would like to have the ability to add a 'new' or 'featured' icon based on its corresponding 'new' or 'featured' category or tag - either will do.

I have added these categories but they do not appear in the code when output and so I cannot target them.

How would I be able to perform the action:

If a post thumbnail has category of 'new' add the class 'new' so I can then target and style - repeating for each category.

I found this below which I think is similar but does not work

There will be multiple categories displaying on the archive page, but I want to style only the previews that have a certain category - I do not want to style the individual post page.

Unfortunately my php skills are limited

Thanks

$post = $wp_query->post;

if ( in_category('new', $post->ID) ) { ?>
    <body <?php body_class('new'); ?>> 
<?php
} 

elseif ( in_category('featured', $post->ID) ) { ?>
    <body <?php body_class('featured'); ?>> 
<?php
} 

else { ?>
    <body <?php body_class('class-name-generic'); ?>>
<?php
    }
?>

0 down vote favorite I have a page showing your typical series of small post teasers - images, excerpt etc which advertise properties for lease.

I would like to have the ability to add a 'new' or 'featured' icon based on its corresponding 'new' or 'featured' category or tag - either will do.

I have added these categories but they do not appear in the code when output and so I cannot target them.

How would I be able to perform the action:

If a post thumbnail has category of 'new' add the class 'new' so I can then target and style - repeating for each category.

I found this below which I think is similar but does not work

There will be multiple categories displaying on the archive page, but I want to style only the previews that have a certain category - I do not want to style the individual post page.

Unfortunately my php skills are limited

Thanks

$post = $wp_query->post;

if ( in_category('new', $post->ID) ) { ?>
    <body <?php body_class('new'); ?>> 
<?php
} 

elseif ( in_category('featured', $post->ID) ) { ?>
    <body <?php body_class('featured'); ?>> 
<?php
} 

else { ?>
    <body <?php body_class('class-name-generic'); ?>>
<?php
    }
?>
Share Improve this question asked Dec 10, 2018 at 12:37 shereesheree 1
Add a comment  | 

1 Answer 1

Reset to default 0

This sounds like something that should go to the The Loop - you want this class per post, not for the whole <body>, right? Roughly archive.php (or index.php, or category-*.php) should look like this:

while (have_posts()) {
    the_post();
    $add_class = array();
    if (in_category('new')) { // inside the loop, don't need $post->ID
        $add_class[] = 'new';
    if (in_category('featured'))
        $add_class[] = 'featured'; // and so on
    ?>
    <article <?php post_class($add_class); ?>>
    <?php // ...
}

Or, depending on the theme, in content.php or some version thereof. See docs for post_class.

Post a comment

comment list (0)

  1. No comments so far