سؤال

I'm really at loss as to why this isn't working:

I am using a shortcode to display a query on a custom post type + taxonomy, this is the segment of it:

        // - arguments -
        $args = array(
            'post_type' => 'customposttypename',
            'taxonomyname' => 'alpha',
            'post_status' => 'publish',
        );

        // - query -
        $my_query = null;
        $my_query = new WP_query($args);
        while ($my_query->have_posts()) : $my_query->the_post();

        // - variables -
        $custom = get_post_custom($post->ID);

... and then variables keep going. The thing is though, when I run the loop, it shows me all of the default post content (i.e. title, content, etc.), but refuses to show custom post type content and halts on the last line above (line 145), i.e. $custom = get_post_custom($post->ID); giving me the following error..

Notice: Undefined variable: post in C:\xampplite...\functions.php on line 145

Notice: Trying to get property of non-object in C:\xampplite...\functions.php on line 145

Then down here it shows me the title, content, etc. normally (just no custom post type content)

Anybody have a clue what' I'm doing wrong?

Thank you!

هل كانت مفيدة؟

المحلول

Notice: Trying to get property of non-object

Would indciate that $post has no scope inside your function. One simple solution here would be to globalise $post so it has scope, effectively fixing the error.

However, whenever you create a query, or run the loop and call the_post method this gives you access to the WordPress template tags.

Rather then put in global statements you could call get_the_ID() which should of course contain the given post's id(ie. the current post for that iteration of the loop).

Update your problem line to read..

$custom = get_post_custom( get_the_ID() );

And that should clear up the problem, of course noting giving $post scope inside the function would also work, it just wouldn't look as poetic!.. ;)

Small side question back to the asker, what are you looking for when you call get_post_custom, are you checking for some particular meta keys, or looping over every meta item you find? There may be some room for improvement there(if you're interested).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى wordpress.stackexchange
scroll top