This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Display names of post types

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Display names of post types

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #49089
    christianbachchristianbach
    Participant

    Hi there,

    Is there a way to display which post type a search result is?

    I have a couple of Custom Post Types – and it would be nice if the search result was displayed like this:

    [Name of Post Type/Custom Post Type]
    [Post Title]
    [Excerpt]

    Example:
    BOOK
    Dear Evan Hansen
    [Excerpt]

    or
    BLOG
    Winners announced
    [Excerpt]

    Best,
    Christian

    #49094
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi Christian,

    Yes – you have two options:

    1. Using results grouping by post type – this is simple, but the ordering will change because of the groups.
    2. Using a bit of custom code – this might be closer to what you are looking for.

    #49098
    christianbachchristianbach
    Participant

    Thnak you – for both the solution and the quick reply.

    The custom code is absolutely the direction for me.
    But: Is there a way I can show the post type as a class all to it self? Which will give me the option to style it diffently than title or content?
    (I am inexperienced with php-coding – but quite okay in css’ing something to look like I want i to 🙂

    Best,
    Christian

    #49099
    Ernest MarcinkoErnest Marcinko
    Keymaster

    Hi Christian,

    Sure! With a tiny bit of modification it’s possible to add a CSS class:

    function asp_show_the_post_type_content( $results ) {
    	foreach ($results as $k=>&$r) {
    		if ( isset($r->post_type) ) {
    			// Modify the post title
    			$post_type_obj = get_post_type_object( $r->post_type );
    			$r->content = "<div class='$r->post_type'>" . $post_type_obj->labels->singular_name . '</div> - ' . $r->content;
    		}
    	}
    
    	return $results;
    }

    With this, the post type is wrapped in a div with the post type class name, which you can reference by:

    .asp_r .post_type_slug {
    
    }
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.