Hi,
I use a shortcode into the title of menu item that display a Custom Field Value (this is the actual main menu into the top bar -> submenu “Services”)
At the moment I have to set either the field + post_id [field "my_key" post_id=1]
or only the field
[field "my_key"]
and it will display the field value of the current post.
I would like to add a third possibility: In fact, The shortcode is used in a menu item type Page that contains Custom Field Value I want to display is stored in the same post_id.
I would like use a shortcode like that [field "my_key" post_id=item_menu_pageid]
or directly by hook for a specific item menu.
this is the function:
add_shortcode('field', 'shortcode_field');
function shortcode_field($atts){
extract(shortcode_atts(array(
'post_id' => NULL,
), $atts));
if(!isset($atts[0])) return;
$field = esc_attr($atts[0]);
global $post;
$post_id = (NULL === $post_id) ? $post->ID : $post_id;
return get_post_meta($post_id, $field, true);
}
from https://www.isitwp.com/get-custom-field-value-with-shortcode/
could you help?