1

Can someone translate this:

If posts category is "cars" show image

For example:

I have an article with category 'car' and another article with category 'banana'. No I'm looking for a way to add an car image to every article with 'car' category. And banana image to every article with 'banana' category. For default (if article has no category) I want to show the same car image.

1
  • 1
    Welcome to WPSE. We have a one question per post policy, and you are asking two. Please post the second question as a new question. Please also take your time and see How to Ask, help center and also visit the tour page :-) Sep 14, 2014 at 16:59

2 Answers 2

1

You can make use of the conditional tag has_catgeory. You can do something like this

if(has_category( 'cars' ) || has_category( 'uncategorized' )) {
    //display cars image
}elseif(has_category( 'banana' )) {
    //display banana image
}

From your comments, here is your code

<?php if(has_category( 'Update' ) || has_category( 'uncategorized' )) { ?>
    <img alt="Hello!" src="<?php bloginfo('template_directory'); ?>/img/loud.png"> 
<?php } elseif(has_category( 'Event' )) { ?>
    <img alt="Hello!" src="<?php bloginfo('template_directory'); ?>/img/event.png"> 
<?php } ?>

EDIT

The conditional tag in_category can also be used for this purpose

EDIT 2

For the second part of your question, check out this post by @JohannesPille on SO. Here is the post reposted.

PLEASE NOTE: I did upvote the answer on SO. If anyone see this as unfit, please rollback this edit :-)

The action create_category runs when a new category is created.

You want your category creation function to run when the theme is activated. The relevant action is after_setup_theme.

Drop this in your theme's functions.php and you should be good to go:

function create_my_cat () {
    if (file_exists (ABSPATH.'/wp-admin/includes/taxonomy.php')) {
        require_once (ABSPATH.'/wp-admin/includes/taxonomy.php'); 
        if ( ! get_cat_ID( 'Testimonials' ) ) {
            wp_create_category( 'Testimonials' );
        }
    }
}
add_action ( 'after_setup_theme', 'create_my_cat' );
3
  • @peter so my code should look like this?<?php if(has_category( 'Update' ) || has_category( 'uncategorized' )) { <img alt="Hello!" src="<?php bloginfo('template_directory'); ?>/img/loud.png"> } elseif(has_category( 'Event' )) { <img alt="Hello!" src="<?php bloginfo('template_directory'); ?>/img/event.png"> } ?>
    – Paul
    Sep 14, 2014 at 17:07
  • @Paul Please see my update, your code in your comments does not use proper php opening and closing tags which will result in a synatx error. I have corrected it and posted it in my answer Sep 14, 2014 at 17:13
  • My pleasure. Enjoy :-) Sep 14, 2014 at 17:45
0

The proper control structure would be a switch case, it makes for more readable and maintainable code. Here is an example within a loop.

<ol class="news-list">
    <?php while (have_posts()) : the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>">
                <span class="post-date">
                    <?php 
                        $category = get_the_category();

                        switch ($category[0]->term_id){
                            // display an icon next to the news item, according to the category.
                            // more icons: http://fontawesome.io/icon/

                            case 3:
                            // private item
                            ?>
                            <i class="fa fa-lock"></i>
                            <?
                            break;

                            case 18:
                            // Headteachers' blog item
                            ?>
                            <i class="fa fa-graduation-cap"></i>
                            <?
                            break;

                            default:
                            ?>
                            <i class="fa fa-newspaper-o"></i>
                            <?
                            break;

                            } 
                            the_time('j F Y'); ?>
                </span>
                <h4><?php the_title();?></h4>
                <p class="post-resume"><?php html5wp_excerpt('html5wp_index','empty_readmore_link'); ?></p>
            </a>
        </li>
    <?php endwhile; ?>
</ol>
1
  • Please properly add your code. Please honor readability Sep 14, 2014 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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