Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Implementing Conditional Custom Taxonomy Code into Single.php WordPress

I'm wanting to output some custom write panel info into my single.php file. Here's the screenshot of the custom write panels that have been created...

http://www.cl.ly/1d461M3g370w0A2A1b3v

<strong>I'd like the content that is placed in these text fields to be inserted into a definition list within the single.php file. I need it to be conditional though upon whether or not I've filled out that part of the custom write panel.</strong> For example, for the first one listed (Purpose of Activity), I would not want either the definition title nor the definition description to be displayed unless I fill it out.

I would need it to look like this...


<dl>
<dt>Purpose of Activity</dt>
<dd><?php echo get_post_meta($post->ID, 'ecpt_purposeofactivity', true); ?></dd>
<dt>Materials Needed</dt>
<dd><?php echo get_post_meta($post->ID, 'ecpt_equipmentneeded', true); ?></dd>
<dt>And so on, and so on...</dt>
<dd>and so on, and so on...</dd>
</dl>


I need the code to be inserted somewhere in here, within the .post div.


<?php
woo_loop_before();

if (have_posts()) { $count = 0;
while (have_posts()) { the_post(); $count++;

woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.
}
}

woo_loop_after();
?>


Again, I would need it to be coded in such a way where if I have not filled one or more, or even all of the fields, that they would ONLY display IF I have filled them out. I hope that makes sense...

Thanks for your help!

Answers (3)

2011-06-13

Fahad Murtaza answers:

Something like this will work for you




<?php

woo_loop_before();



if (have_posts()) { $count = 0;

while (have_posts()) { the_post(); $count++;



woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.

}

}

show_meta_info();

woo_loop_after();

?>

<?php
function show_meta_info() {
global $post;
?>
<dl>
<?php if(get_post_meta($post->ID, 'ecpt_purposeofactivity', true)!=''){?>
<dt>Purpose of Activity</dt>

<dd><?php echo get_post_meta($post->ID, 'ecpt_purposeofactivity', true); ?></dd>
<?php }// end if ?>

<?php if(get_post_meta($post->ID, 'ecpt_equipmentneeded', true)!=''){?>
<dt>Materials Needed</dt>

<dd><?php echo get_post_meta($post->ID, 'ecpt_equipmentneeded', true); ?></dd>
<?php }// end if ?>

<?php if(0){ // You can place a condition instead of 0 for description, variation etc ?>
<dt>And so on, and so on...</dt>

<dd>and so on, and so on...</dd>
<?php }// end if ?>
</dl>
<?php
} // end function
?>


Spencer Barfuss comments:

Ok, I just realized that even though I've setup the custom post type and all, it's not feeding into the Home Page, which is pulling in the most recent posts. So, I'm not sure how to do that either, and if you feel like this should be worth more than $10, let me know, and I'll cancel this question.

Also, it seems that I'm having trouble getting this to work...


Fahad Murtaza comments:

Hi Spencer

I can help you fix that. Its not about money here, its about helping each other and getting a little reward :) That surely goes to cappuccino.

So please update the question and I will try to help with your updated question. Also if you want to increase the funds for more motivation, you can :)

Regards,
Fahd Murtaza


Spencer Barfuss comments:

Ok, well I need it to automatically show up on the home page as well. Whenever a post is made, they always show up on the home page. And I realized that once I created the custom post type and all, I don't know how to get it to show up on the home page like the other posts.

I'm using the Canvas theme by WooThemes. Do you need the code for the index.php file? Is that the one that controls what is posted on the front page?

Also, there is some functionality to enable Tumblog. I might want to enable this in the future. Is there another file that would need to be changed as well to ensure that newly created PE Lessons are showing up on the homepage with Tumblog enabled?

Thanks for your help!

2011-06-13

Michael Fields answers:

You'll just need a conditional statement for each custom field. The following should illustrate:


$meta = get_post_meta( $post->ID, 'ecpt_purposeofactivity', true );
if ( ! empty( $meta ) ) {
print '<dt>Purpose of Activity</dt>';
print '<dd>' . $meta . '</dd>';
}

2011-06-13

Jerson Baguio answers:

Try this if this maybe this what your looking into

<?php
woo_loop_before();
if (have_posts()) { $count = 0;

while (have_posts()) { the_post(); $count++;

woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.
$ecpt_purposeofactivity = get_post_meta($post->ID, 'ecpt_purposeofactivity', true);
$ecpt_equipmentneeded = get_post_meta($post->ID, 'ecpt_equipmentneeded', true);
?>
<dl>
<?php if($ecpt_purposeofactivity!=''):?>
<dt>Purpose of Activity</dt>
<dd><?php echo $ecpt_purposeofactivity; ?></dd>
<?php endif;?>
<?php if($ecpt_equipmentneeded!=''):?>
<dt>Materials Needed</dt>
<dd><?php echo $ecpt_equipmentneeded; ?></dd>
<?php endif?>
</dl>
<?php

}

}



woo_loop_after();

?>


Jerson Baguio comments:

And here's another version in case you might encounter issue on $post->ID so i replaced it with the_ID() instead


<?php
woo_loop_before();
if (have_posts()) { $count = 0;

while (have_posts()) { the_post(); $count++;

woo_get_template_part( 'content', get_post_type() ); // Get the post content template file, contextually.
$ecpt_purposeofactivity = get_post_meta(the_ID(), 'ecpt_purposeofactivity', true);
$ecpt_equipmentneeded = get_post_meta(the_ID(), 'ecpt_equipmentneeded', true);
?>
<dl>
<?php if($ecpt_purposeofactivity!=''):?>
<dt>Purpose of Activity</dt>
<dd><?php echo $ecpt_purposeofactivity; ?></dd>
<?php endif;?>
<?php if($ecpt_equipmentneeded!=''):?>
<dt>Materials Needed</dt>
<dd><?php echo $ecpt_equipmentneeded; ?></dd>
<?php endif?>
</dl>
<?php

}

}
woo_loop_after();

?>