WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using Shortcode in Post Meta (4 posts)

  1. vavroom
    Member
    Posted 4 years ago #

    Hello,

    I've spent the better part of the afternoon trying to make this work, all to no avail! I hope someone will have a pointer for me.

    I created a shortcode, which works like a charm if I use it in my post content. However, if I use it in a custom/meta field, it doesn't parse at all and gets displayed as is.

    Any idea what might be going on? And more importantly, how I can resolve it?

    Here's the code for it:

    function nsg_shortcode( $atts, $content = null ) {
    global $wpdb;
     $querystr = " SELECT $wpdb->posts.post_content, $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->postmeta.meta_value FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->postmeta.meta_key = 'quickdef'
    AND $wpdb->posts.post_title ='$content' ";
    
     $nsgdata = $wpdb->get_row($querystr, ARRAY_A);
    
    return $content . "<span class=\"pop\"> ( " . $nsgdata['meta_value'] . ")</span> <span class=\"question\">[<a rel=\"nofollow\" href=\"/?p=" . $nsgdata['ID'] ."\" title=\"" . $nsgdata['meta_value'] . "\">?</a>]</span>";
    
     $wpdb->flush();
    }
    add_shortcode('nsg', 'nsg_shortcode');
  2. vavroom
    Member
    Posted 4 years ago #

    I know it's frowned upon to bump a topic, but I could REALLY use some help on this and I'm no closer to figuring it out, despite continued testing of various ways to accomplish this... Anyone?

    Thanks

  3. velofille
    Member
    Posted 4 years ago #

    eval($nsgdata['meta_value'])

  4. vavroom
    Member
    Posted 4 years ago #

    With the assistance of a couple people, finally got this working.

    Instead of calling the custom field like so:
    echo get_post_meta($post->ID, 'recipe', true);

    I am doing this:

    $nsval = get_post_meta($post->ID, 'recipe', true);
    $nsoutput = do_shortcode( $nsval ) ;
    print $nsoutput;

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.