• Resolved Chris_K

    (@handysolo)


    For some odd reason, I’m convinced that there exists a plugin that’ll do what I’m after. But I’ve yet to find it in my links or with searches. Hopefully it’s just a case of my search-fu being weak…

    I’d like to be able to add a custom field to some posts.
    Then, in the single.php I’d like to check for the field.
    If the field exists (or has a value of “True”?) I’d like to display some additional code at the end of the content.

    Does this ring any bells? Any ideas?

Viewing 7 replies - 1 through 7 (of 7 total)
  • My first question would be to what would be the determiner of which posts? Certain category?

    Thread Starter Chris_K

    (@handysolo)

    Not category. The determiner would be me remembering to add the custom field to the applicable posts.

    Elaboration of original post: Long story short, I want to manually/periodically add a custom field to some of my more popular posts. If that field is set or of a certain value (aka “True”) I want to add some code to the post.

    What code? The new tagometer!

    So, the idea is that I’d add a custom field to the applicable posts. Then, in single.php, I’d check for the tag and add code as necessary.

    For my plugin posts (to display download and source view links) I currently test off $post_meta_cache, like so:

    if( $post_meta_cache[$post->ID]['wp'] ) :

    With ‘wp’ the key for the desired custom field. Your mileage may vary…

    Thread Starter Chris_K

    (@handysolo)

    Ah, there we go. Don’t need a plugin, just need a kick in the rear. Thanks guys!

    Here’s what I ended up with:

    <?php if( $post_meta_cache[$post->ID]['ShowDtag'] ) { ?>
    <script src="http://images.del.icio.us/static/js/blogbadge.js"></script>
    <?php } ?>

    ShowDtag is my custom field. Only exists on posts that I want to show the del.icio.us thingy on.

    Thread Starter Chris_K

    (@handysolo)

    That little code snippet no longer works since I’ve upgraded to 2.1.

    Can I not use $post_meta_cache[$post->ID]['ShowDtag'] in that fashion any more?

    Use the API function get_post_meta($post_id, $key, $single = false)

    e.g.

    <?php if( get_post_meta($post->ID, 'ShowDtag', true) ) { ?>
    <script src="http://images.del.icio.us/static/js/blogbadge.js"></script>
    <?php } ?>

    The third param just tells it that you’re expecting a single result… otherwise you get an array of results.

    Thread Starter Chris_K

    (@handysolo)

    Thank you, Mark. Will give that a shot soon. 🙂

    [Update] Worked great. Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Plugin Quest – conditional display based on custom field val’ is closed to new replies.