Jeff Rodger
Jeff Rodger

Reputation: 11

Echo a custom field within a function?

So I have a custom field called 'ingredients'. I want to output this custom field information out onto my custom tab.

This is what I have but it echos out nothing.

function my_custom_tab(){

global $post;

echo get_post_meta($post->ID, 'ingredients', true);
}

Any ideas on why its not echoing anything out?

EDIT: Whatever I put in that last function I need to echo there like this:

add_filter('woocommerce_product_tabs', 'woo_remove_product_tabs', 98);

function woo_remove_product_tabs( $tabs ){

$tabs['my_custom_tab'] = array(
        'title' => "My Custom Tab",
        'priority' => 15,
        'callback' => 'my_custom_tab'

    );

return $tabs;
}
function my_custom_tab(){

echo '<h2>This is a title</h2>';
global $post;

return get_post_meta($post->ID, 'ingredients', true);
}

If I insert that echo it will output that in the custom tab. Is there anyway to just say echo 'ingredients', the custom field?

Upvotes: 0

Views: 133

Answers (2)

Jeff Rodger
Jeff Rodger

Reputation: 11

I figured it out. It was alot simplier than I expected:

function my_custom_tab(){
echo get_post_meta( get_the_ID(), 'ingredients', true ); 
}

Upvotes: 0

Script47
Script47

Reputation: 14550

Use return then echo the function.

function my_custom_tab(){

    global $post;

    return get_post_meta($post->ID, 'ingredients', true);
}

echo my_custom_tab();

return as you can guess, will pass the value back, so when you echo the function it should display the value.

Also make sure that get_post_meta is actually working.

Some background reading:

http://php.net/manual/en/functions.returning-values.php

Upvotes: 1

Related Questions