WordPress.org

Ready to get started?Download WordPress

Forums

display custom field only if value present? (12 posts)

  1. kvtd1
    Member
    Posted 7 years ago #

    I currently have a custom field for rating the post's importance. In index.php template I call an image (named by number) based on the rating entered in the custom field:
    <img src="<?php bloginfo('url'); ?>/images/peppers/<?php $values = get_post_custom_values("peppers"); echo $values[0]; ?>.gif">
    Works fine except when there is no rating value entered for the post. Then I get the red x img placeholder

    How do I make this conditional - that is so it shows the img only if custom field key "peppers" has an assigned value? If no value (empty), nothing is placed there.
    -thanks

  2. MichaelH
    Member
    Posted 7 years ago #

    Something like;

    $values = get_post_custom_values("peppers");
    if ( is_array($values) )
    	<img src="<?php bloginfo('url'); ?>/images/peppers/<?php echo $values[0]; ?>.gif">
    else
    	<?php echo 'nothing to report'; ?>
  3. kvtd1
    Member
    Posted 7 years ago #

    thanks for pointing me in the right direction, but I can't get it to work without throwing an error no doubt to my newer than newbie php skills ...

  4. Dalton Rooney
    Member
    Posted 7 years ago #

    MichaelH - thanks so much, I was looking all over for this.

  5. mfapsj
    Member
    Posted 7 years ago #

    My original code:
    <a href="<?php echo get_post_meta($post->ID, "Photo Gallery", $single = true); ?>">Photo Gallery</a><br />

    The new one:

    <?php
    $values = get_post_custom_values("Photo Gallery");
    if ( is_array($values) )
    	<a href="<?php echo $values[0]; ?>">Photo Gallery</a> ?>
    else
    	<?php echo 'Photo Gallery'; ?> ?>
    <br />

    The error I get:
    Parse error: parse error, unexpected '<' in /home/content/m/a/r/mariaSun/html/marion/wp-content/themes/default/single-cat-14.php on line 31

  6. llizard
    Member
    Posted 6 years ago #

    Mfapsj, I'm pretty much a neophyte in php but I think your coding is closing the php prematurely.

    I'm not sure that this will work but you could give it a try:

    <?php
    $values = get_post_custom_values("Photo Gallery");
    if ( is_array($values) ) {
    echo '<a href="' . $values[0]. '">Photo Gallery</a>';
    }
    else {
    echo 'Photo Gallery';
    }
    ?>
    <br />
  7. llizard
    Member
    Posted 6 years ago #

    I'm running wp 2.0.11 with "Get Custom Field Values 2.1" and had hoped that this thread might resolve the difficulty I am having because not every post uses the fields. I read the thread Anyone using "Get Custom Field Values" plugin in WP2.2? and also have stared many times at WP Codex: Using Custom Fields but have been unsuccessful at translating the contents. (I have only a vague understanding of PHP...)

    This is the code I am currently using that does not break the page:

    <?php // add custom fields
    if (function_exists('c2c_get_custom')) {
    echo '<ul class="post-meta">';
    echo c2c_get_custom('Date of Publication', '<li>', '</li>');
    echo c2c_get_custom('Publication', '<li class="publication"><strong>', '</strong></li>');
    echo c2c_get_custom('Publication Title', '<li class="pubtitle"><strong>', '</strong></li>');
    echo c2c_get_custom('Publication Author', '<li>By ', '</li>');
    echo c2c_get_custom('Publication URL', '<li><small>(<a href="', ' " title="offsite link">learn more by following this link</a>)</small></li>');
    echo '</ul>';
    }
    else {echo '';}
    ?>

    Unfortunately, this does not quite achieve my expected results - if there are no custom fields filled in, then the source code shows

    <ul class="post-meta"></ul>

    which means that the page does not validate.

    After reading this thread, I tried the following:

    if (is_array(c2c_get_custom($field))) {
    echo '<ul class="post-meta">';
    echo c2c_get_custom('Date of Publication', '<li>', '</li>');
    echo c2c_get_custom('Publication', '<li class="publication"><strong>', '</strong></li>');
    echo c2c_get_custom('Publication Title', '<li class="pubtitle"><strong>', '</strong></li>');
    echo c2c_get_custom('Publication Author', '<li>By ', '</li>');
    
    echo c2c_get_custom('Publication URL', '<li><small>(<a href="', ' " title="offsite link">learn more by following this link</a>)</small></li>');
    echo '</ul>';
    }
    else {
    echo '';
    }

    I've also tried opening with

    if (!empty(c2c_get_custom)) {

    and while these do not cause error messages, they also do not display any custom fields at all.

    In the css for .post-meta, there is a border, so adding an empty set of <li></li> is not a viable option either because there will be an empty bordered section whenever the custom fields are not filled in.

    Any ideas on how I can fix this?

    (I hope this made sense!)

    -ejm

    P.S. Here is a post that has the custom fields filled in: etherwork.net/blog/?p=447
    and here is one that does not:
    etherwork.net/blog/?p=446

    (Please refrain from making those URLs into live links - I'm really getting tired of spammers...)

  8. llizard
    Member
    Posted 6 years ago #

    Excuse me for replying to myself. I hope I don't get in trouble but I have started a new thread about this entitled "conditional tags and using custom fields"

    Please reply at
    http://wordpress.org/support/topic/138355

  9. marzar00
    Member
    Posted 6 years ago #

    I have been using custom fields for a while. I place the field individually. For example I use a custom field called source to display the source of the news. There are two states. One is just text and the other is the text with a link. The code I use is:

    <?php if(get_post_custom_values('source_href')) :
    foreach(get_post_custom_values('source_href') as
    $source_href) {}
    foreach(get_post_custom_values('source_name') as
    $source_name) {} ?>
    <div class="source"><em>Source:</em> <a href="<?php echo $source_href; ?>" target="blank"><?php echo $source_name; ?></a></div>
    <?php elseif(get_post_custom_values('source_name')) :
    foreach(get_post_custom_values('source_name') as
    $source_name) {} ?>
    <div class="source"><em>Source:</em> <?php echo
    $source_name; ?></div>
    <?php endif; ?>

    Hope that helps,

    Marzar

  10. llizard
    Member
    Posted 6 years ago #

    Thank you for your reply, Marzar. But it does not really clarify for me how to use a conditional tag in order to surround the fields in an unordered list as described in this thread:

    http://wordpress.org/support/topic/138355

  11. vickyh
    Member
    Posted 6 years ago #

    Thanks. That has helped.

  12. buckyreal
    Member
    Posted 6 years ago #

    I can't get this code to work.

    I can go into the database and delete the custom (empty) Fax key and it works, but as long as the post is saved with an empty Fax: field it shows up... on the page... just a blank "Fax:"

    <?php $values = get_post_custom_values("Fax");
    if ( !empty($values) ) {
    echo 'Fax: ' . $values[0]. ' ';
    }
    else {
    echo 'No Fax';
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic