WordPress.org

Ready to get started?Download WordPress

Forums

Custom Field Display (7 posts)

  1. liondedan
    Member
    Posted 1 year ago #

    Hello,

    I've got a template for a website that I'm making, however not all the products that I sell have text for every single field. My php is not that great so I'm looking for a way to hide a fairly large div that contains an image and some custom field text.I know the basic structure but my knowledge of all the synax regarding the quotations and double quotations is really poor. This is the div that I need to hide if the custom field 'answer_text' is empty, but display if it contains any text:

    <!--MODUAL BOX-->
    <div class="modual_box">
    	<div class="modual_box_icon">
    		<img src="<?php bloginfo('template_directory'); ?>/images/faq_title_icon.gif" />
    	</div>
    	<h2>Frequently Asked Questions</h2>
    	<p>
    		<?php echo get_post_meta($post->ID, 'answer_text', true); ?>
    	</p>
    </div>
    	<!--END MODUAL CONTAINER-->
    	<div class="divider">
    		<div class="dark">
    		</div>
    		<div class="light">
    		</div>
    </div>
    	<!--FAQS END MODUAL BOX-->

    ---------------------------------------------

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

    try a conditional statement, for example:

    <?php if( get_post_meta($post->ID, 'answer_text', true) ) : ?> 
    
    <!--MODUAL BOX-->
    <div class="modual_box">
    	<div class="modual_box_icon">
    		<img src="<?php bloginfo('template_directory'); ?>/images/faq_title_icon.gif" />
    	</div>
    	<h2>Frequently Asked Questions</h2>
    	<p>
    		<?php echo get_post_meta($post->ID, 'answer_text', true); ?>
    	</p>
    </div>
    	<!--END MODUAL CONTAINER-->
    	<div class="divider">
    		<div class="dark">
    		</div>
    		<div class="light">
    		</div>
    </div>
    	<!--FAQS END MODUAL BOX-->
    
    <?php endif; ?>
  3. liondedan
    Member
    Posted 1 year ago #

    AMAZING. <--- didn't mean to do that, its just karma for helping. Could you explain to me exactly how you did it with regards to the quotes and double quotes? I was along the right lines but couldn't get my head around the correct syntax for the quotes.

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

    with regards to the quotes and double quotes?

    no quotes were harmed during the implementation of the code;

    only the first and last line are new, and those are just part of a standard conditional if statement.

  5. liondedan
    Member
    Posted 1 year ago #

    mmm okay, I thought you had to escape single quotes within double quotes inside php?

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

    you had to escape single quotes within double quotes inside php?

    no; you would need to escape double quotes within double quotes.

    however, your exsisting code is not even within php; the php tag for the conditional statement was closed at the end of the first line, etc.

    there are online php tutorials available where you can check the proper php syntax.

  7. liondedan
    Member
    Posted 1 year ago #

    Thank you very much for your time alchymth, I appreciate it!

Topic Closed

This topic has been closed to new replies.

About this Topic