0

I must be doing something wrong here. I want to display the custom fields "pw_featured_note" & "pw_featured_price"

Here is my code so far

function featured() {

query_posts('post_type=property&posts_per_page=2');
    if (have_posts()) :
    while (have_posts()) : the_post();
    echo "<div class=\"singlefeatured group\">";
    if ( get_post_meta($post->ID, 'pw_featured_property', true) ) ;
    echo "<a href='".get_permalink()."'>";
    the_post_thumbnail('home-thumb');
    echo "</a>";
    echo "<h2><a href='".get_permalink()."'>".get_the_title()."</a></h2>";
    echo "<p>".get_post_meta($post->ID, 'pw_featured_property', true)."</p>";
    echo "<p> goodbye </p>";
    echo "</div>";
    endwhile;
    endif; 
    wp_reset_query();

}

add_shortcode('featuredproperty', 'featured');

Any help would be greatly appreciated.

1
  • 1
    What is your question? Plus, never echo something in a shortcode function, use return $string; instead.
    – fuxia
    Dec 16, 2011 at 23:47

3 Answers 3

0

Try this code, I've removed query_posts and used get_posts instead because I'm not sure if query_posts will work in a shortcode and using get_posts is safer.

function featured()
{
    $posts = get_posts(array('post_type' => 'property', 'posts_per_page' => 2));

    if(isset($posts) && !empty($posts))
    {
        foreach($posts as $post)
        {
            echo "<div class=\"singlefeatured group\">";

            //I wasn't sure what you wanted to get inside this if statement, please correct this
            if ( get_post_meta($post->ID, 'pw_featured_property', true))
            {
                echo "<a href='".get_permalink($post->ID)."'>";
                    echo get_the_post_thumbnail($post->ID, 'home-thumb');
                echo "</a>";
            }
            echo "<h2><a href='".get_permalink($post->ID)."'>".get_the_title($post->ID)."</a></h2>";
            echo "<p>".get_post_meta($post->ID, 'pw_featured_property', true)."</p>";
            echo "<p> goodbye </p>";
            echo "</div>";
        }

    }
}

add_shortcode('featuredproperty', 'featured');

Just make sure you check what comes inside the inner if statement, I've just pulled in the a tag.

1
  • You're welcome mate... Just one more thing, as you mentioned in the question, where are pw_featured_note & pw_featured_price fields in the code? I assume you figured that out! Dec 13, 2011 at 16:07
0

I can see from your code that you opened two 'if', but closed only one.

2
  • Thanks for noticing that, although it's working better with just the one closed "if" Dec 13, 2011 at 14:58
  • 1
    The second if line is also invalid, it has a semi-colon after it.
    – t31os
    Dec 13, 2011 at 15:28
0

Add this code to your functions.php:

function foobar_func( $atts ){
     global $post;
     $key=$atts['name'];
     if(empty($key)) return;
    return get_post_meta($post->ID, $key, true);
}
add_shortcode( 'foobar', 'foobar_func' );

And for display, add [foobar name=custom_field_name] to your post or page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.