WordPress.org

Forums

[resolved] Hide thumbnail from certain category in home/index page (11 posts)

  1. M3
    Member
    Posted 2 years ago #

    Hi, I need urgent help please and really appreciate for your kind reply.

    My Question:
    How can I hide or to not showing post thumbnail from certain category in my home or index.php?

    My Current situations:
    1. The thumbnail and the permalink code is located in the index.php file, so I can not delete the entire code. And I can not move the code to the post page (single.php) file or to the post-type page (single-135.php) file either, since both file already displaying the full image of the post itself. View: Index/home page; while this is Single post page

    2. Currently I'm guessing--if it is possible--a solution to have a conditional code that can hide the thumbnail in the home/index page if the post is coming from a certain category. In my case is category 135 posting from the single-135.php.

    3. I have been trying to find a solution here and there, and I almost got it actually, but eventually the case is a bit different and just before the answer came out, the thread was closed. Lucky me!

    Thank you and apologize for my bad English. Please be patience since I'm not very good with php coding.

  2. Drew Jaynes
    4.2 Release Lead
    Posted 2 years ago #

    Hi M3,

    You can use a conditional on the the_post_thumbnail() call in your index.php loop to exclude for a specific category. Maybe something like:

    if ( has_post_thumbnail() && ! in_category('5') ) {
    	the_post_thumbnail();
    }

    The above example first checks if the post has a thumbnail AND (&&) the post is NOT in category id 5.

    Obviously you'd have to fit it to your own theme's style of code but you get the idea. If you can supply a Pastebin of your index.php loop code, we might be able to help you modify it.

    For more info, visit Conditional Tags in the Codex.

  3. M3
    Member
    Posted 2 years ago #

    Thanks Drew, I'll try it right away.

  4. M3
    Member
    Posted 2 years ago #

    Hi again Drew,
    I'm a bit confuse on how to combine the 2 codes which is the thumbnail and the permalink all together with the conditional code you gave me. This is the current code in my index.php:

    <div class="thumbnail">
    
    <a>" title="<?php the_title_attribute(); ?>">
    
    <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php getImage('1'); ?>&w=168&h=168&zc=1">
    
    </a>
    
    </div>
  5. Drew Jaynes
    4.2 Release Lead
    Posted 2 years ago #

    You could actually wrap a conditional around the whole thing.

    <?php if ( ! in_category('5') ) { ?>
    
    	<div class="thumbnail">
    		<a>" title="<?php the_title_attribute(); ?>">
    		<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php getImage('1'); ?>&w=168&h=168&zc=1">
    		</a>
    	</div>
    
    <?php } ?>
  6. M3
    Member
    Posted 2 years ago #

    The thumbnail disappear from category 135 but the <div> of the thumbnail is still there, how do I remove it?

    I decided not to use thumbnail permalink
    <a>" title="<?php the_title_attribute(); ?>">

    since readers eventually can click the title.

  7. Drew Jaynes
    4.2 Release Lead
    Posted 2 years ago #

    The <a> tag is malformed anyway (unless you removed it for brevity). It should be something more like this:
    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">

    EDIT: I didn't realize you'd supplied links in your first post, sorry. It looks like not only does the post excerpt container (<div class="entry">) have an absolute width of 360px, it is also floated right. So even when the thumbnail div is hidden, the space for the thumbnail is still reserved.

    Here's what I'd do in this situation:

    1. In the stylesheet, I'd add a new class mimicking the .entry class but removing the float and changing the width:
      .entry-no-image {
        font-size: 14px;
        line-height: 1.6em;
        padding-top: 0;
        text-align: justify;
        width: 100%;
      }
    2. In the Loop, I'd replace <div class="entry"> with a conditional check. Something like this:
      <?php if ( ! in_category('135') ) { ?>
      	<div class="entry">
      <?php } else { ?>
      	<div class="entry-no-image">
      <?php } ?>
    3. Then I'd also do the other in_category('135') check we discussed before.

    In essence, by adding a conditional check on the <div class="entry">, you can choose which class is applied to the the div and make sure the post excerpt moves over to occupy the now empty thumbnail space.

    Hope that helps.

  8. M3
    Member
    Posted 2 years ago #

    Sorry to trouble you again, for the last code you gave me I set it like this:

    <?php if ( ! in_category('135') ) { ?>
    	<div class="entry">
    
    	<?php wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore'); ?>
    
    	</div>
    
    <?php } else { ?>
    	<div class="entry-no-image">
    
    </div>
    <?php } ?>
    
    <div class="clear"></div>

    After I make the change as above and adding the css just after the .entry, the post content from category 135 using the single-135.php is not visible anymore in the index page.

    And beside putting image in the post contect for single-135.php, I also tried typing text but both didn't showed up. While all other post content besides from category 135 is visible and working fine.

    Thank you very much for your help Drew, I really appreciate it.

  9. Drew Jaynes
    4.2 Release Lead
    Posted 2 years ago #

    No problem. You accidentally wrapped the conditional around the entire div. You only want to wrap it around the opening tag, not the whole thing. Like this:

    <?php if ( ! in_category('135') ) { ?>
    
    	<div class="entry">
    <?php } else { ?>
    	<div class="entry-no-image">
    <?php } ?>

    BUT, you can simplify the (2) conditionals into a single check. This bit will handle both sections at the same time:

    <?php if ( ! in_category('135') ) { ?>
    
    	<div class="thumbnail">
    		<a>" title="<?php the_title_attribute(); ?>">
    		<img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php getImage('1'); ?>&w=168&h=168&zc=1">
    		</a>
    	</div>
    
    	<div class="entry">
    
    <?php } else { ?>
    
    	<div class="entry-no-image">
    
    <?php } ?>
    
    	<?php wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore'); ?>
    
    	</div>

    The above code works like this: If NOT in_category('135'), show the thumbnail div and div class="entry", OTHERWISE, skip the thumbnail div altogether and ONLY add <div class="entry-no-image">. Remember, on the <div class="entry"> part, you're only working with the opening tag of the div where the CSS class is assigned, NOT the whole div.

    Does that make sense?

  10. M3
    Member
    Posted 2 years ago #

    I made minor change to the code. I change conditional in <div class entry> from in_category to is_category and now the picture is back again but the reserved div of the thumbnail or the 360px text content area is still there. I also add for the excerpt code from:

    <?php wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore'); ?>

    to this conditional code:

    <?php if ( in_category('135')) {
    the_content();
    } else {
    wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore');
    } ?>

    Everything is back except for the previous reserved thumbnail area or the 360px text content area, which make post coming from category 135 is not stage in the middle.

    I hope my explanation is not confusing :)

    Anyway, thanks again for all the help.

  11. M3
    Member
    Posted 2 years ago #

    Hahaha :) thank you Drew it works perfectly now. Your awesome!

    Just for reference. this is the last code. as for the excerpt I use the change I've made. Anyway here it is:

    <?php if ( ! in_category('135') ) { ?>
    
    <div class="thumbnail">
    
    <img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php getImage('1'); ?>&w=168&h=168&zc=1"></a>
    
    </div>
    
    <div class="entry">
    
    <?php } else { ?>
    
    <div class="entry-no-image">
    
    <?php } ?>
    
    <?php if ( in_category('135')) {
    
    the_content();
    
    } else {
    
    wpe_excerpt('wpe_excerptlength_index', 'wpe_excerptmore');
    
    } ?>
    
    </div>

    Again, thank you very much for your help.

Topic Closed

This topic has been closed to new replies.

About this Topic