We're merging with GenerateBlocks! Learn more here.

[Support request] Is it possible to show the post type in the list?

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Is it possible to show the post type in the list?

Viewing 15 posts - 1 through 15 (of 21 total)
  • Author
    Posts
  • #28988
    Joon
    Participant

    So I was wondering if it is possible to show the post type in the list? I know you can show the category/taxonomy so I guess it would be possible to somehow show the post type?

    I have a CPT called “Events” and they are showing in the same list as “News”. But my goal would be to achieve something like this
    (badges with post type name over the image):
    https://pasteboard.co/JWlk8Y2.png

    Do you think this would be possible?

    #28993
    elvin
    Moderator

    Hi there,

    Do you want it applied on a specific list only?

    If yes, try this out:

    Add this PHP snippet.

    add_action('wpsp_inside_image_container',function($settings){
    	if(1821 === (int) $settings['list_id']){
    		$post_label = get_post_type_object( get_post_type( get_the_ID() ) )->labels->name ;
    		echo '<div class="wpsp-post-type-name">'.$post_label.'</div>';
    	}
    });

    Then add this CSS for the formatting of the label.

    section#wpsp-1821 .wpsp-post-type-name {
        position: absolute;
        bottom:10px;
        right: 10px;
        color: white;
        background-color: black;
        padding: 5px;
    }
    
    section#wpsp-1821 .wp-show-posts-image {
        position: relative;
    }

    And change 1821 to the WPSP id you want this list applied to.

    #29002
    Joon
    Participant

    That’s perfect. Thank you! 🙂

    #29004
    Joon
    Participant

    Ah one thing I didn’t realize… Would it be possible to link the CPT to the CPT archive?

    #29027
    elvin
    Moderator

    Let’s modify the code a bit to add the anchor tag:

    add_action('wpsp_inside_image_container',function($settings){
    	if(1821 === (int) $settings['list_id']){
    		$post_label = get_post_type_object( get_post_type( get_the_ID() ) )->labels->name ;
    		$cpt_archive_link = get_post_type_archive_link( get_post_type( get_the_ID() ) );
    		echo '<div class="wpsp-post-type-name"><a href="'.$cpt_archive_link.'">'.$post_label.'</a></div>';
    	}
    });

    Not sure how well this works. Let us know.

    #29050
    Joon
    Participant

    It works! Thank you. I know this may be getting a bit out of hand but I ran into another issue with the list.

    I am listing both traditional posts and events (CPT) in the same list, but WPSP shows the publishing date of the post, as it should. But because I have an event, it would be more logical to show the date of the event instead.

    I already achieved a working code, so I am really close! The code checks if its the CPT with Events, and displays the date in the post like this: https://pasteboard.co/JWwd44z.png
    But would it be possible to replace the post-date with this code?

    if ( 'tribe_events' == get_post_type() ) {
    		echo '<div class="wpsp-post-type-name"><a href="'.$cpt_archive_link.'">'.$post_label.'</a></div>';	
    		$startDate = strtotime( get_post_meta( get_the_ID(), '_EventStartDate', true ));			
    		$startDate = date( 'j.n.Y', $startDate );
    		echo 'Event date: ',$startDate;
                    }
    #29052
    Joon
    Participant

    So I already have the script checking if it’s a CPT, and I already have the custom field there. I just need to get the postdate replaced. Like this would be ideal! https://pasteboard.co/JWwgCaK.png

    #29102
    elvin
    Moderator

    Use the wpsp_date_output filter for the date.
    https://github.com/tomusborne/wp-show-posts/blob/35e410d7800273fc66f211c0f80d553e95d17f83/inc/functions.php#L91

    Try this out.

    add_filter('wpsp_date_output',function(){
        if ( 'tribe_events' == get_post_type() ) { 
    		$cpt_archive_link = get_post_type_archive_link( get_post_type( get_the_ID() ) );
    
            $startDate = strtotime( get_post_meta( get_the_ID(), '_EventStartDate', true ));			
    		$startDate = date( 'j.n.Y', $startDate );
    
            return sprintf('<div class="wpsp-post-type-name">
                            Event date: %1$s
                        </div>',
                        $startDate
                    );
        }
    });
    #29120
    Joon
    Participant

    Thanks for the help again!

    So the wpsp_date_output filter was exactly what I was looking for.

    I did some tests with the code above, and realized that using the _EventStartDate custom field was not the best way to pull the event info from the plugin. They had a function for doing it better (https://docs.theeventscalendar.com/reference/functions/tribe_events_event_schedule_details/).

    I ended up using the code below. Still having some problems. I think it has something to do with the wpsp_date_output? It almost works but with this code it first shows the post date, and then the event details… (https://pasteboard.co/JWXddSF.jpg)

    How would I get rid of the postdate?

    add_filter('wpsp_date_output',function(){
        if ( 'tribe_events' == get_post_type() ) { 
            return tribe_events_event_schedule_details($null, $before = 'Event date: ');
    	}
    });
    
    #29122
    Joon
    Participant

    And I just realized that the wpsp_date_output filter is tied to what date is used in the sorting also?

    So my code seems to make the event date be prioritized over the post date when sorting by date.

    #29125
    elvin
    Moderator

    wpsp_date_output only changes the date being presented on the front end. It doesn’t meddle with the query. (sorting/ “orderby”)

    How would I get rid of the postdate?

    Can you try this?

    add_filter('wpsp_date_output',function($output){
        if ( 'tribe_events' == get_post_type() ) { 
            $output = tribe_events_event_schedule_details($null, $before = 'Event date: ');
        }
        return $output;
    },15,1);
    #29127
    Joon
    Participant

    Ok. So I guess there is something else happening with The Events Calendar plugin that leads to WPSP using the eventdate for sorting instead of the publishing date of the post.

    I tried the code you posted but still the date shows up twice? Do you think this could be a problem with their function?
    https://pji.eitcon.com/

    #29165
    elvin
    Moderator

    It’s weird that it keeps the <time> elements.

    I’ve tested this on my end and it completely removes the time string elements on mine.

    Can you check your snippets if you have other add_filter codes that use wpsp_date_output?

    Let us know.

    #29180
    Joon
    Participant

    Yeah something weird is clearly going on. I tested it out on a blank demosite with only WPSP and The Events Calendar and the code works perfectly!

    So clearly something on my end messing with the outcome. I’ll look into it and let you know.

    But thanks for the help again! 🙂

    #29182
    Joon
    Participant

    Ok you were right. I had a code in code snippets affecting the end result.

    I had this code that removes the link from the date. Is it possible to still remove the link?

    add_filter( 'wpsp_date_output', 'tu_wpsp_remove_date_link' );
    function tu_wpsp_remove_date_link() {
        $time_string = '<time class="wp-show-posts-entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    
        if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
            $time_string .= '<time class="wp-show-posts-updated" datetime="%3$s" itemprop="dateModified">%4$s</time>';
        }
    
        $time_string = sprintf( $time_string,
            esc_attr( get_the_date( 'c' ) ),
            esc_html( get_the_date() ),
            esc_attr( get_the_modified_date( 'c' ) ),
            esc_html( get_the_modified_date() )
        );
    
        printf(
            '<span class="wp-show-posts-posted-on wp-show-posts-meta">%1$s</span>',
            $time_string
        );
    }
Viewing 15 posts - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.