categories - Style post archive blog preview based on category

admin2025-06-05  3

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749110399a316447.html

最新回复(0)