Forums

If post is in a specific category, do not show image (5 posts)

  1. ktmaten
    Member
    Posted 9 months ago #

    Hello,

    I am far from experienced with PHP. I know how to cut and paste a little but thats about as far as it goes. I was wondering if somebody could help me out with the following;

    On my blog I use a custom field called "Image" to add an image to the post and show it on top of the blog entry (on single.php). It's a rather big image (560x300). When I add a video to my post I would like that image not to show up. Basically, Im looking for a piece of code that can do the following:

    IF the post contains the tag 'video' DO NOT show image on top of post.

    From what I've read around these forums it seems difficult to do this based on a tag though, so I would also settle for the same code, based on a category instead of a tag.

    IF the post is in category 'video' DO NOT show image on top of post.

    This is what the coding on single.php looks like:

    <div id="content">
    
    	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
            <div class="post" id="post-<?php the_ID(); ?>">
    
    	<h2 class="title"><?php the_title(); ?></h2>
    	<div id="stats" class="clearfloat"><span class="left"> <!-- <?php _e('Geschreven door','arthemia');?> <?php the_author_posts_link(); ?> <?php _e('op','arthemia');?> --> <?php the_time(get_option('date_format')); ?> – <?php the_time(); ?> – <?php the_tags(' ', ' <b>x</b> '); ?></span><span class="right"><a href="#respond"><?php comments_number(__('0 Reacties','arthemia'), __('1 Reactie','arthemia'), __('% Reacties','arthemia') );?></a></span></div>
    
    	<div class="entry clear float">
    
    	<?php $values = get_post_custom_values("Image");
    	if (isset($values[0])) { ?>
    	<p><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
    $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=564&h=300&zc=1&q=100"
    alt="<?php the_title(); ?>" class="left" width="564px" height="300px"  /></a></p>
    
    <?php } ?>

    Any help would be much appreciated!

  2. alchymyth
    The Sweeper
    Posted 9 months ago #

    using the conditional tags in_categeory() or has_tag() might work;
    http://codex.wordpress.org/Function_Reference/in_category
    http://codex.wordpress.org/Function_Reference/has_tag

    example (untested):

    <?php if( !has_tag('video') ) : ?>
    <?php $values = get_post_custom_values("Image");
    	if (isset($values[0])) { ?>
    	<p><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img src="<?php echo bloginfo('template_url'); ?>/scripts/timthumb.php?src=/<?php
    $values = get_post_custom_values("Image"); echo $values[0]; ?>&w=564&h=300&zc=1&q=100"
    alt="<?php the_title(); ?>" class="left" width="564px" height="300px"  /></a></p>
    <?php } ?>
    <?php endif; ?>

    -----
    why don't you simply remove the custom field "Image" from all posts with a video?

  3. ktmaten
    Member
    Posted 9 months ago #

    Hey alchymyth, thanks for your reply.

    Like I stated in my first post, I barely know anything about PHP. However, looking at your piece of code, it seems to me that it will show the "Image" when a post contains a 'video' tag. What I want it to do is not show the "Image" when it has the 'video' tag. Again, I might be wrong because I don't know a whole lot about PHP. Im gonna try it out anyway.

    The reason I must use the "Image" custom field is because it not only adds an image on top of my blog post but also as a thumbnail on the homepage.

  4. alchymyth
    The Sweeper
    Posted 9 months ago #

    if( !has_tag('video') )

    the exclamation mark ! is a php logic negation operator - the above reads "if it not has the tag 'video' - show the image"

  5. ktmaten
    Member
    Posted 9 months ago #

    Thanks a lot, works like a charm!

Reply

You must log in to post.

About this Topic