• I need to know how to extract a value from a custom field and then insert it inside the Loop. The field will only exist on some records, so the code needs to format the code one way if the field value is null and another way if it is not null.

    The nearly complete page is located here.

    The code I am using is in a file in my theme called category-724.php. (The number is a reference to the category number.) Here’s the code, with the pseudo code in the comments:

    <?php get_header(); ?>
    
    <?php query_posts($query_string . "&showposts=20"); ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    	<div class="post">
    
    		<div class="p-head">
     			<p class="p-date"><?php the_time('l, F j, Y') ?></p>
    
     			/* If the value in the "external_link" custom field is null (i.e., the
     			field doesn't exist, I want to insert the line below. Note that it
     			is unlinked. */
    
    			 <h1><?php the_title(); ?></h1> 
    
    			 /* However, if the value in "external_link" custom field is not
    			 null (i.e., it exists and has a legitimate value), then I want to
    			 use that value to create a hyperlink with the post title, similar
    			 to the line below:
    
    			<h1><a href=$external_link target="_blank">$the_title</a></h1> */
    
    		</div>
    
    		<div class="p-con">
    			<?php the_content(); ?>
    		</div>
    
    		<?php the_tags('Resource: &nbsp',', '); ?>
    
    	</div>
    
    <?php endwhile; ?>
    <?php include("nav.php"); ?>
    <?php get_footer(); ?>

    The problem is that I am new to PHP and I am having a difficult type getting the IF-THEN loop right.

    Can someone lend me a hand? Thanks so much.

    Note: if you look at the source code on the page I used as an example, I have hard-coded the values in without the benefit of a custom field.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter michaelhyatt

    (@michaelhyatt)

    Anyone?

    Try something like this:

    <?php $values = get_post_custom_values("external_link");
    if (isset($values[0])) {
    	?>
                      <h1><a href="<?php $values = get_post_custom_values("external_link"); echo $values[0]; ?>" alt="" target="_blank" />"<?php the_title(); ?></a></h1>
    	<?php }
    	else { ?>
    		<h1><?php the_title(); ?></h1>
    	<?php } ?>
    Thread Starter michaelhyatt

    (@michaelhyatt)

    Brian Fegter of MisterNifty.com provided the solution. Here is the working code:

    <?php get_header(); ?>
    
    <?php query_posts($query_string . "&showposts=20"); ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
        <?php $external_link = get_post_meta($post->ID, 'external_link', true); ?>
    
    	<div class="post">
    
    		<div class="p-head">
     			<p class="p-date"><?php the_time('l, F j, Y') ?></p>                        
    
    			<?php if(!$external_link): ?>
    				<h1><?php the_title(); ?></h1>
    
    			<?php else: ?>
    				<h1><a href="<?php echo $external_link; ?>" target="_blank"><?php the_title(); ?></a></h1>
    
    			<?php endif; ?>
    
    		</div>
    
    		<div class="p-con">
    			<?php the_content(); ?>
    		</div>
    
    		<?php the_tags('Resource: &nbsp',', '); ?>
    
    	</div>
    
    <?php endwhile; ?>
    <?php include("nav.php"); ?>
    <?php get_footer(); ?>

    Thanks Michael!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Creating an IF-THEN condition within The Loop’ is closed to new replies.