WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Plugin Quest - conditional display based on custom field val (8 posts)

  1. Chris_K
    Member
    Posted 7 years ago #

    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?

  2. Michael Bishop

    Posted 7 years ago #

    My first question would be to what would be the determiner of which posts? Certain category?

  3. Chris_K
    Member
    Posted 7 years ago #

    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.

  4. Kafkaesqui

    Posted 7 years ago #

    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...

  5. Chris_K
    Member
    Posted 7 years ago #

    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.

  6. Chris_K
    Member
    Posted 7 years ago #

    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?

  7. Mark Jaquith
    WordPress Lead Dev
    Posted 7 years ago #

    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.

  8. Chris_K
    Member
    Posted 7 years ago #

    Thank you, Mark. Will give that a shot soon. :-)

    [Update] Worked great. Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic