WordPress.org

Ready to get started?Download WordPress

Forums

Custom Content Type Manager
Exclude custom field when there is no value (5 posts)

  1. tiffpowpow
    Member
    Posted 3 years ago #

    Hi there...this is a great! I've created a form using this plugin for various sites that I have. But sometimes not all the fields will be filled in, but the name of the field keeps showing up. Is there a way to code it so that if the field is left blank, the field name/type can be excluded from the post?

    Here is an example of a page where I'm using the form. The fields that have no value are still showing up.
    http://trialdemo.gobighead.com/listings/9704-valley-woods-lane/

    I am not a coder, so any help would be greatly appreciated. Thank you!

  2. Gonzalo
    Member
    Posted 3 years ago #

    Hi Tiff. I have solved this problem quite easily.
    Know you're not a programmer but still give you my solution. If you want to send me the page where you want to show the custom post type and arrange for you. It is really easy:
    I asked in an if statement if get_custom_field has a value. If it does print the field, if not ignored.
    Look:

    <div class="entry-content">
    
    	<?php the_post_thumbnail(); ?>
    	<?php if (get_custom_field('subtitulo')):?>
    		<p class="evento-subtitulo"><?php print_custom_field('subtitulo'); ?></p>
    	<?php endif;?>
    	<?php if (get_custom_field('fecha_inicio')):?>
    		<p><strong>Fecha:</strong> <?php print_custom_field('fecha_inicio'); ?><?php echo get_custom_field('fecha_fin') ? ' hasta el '.get_custom_field('fecha_fin') :''; ?></p>
    	<?php endif;?>
    	<?php if (get_custom_field('lugar')):?>
    		<p><strong>Lugar:</strong> <?php print_custom_field('lugar'); ?></p>
    	<?php endif;?>
    	<?php if (get_custom_field('organizador')):?>
    		<p><strong>Organiza:</strong> <?php print_custom_field('organizador'); ?></p>
    	<?php endif;?>
    	<?php if (get_custom_field('descripcion')):?>
    		<?php print_custom_field('descripcion'); ?>
    	<?php endif;?>
    	<?php if (get_custom_field('enlace')):?>
    		<p><strong>Página Web:</strong> <strong><a href="http://<?php print_custom_field('enlace'); ?>"><?php print_custom_field('enlace'); ?></a></strong></p>
    	<?php endif;?>
    	<?php if (get_custom_field('imagen')):?>
    		<p class="evento-imagen"><?php print_custom_field('imagen'); ?></p>
    	<?php endif;?>
    
    </div><!-- .entry-content -->

    Good luck!

    http://www.cuadric.com
    Maquetación y programación Web para diseñadores y agencias.

  3. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    Thanks Gonzalo for stepping in!

    I'm working to optimize the output that each field-type returns when the "print_custom_field()" function returns (e.g. in the future, image fields will actually print the image tag instead of just returning the integer key).

    I've opened up forums dedicated to this plugin here: http://cctm.freeforums.org/index.php
    I've been completely disappointed with the WP forums and their total lack of support for critical and ongoing problems with their repository and these forums, so I'd like to move discussions out of here as much as possible -- sorry, it's totally dumb to do that because WP can and should have this type of functionality, but after wresting with their decrepit system for almost a year now, I'm giving up and solving the problem by moving this functionality elsewhere. Thanks for understanding.

    Because these forums are not particularly useful to me as the developer since they don't notify me of problems, I kindly request that all bug reports be filed here: http://code.google.com/p/wordpress-custom-content-type-manager/issues/list

    Thanks!

  4. designisgopdx
    Member
    Posted 3 years ago #

    Is there a way to do that when using the multiselect custom field? Here is my code.

    <div class="in">
    				<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>	
    
    				<h1><?php the_title(); ?></h1>
    
    				<p><?php the_content(); ?></p>
    
    				<?php if (get_custom_field('print')):?>
    
    					<strong>Print Design</strong> <?php print_custom_field('print', array('<li>[+value+]</li>','<ul>[+content+]</ul>')); ?>
    
    				<?php endif;?>
    
    				<?php if (get_custom_field('brand')):?>
    
    					<strong>Brand & Identity</strong> <?php print_custom_field('brand', array('<li>[+value+]</li>','<ul>[+content+]</ul>')); ?>
    
    				<?php endif;?>
    
    				<?php if (get_custom_field('web')):?>
    
    					<strong>Web Design</strong> <?php print_custom_field('web', array('<li>[+value+]</li>','<ul>[+content+]</ul>')); ?>
    
    				<?php endif;?>
    
    				<?php if (get_custom_field('custom')):?>
    
    					<strong>Custom Design</strong> <?php print_custom_field('custom', array('<li>[+value+]</li>','<ul>[+content+]</ul>')); ?>
    
    				<?php endif;?>
    
    				<?php endwhile; // end of the loop. ?>
    
    			</div><!-- END in -->

    When one of the fields are empty, I'm getting an error:

    Warning: Invalid argument supplied for foreach() in /Users/minhly/Sites/digwp/wp-content/plugins/custom-content-type-manager/includes/OutputFilters.php on line 61

  5. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    The functions aren't intended to be used that way. You would need to provide each instance of get_custom_field or get_print_field with the arguments that are required, so instead of this:

    if (get_custom_field('custom')):

    You would need this:

    if ( get_custom_field('custom', array('<li>[+value+]</li>','<ul>[+content+]</ul>')) )

    But that's just really awkward.... I know it's an impossible battle with anyone who does front-end work, but in my opinion, trying to filter out empty values like that is a bad idea (yes, I know, I'm asking for it). So really what you're asking for is a different type of output filter -- one that can filter out empty values and return an optional bit of text if the value is empty.

    Alternatively, you could use the built-in WP function:
    get_post_meta(), e.g.

    if ( get_post_meta( get_the_ID(), 'custom', true) )

    Then use the get_custom_field function as you normally would -- using WP's function here bypasses and special function input criteria that is built into the output filters.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic