WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] hide block of code if featured image exists

[Resolved] hide block of code if featured image exists

  • i seem to be getting my quotes and double quotes hosed up maybe? I’m trying to either show or hide this entire block of code IF a featured image exists . . . lil’ help?

    <div class="articlePhoto">
      <div class="articleImage"><?php echo get_the_post_thumbnail( get_the_ID(), 'medium', array ( 'class' => 'styled-image' ) ); ?></div>
                  <div class="relatedContent module">
                    <div class="moduleHeight">
                      <h3>You Might Also Consider</h3>
                      <div class="moduleContent">
                        <ul class="icons">
    					  <?php $post_categories = wp_get_post_categories( $post->ID ); ?>
                          <?php $query = array ( 'post_type' => 'articles', 'articletype' => 'news', 'category__in' => $post_categories, 'posts_per_page' => 5 ); ?>
                          <?php $queryObject = new WP_Query($query); ?>
                          <?php if ($queryObject->have_posts()) : while ($queryObject->have_posts()) : $queryObject->the_post(); ?>
                          <li class="icon-file"><a href="articles/<?php echo basename(get_permalink()); ?>/"><?php if (strlen($post->post_title) > 40) { echo substr(the_title($before = '', $after = '', FALSE), 0, 40) . '...'; } else { the_title();} ?>
                            </a></li>
                          <?php endwhile; else: ?>
                          <li>No research articles found.</li>
                          <?php endif; ?>
                          <?php wp_reset_query(); ?>
                        </ul>
                        <p><a href="/articles/news/"><i class="icon-circle-arrow-right"></i> See all items for sleep.</a></p>
                      </div>
                    </div>
                  </div>
                  <div class="clear"></div>
                </div>
Viewing 15 replies - 1 through 15 (of 15 total)
  • You could wrap it in a check for has_post_thumbnail()

    okay, since the code above already has php code there seems to be conflict with quotes and such. What’s the rule in this case?

    I was only answer the question of how to show or hide the block of code based on the condition of whether there was a featured image. I’m sorry if I misunderstood but I thought that was the question.

    As far as you code above, unless I’m missing something, I don’t see anything fundamentally wrong with it.

    What are you expecting to happen? And what is happening instead?

    this block of code is breaking in the browser I assume because of the tick marks or quotes. For instance the browser is actually rendering ‘medium’.

    Is this code that you copied from somewhere?

    yes, our main website

    This block of code basically displays an image and a widget. If there is no featured thumbnail, we want to hide this vblock of code so that the content (text) from the post moves up directly under the title.

    Have you tried replacing the apostrophe’s? Sometime when copying code from another site it doesn’t actually copy it as you intended.

    Also, Could you perhaps provide a link to the page where this is happening?

    No, it’s the correct code. Running this code above produces the image and widget. No link as it is an internal website not open to the public.

    I thought you said there was a problem but now you are saying that, “Running this code above produces the image and widget”. Then what is the issue we are trying to solve?

    it displays the image and widget whether there is a featured image or not. I want to hide this entire block of code IF there is no featured image.

    Actually jsut figured it out:

    <?php if( has_post_thumbnail() ) { ?>
    
    CODE ABOVE GOESW HERE
    
    <?php } else { ?>
    
    NO CODE
    
    <?php } ?>

    Thank you for your attention, James. Appreciated.

    but my very first response explains how to do that. You would wrap the code in an if( has_post_thumbnail() ) { //show } else { //don’t show } statement.

    edit: well done – I am a bit late ;-(

    I would not expect any problem with any single or double qoutes or whatever, if you add a line like this before all of your posted code, and then end it after the code:

    <?php if( has_post_thumnail() ) : ?>
    
    all your code here
    
    <?php endif; ?>
Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[Resolved] hide block of code if featured image exists’ is closed to new replies.
Skip to toolbar