Question

I am using developing a child theme for Woothemes' Canvas.

I am trying to use functions.php in the child theme to only use actions on my custom post type.

This code doesn't seem to be working:

 add_action( 'woo_post_inside_after', 'my_geo_mashup' );
function my_geo_mashup() {
       echo GeoMashup::map();
    if ($post->post_type == 'listings') {
        //My function
       }
}

add_action( 'woo_post_inside_before', 'listings_nivo' );
function listings_nivo() {
echo do_shortcode('[nivo source="current-post" ]');
 if ($post->post_type == 'listings') {
        //My function
       }
}

So, I'm unsure how to get the above to work properly and only show these items on the custom post type, or only for the custom post type template single-listings.php (as I only want the map and slider to show on the actual post, not on the blog page (archive.php)

Was it helpful?

Solution

Rather than making the entire $post object global, you can just make $post_type global instead. Ex below.

I'm not exactly sure where that function is being loaded, but make sure you hook somewhere within the post. If the action is before, as far as I know and from experience, the post variable will be null.

Just as a test, try running the action in wp_footer Ex. add_action( 'wp_footer', 'listings_nivo' ); See if that yeilds any results.

if echoing var_dump($post) is still null, well, not sure where to go from there.

So you can try running the below, then run the action in the appropriate place if it works:

function listings_nivo() {
    echo do_shortcode('[nivo source="current-post" ]');
    global $post_type;
        // Diagnostic purposes
    echo var_dump($post_type);
    if ($post_type == 'listings') {
            //My function
    }
}
add_action( 'wp_footer', 'listings_nivo' );

Check your error log or turn wp_debug to true in your wp-config.php file if nothing else to see if anything else is going on.

Best of luck!

OTHER TIPS

Inside your function, try adding global $post;. Then to see what you are getting with $post->post_type echo it out to the screen. As long as this gives you "listings", your code should work. If not, there's probably another issue at play.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top