Вот простая реализация.
Добавить в functions.php:
// add hook for new shortcode
add_shortcode('custom_field', 'shortcode_field');
function shortcode_field($atts) {
// get attributes that were passed in the shortcode
extract(shortcode_atts(array(
'post_id' => null,
), $atts));
// if we don't at least have a field name, then exit
if (! isset($atts[0])) {
return;
}
// field is the first attribute
$field = esc_attr($atts[0]);
// get the current post
global $post;
// if no post id was passed as a parameter to the shortcode, use the current post
$post_id = (null === $post_id) ? $post->ID : $post_id;
// do the custom field lookup for the appropriate post and retur the text
return get_post_meta($post_id, $field, true);
}
Вы бы использовать шорткод в WordPress редактора в одном из следующих двух способов:
[custom_field "field_name"] - looks up field_name for the current post
[custom_field "field_name" post_id=999] - looks up field_name for post with id 999
Вы можете получить фантазии и использовать ту же концепцию, с некоторыми плагинами генератора коротких кодов GUI, такими как Shortcodes Pro, что даст вам хорошую кнопку короткого кода в вашем редакторе wordpress.
Великолепный сэр! Спасибо, тонна! – user3886632