Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nextab/1170afb98a8325b7e5edd0553f7fb01e to your computer and use it in GitHub Desktop.
Save nextab/1170afb98a8325b7e5edd0553f7fb01e to your computer and use it in GitHub Desktop.
Getting started with creating your own custom shortcodes in WordPress
function output_my_custom_field($atts, $content = null) {
$a = shortcode_atts([
"field" => "_my_custom_field_name",
"title" => "Videodauer:",
"class" => "custom_field_value_wrapper",
], $atts);
$return_string = '';
if(get_post_meta(get_the_ID(), $a["field"], true) != '') {
$my_custom_field_value = get_post_meta(get_the_ID(), $a["field"], true);
$return_string = '<div class="' . $a["class"] . '"><strong>' . $a["title"] . '</strong> ' . $my_custom_field_value . '</div>';
}
return $return_string;
}
add_shortcode('my_custom_field_value', 'output_my_custom_field');
/* Anwendung:
[my_custom_field_value field="_my_custom_field_name"]
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment