Skip to content

Instantly share code, notes, and snippets.

@LaxusCroco
Last active August 14, 2023 20:03
Show Gist options
  • Save LaxusCroco/ce8e19c3cb4ef24c3b2829e30306f124 to your computer and use it in GitHub Desktop.
Save LaxusCroco/ce8e19c3cb4ef24c3b2829e30306f124 to your computer and use it in GitHub Desktop.
Shortcode 'get_post_type_name' to get post type name/slug on single post pages and inside post listings. Accepts the parameter 'display' with 2 values = "name" and "slug". "name" returns CPT name, "slug" returns CPT slug. Example: [get_post_type_name display="name"]
<?php
add_shortcode( 'get_post_type_name', 'get_post_type_name' );
function get_post_type_name( $args ) {
$display = $args['display'];
if ( ! $display ) {
return 'No data found';
}
$post_id = jet_engine()->listings->data->get_current_object_id();
$result = '';
switch ( $display ) {
case "slug":
$post_type_slug = get_post_type( $post_id );
If ( $post_type_slug ) {
$result = $post_type_slug;
}
break;
case "name":
$post_type_name = get_post_type_object( get_post_type( $post_id ) );
if ($post_type_name) {
$result = esc_html($post_type_name->labels->name);
}
break;
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment