Ioria
Ioria

Reputation: 1

Print "custom label" for indicating type of post in the front end for Wordpress

I have a blog with different custom post types ("books", "interviews", "recipes", "events", etc...). those are all appearing in the home page with same format like a grid. I would like to print in the front end a "label" customized possibly, representing the kind/content of post.

For example: if the post is a CPT "Book", I want to show in the grid cell "looking for a book?" if the post is a CPT "Recipe", I want to show on same position, for specific post "hungry?" etc...

Can you maybe help me in this? I guess I need some PHP code and set it with Elementor, but I am not a developer... :(

Thanks for any help. Mario.

I have been asked in comment to put a screenshot. this is a fake grid taken from internet (I know, ugly layout), presenting in descending order by date all posts, very different in domain (different custom post types), which I am able to do it. What I need is, depending by Post Type, to add a slogan like "watch the movie" or "hungry?" or "Interview with...", a static string totally dependent by the type of CPT. fake sample from internet

Further integration to explain the context. See the current home page of my site: click here You see two "posts" in a grid (3 columns, published with "post" widget in elementor and a custom skin. This custom skin is linked to a "loop" template created with "ele custom skin". As you see by the pic, you have one post which is a recipe (custom post type "Recipe") and one is a book (custom post type is book). But here I can eventually find also a standard post. Now, when you see the "red dot", I would like to put a word, which is directly dependent by the post type:


as a sample I have in this link click here for each loop in the grid, called using "shortcode" widget

[helloworld]

and coded in snippets plugin following portion of php code

function HelloWorldShortcode() {
    return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');

Upvotes: 0

Views: 71

Answers (1)

Vijay Hardaha
Vijay Hardaha

Reputation: 2584

Here is the code, from this code shortcode will be [vkh_display_post_tagline]


/**
 * Custom shortcode to display the tagline by the post type.
 */
/**
 * Custom shortcode to display the tagline by the post type.
 */
function vkh_display_tagline_by_post_type() {
    global $post;

    if ( ! $post || empty( $post ) ) {
        return;
    }

    // Uncomment the next line for debugging and to know if we're getting post type of not.
    // return $post->post_type;

    $heading = '';

    switch ( $post->post_type ) {
        case 'recipe':
            $heading = __( 'Hungry?', 'your-text-domain' );
            break;

        case 'book':
            $heading = __( 'Our book reviews', 'your-text-domain' );
            break;

        // Add more cases following above examples.

        default:
            // If you have to assign anything default when a case doesn't match
            // then write that here.
            $heading = '';
            break;
    }

    // When heading is not empty return heading within the html tag.
    if ( ! empty( $heading ) ) {
        return '<h4 class="post-tagline">' . esc_html( $heading ) . '</h4>';
    }
}
add_shortcode( 'vkh_display_post_tagline', 'vkh_display_tagline_by_post_type' );

Upvotes: 0

Related Questions