WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] custom fields (5 posts)

  1. tara_irvine
    Member
    Posted 3 years ago #

    I have this page that lists the titles of all the posts, I want some to be a different colour depending on a custom field, If it's empty it should remain the same but if the custom field (finished) has a value (any value) I would like the titles of those posts to be gray.

    Is this possible? There might only be one post with the custom field of finished...

    Finding this a little tricky.

  2. chinmoy
    Member
    Posted 3 years ago #

    Yes it is possible. I give you a sample.

    <?php
     $cust_val = get_post_meta($post->ID, "color", true);
     if($cust_val !=""):
    ?>
       <h2 style="color:<?php echo $cust_val; ?>"><?php the_title() ?></h2>
    <?php else: ?>
       <h2><?php the_title() ?></h2>
    <?php endif; ?>

    thanks
    Chinmoy

  3. keesiemeijer
    moderator
    Posted 3 years ago #

    You can do it with get post meta
    [edit] aha to slow, use chinmoy29 solutution

  4. tara_irvine
    Member
    Posted 3 years ago #

    w00t Works a treat thanks so much, I was on the right lines with an if vlue != "" but didn't know where to put it.

    One more question, It's working well on one page, but not on another, I'm unsure if it's to do with the style hierarchy or if it needs !important in the a href style.

    I'm sure I'll get there eventually like : )

  5. keesiemeijer
    moderator
    Posted 3 years ago #

    You have to put this code on your theme's template files where you want this to happen (page.php, index.php, single.php etc..)

Topic Closed

This topic has been closed to new replies.

About this Topic