Support » Plugin: WordPress Popular Posts » Display Custom Post Type in Output

  • Resolved robertallen

    (@robertallen)


    Is there a way to display the Custom Post Type in the actual output?

    Here’s my code but it doesn’t work:

    <?php
                            $most_popular_args = array(
                                'post_type' => 'tl-article, video, press-release, press-mention, white-papers, analyst-report, fact-sheet, case-study, customer-quotes, infographics, partnerships, on_demand_webcasts, quarterly_release, integrations, podcasts',
                                'limit' => 3,
                                'range' => 'last30days',
                                'stats_views' => 0,
                                'stats_taxonomy' => 1,
                                'post_html' => '<li><h3 class="mt-0"><a href="{url}">{text_title}</a><br>{post_type}</li></h3>'
                            );
    
                            wpp_get_mostpopular( $most_popular_args );
                            ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Héctor Cabrera

    (@hcabrera)

    Hi @robertallen,

    The built-in Content Tags don’t include {post_type} -as you’re probably already aware of- but it’s fairly easy to add a new custom Content Tag for this:

    /**
     * Parses custom content tags in WordPress Popular Posts.
     *
     * @param  string  $html    The HTML markup from the plugin.
     * @param  integer $post_id The post/page ID.
     * @return string
     */
    function wpp_parse_tags_in_popular_posts( $html, $post_id ){
    
        // Replace custom content tag {post_type} with the actual post type
        if ( false !== strpos($html, '{post_type}') ) {
            // Get post type
            $post_type = get_post_type( $post_id );
    
            if ( $post_type ) {
                $html = str_replace( '{post_type}', $post_type, $html );
            }
        }
    
        return $html;
    
    }
    add_filter( "wpp_parse_custom_content_tags", "wpp_parse_tags_in_popular_posts", 10, 2 );

    Awesome, thank you. I just need to figure out how to echo the name of the post type instead of the post type slug itself. Appreciate it.

    /**
    * Parses custom content tags in WordPress Popular Posts.
    *
    * @param string $html The HTML markup from the plugin.
    * @param integer $post_id The post/page ID.
    * @return string
    */
    function wpp_parse_tags_in_popular_posts( $html, $post_id ){

    // Replace custom content tag {post_type} with the actual post type
    if ( false !== strpos($html, ‘{post_type}’) ) {
    // Get post type
    // $post_type_label = $post_type->labels->singular_name;
    $post_type = get_post_type( $post_id );
    $post_type_labels = get_post_type_object( $post_type );
    $post_type_label = $post_type_labels->labels->singular_name;

    if ( $post_type_label ) {
    $html = str_replace( ‘{post_type}’, $post_type_label, $html );
    }
    }

    return $html;

    }
    add_filter( “wpp_parse_custom_content_tags”, “wpp_parse_tags_in_popular_posts”, 10, 2 );

    Resolved, thank you very much.

    Plugin Author Héctor Cabrera

    (@hcabrera)

    Ah, my bad. Glad to know you were able to figure it out, and thanks for sharing the solution! I’m sure future readers will appreciate that as well.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display Custom Post Type in Output’ is closed to new replies.