0

How can I add the 'Post Type' (Blog, Page) before the titles of search results?

Post type: Title

Example: Search results for "Potato"

Blog: How to cook the perfect potato!

Page: Sweet Potatoes

etc..

Thanks!

Contents of my index.php

<?php
        if ( have_posts() ) :
            while ( have_posts() ) : the_post();
                $post_format = et_pb_post_format(); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class( 'et_pb_post' ); ?>>

            <?php
                $thumb = '';

                $width = (int) apply_filters( 'et_pb_index_blog_image_width', 1080 );

                $height    = (int) apply_filters( 'et_pb_index_blog_image_height', 675 );
                $classtext = 'et_pb_post_main_image';
                $titletext = get_the_title();
                $alttext   = get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true );
                $thumbnail = get_thumbnail( $width, $height, $classtext, $alttext, $titletext, false, 'Blogimage' );
                $thumb     = $thumbnail["thumb"];

                et_divi_post_format_content();

                if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) {
                    if ( 'video' === $post_format && false !== ( $first_video = et_get_first_video() ) ) :
                        printf(
                            '<div class="et_main_video_container">
                                %1$s
                            </div>',
                            et_core_esc_previously( $first_video )
                        );
                    elseif ( ! in_array( $post_format, array( 'gallery' ) ) && 'on' === et_get_option( 'divi_thumbnails_index', 'on' ) && '' !== $thumb ) : ?>
                        <a class="entry-featured-image-url" href="<?php the_permalink(); ?>">
                            <?php print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height ); ?>
                        </a>
                <?php
                    elseif ( 'gallery' === $post_format ) :
                        et_pb_gallery_images();
                    endif;
                } ?>

            <?php if ( ! in_array( $post_format, array( 'link', 'audio', 'quote' ) ) ) : ?>
                <?php if ( ! in_array( $post_format, array( 'link', 'audio' ) ) ) : ?>
                    <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                <?php endif; ?>

                <?php
                    et_divi_post_meta();

                    if ( 'on' !== et_get_option( 'divi_blog_style', 'false' ) || ( is_search() && ( 'on' === get_post_meta( get_the_ID(), '_et_pb_use_builder', true ) ) ) ) {
                        truncate_post( 270 );
                    } else {
                        the_content();
                    }
                ?>
            <?php endif; ?>

                </article> <!-- .et_pb_post -->
        <?php
                endwhile;

                if ( function_exists( 'wp_pagenavi' ) )
                    wp_pagenavi();
                else
                    get_template_part( 'includes/navigation', 'index' );
            else :
                get_template_part( 'includes/no-results', 'index' );
            endif;
        ?>
5
  • I guess "get_post_type" is what you are looking for developer.wordpress.org/reference/functions/get_post_type Jul 29, 2021 at 21:18
  • Thank you. Being a php noob, how would I implement this within the search results? I assume some sort of if statment is_search() ? Sorry my php knowledge is limited! Jul 30, 2021 at 1:20
  • You may have to share the relevant part of the search.php file in order to get a clear answer. probably something similar to: function echo_post_type( $post = null ) { $post = get_post( $post ); if ( $post ) { echo $post->post_type; } } echo_post_type( $post); Jul 30, 2021 at 12:04
  • Thanks Julian I have discovered that the divi theme I am using doesn't have a search.php but I believe the index.php contains the relevant code. After some googling, I should be able to edit this by making a copy and renaming it search.php in my child theme? I have updated my post with the code. Thank you so much for your input. Aug 1, 2021 at 2:07
  • The specifics of how you must/are able to implement a child theme vary from theme to theme, and even more so for massive theme frameworks like Divi. You will need to consult Divi's documentation and/or support channels for the proper approach. Another perhaps easier possibility might be to use a the_title filter hook and add the post type if the ! is_admin() && is_search() predicates are true, assuming Divi is compatible with those hooks and conditions.
    – bosco
    Aug 2, 2021 at 22:47

1 Answer 1

1

I solved this by copying the divi index.php and placing it in my child theme as search.php

I then found where the title and altered it with the below code:

<h2 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php $post_type = get_post_type( $post->ID );
echo $post_type . ": ";
the_title(); ?>
</a>
</h2>

Probably not the correct way to do it but the result was perfect. My search results now have:

Blog: 'Blog title'

Page: 'Page title'

etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.