WordPress.org

Ready to get started?Download WordPress

Forums

Hide a Custom Fields div if there are no Custom Fields? (7 posts)

  1. Adam
    Member
    Posted 2 years ago #

    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

  2. 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/

  3. Adam
    Member
    Posted 2 years ago #

    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

  4. 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; ?>
  5. Adam
    Member
    Posted 2 years ago #

    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.

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

  7. Adam
    Member
    Posted 2 years ago #

    Thanks again, Christine.

Topic Closed

This topic has been closed to new replies.

About this Topic