WordPress.org

Support

Support » How-To and Troubleshooting » Hide a Custom Fields div if there are no Custom Fields?

Hide a Custom Fields div if there are no Custom Fields?

  • Hi WPsters!

    I’m not especially math-inclined, but I’m fudging my way through some basic PHP to get some sweet little customizations together.

    I have a div for Custom Fields, and I’d like it only to appear when there are key values present. In other words: no custom fields, no div.

    Surely that’s possible?

    Thanks so much!
    Adam

Viewing 6 replies - 1 through 6 (of 6 total)
  • Christine Rondeau

    @crondeau

    Volunteer Forum Moderator

    Yep, that’s possible, you can do that with conditionals. So in your template, you would need to insert something like this:

    <?php while (have_posts()) : the_post();
    	  $link=get_post_meta($post->ID, 'link', true);
    	    if($link != '') {
    	     do something
    	    }else {
    	     do something else
    	    }
    	the_content();
    	endwhile; ?>

    I’ve got a post about conditionals here – http://www.bluelimemedia.com/2011/06/24/wordpress-conditional-tag/

    Thanks so much, Christine.

    I hate to ask for further clarification, but I still need my hand held through this process. Am I getting this? Replace do something

    with

    echo (<div id="details">);

    and leave the “details” div below it? What should I put in place of do something else to make sure it displays nothing?

    Cheers,
    Adam

    Christine Rondeau

    @crondeau

    Volunteer Forum Moderator

    You could leave the else out… so something like this :

    <?php while (have_posts()) : the_post();
    	  $link=get_post_meta($post->ID, 'link', true);
    	    if($link != '') {
    	     echo '<div id="details>';
    	    }
    	the_content();
    	endwhile; ?>

    I really appreciate you sticking with me.

    I copied/pasted and get an infinitely repeating div (screenshot). I’m working locally using MAMP, which makes this a little harder to share. Here’s the nearby code:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<div <?php post_class(); ?>>
    
    		<div class="entry">			
    
    			<div class="featuredimage">
    			<?php the_post_thumbnail('review'); ?>
    			</div>
    
    			<?php while (have_posts()) : the_post();
    	  $link=get_post_meta($post->ID, 'link', true);
    	    if($link != '') {
    	     echo '<div id="details>';
    	    }
    	the_content();
    	endwhile; ?>
    
    			<div id="details">
    			<h2>Details</h2> 

    You’re a lifesaver.

    Christine Rondeau

    @crondeau

    Volunteer Forum Moderator

    The code snippet that I had was for if I have a custom field with a key value “link” to do something.. otherwise do something else.

    I think that you’re trying to do something else.

    It sounds to me like you have more than one custom field, so you would need to check every single one..

    Thanks again, Christine.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hide a Custom Fields div if there are no Custom Fields?’ is closed to new replies.
Skip to toolbar