WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] hide block of code if featured image exists (15 posts)

  1. danalydesign
    Member
    Posted 1 year ago #

    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>
  2. James Laws
    The WP Ninjas
    Posted 1 year ago #

    You could wrap it in a check for has_post_thumbnail()

  3. danalydesign
    Member
    Posted 1 year ago #

    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?

  4. James Laws
    The WP Ninjas
    Posted 1 year ago #

    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?

  5. danalydesign
    Member
    Posted 1 year ago #

    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'.

  6. James Laws
    The WP Ninjas
    Posted 1 year ago #

    Is this code that you copied from somewhere?

  7. danalydesign
    Member
    Posted 1 year ago #

    yes, our main website

  8. danalydesign
    Member
    Posted 1 year ago #

    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.

  9. James Laws
    The WP Ninjas
    Posted 1 year ago #

    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?

  10. danalydesign
    Member
    Posted 1 year ago #

    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.

  11. James Laws
    The WP Ninjas
    Posted 1 year ago #

    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?

  12. danalydesign
    Member
    Posted 1 year ago #

    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.

  13. danalydesign
    Member
    Posted 1 year ago #

    Actually jsut figured it out:

    <?php if( has_post_thumbnail() ) { ?>
    
    CODE ABOVE GOESW HERE
    
    <?php } else { ?>
    
    NO CODE
    
    <?php } ?>
  14. danalydesign
    Member
    Posted 1 year ago #

    Thank you for your attention, James. Appreciated.

  15. James Laws
    The WP Ninjas
    Posted 1 year ago #

    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.

  16. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.