admin管理员组

文章数量:1292618

I've been struggling to make it work. This code works and returns post_type title and post titles inside it. I found it here

But i need to have some sort of formatting and excerpt. Currently I cannot make it work with get_post_type_object . I'd very much like to use regular search loop with while and get custom templates for post types with get_template_part()

<?php

    $types = array( 'product', 'post', 'page', 'useful' );
    $posts_titles = [];
    while ( have_posts() ) {
        the_post();

        $type = $post->post_type;
        if ( !isset($posts_titles[$type]) )
            $posts_titles[$type] = [];

        $posts_titles[$type][] = get_the_title();
    }
    rewind_posts();

    foreach( $types as $type ) : 

        if ( !isset($posts_titles[$type]) )
            continue;

        ?>
        <div class="search_products_wrap search-results-post-type-item post-type-<?php echo $type ?>">
            <header class="post-type-header">
            <div class="search-block-title">
                <h2 class="post-type-title">
                    <?php
                        $post_type_obj = get_post_type_object( $type );
                        echo $post_type_obj->labels->name;
                    ?>
                </h2>
            </div>
                
            </header>
            <div class="search-results-list">

                <?php foreach( $posts_titles[$type] as $title ) : ?>             
                    <div class="search-results-list-item">
                        <h3 class="entry-title"><?php echo htmlspecialchars($title); ?></h3>
                    </div>

                <?php endforeach; ?>

            </div>
        </div>

    <?php endforeach; 
?>

This code kind of works but it lacks proper implementation. It returns post type titles when there are no results.

if( have_posts() ){
    $types = array('product', 'post', 'page', 'useful');
    foreach( $types as $type ){
        echo '<div class="clearfix post-type-wrap '. $type .'">';
        if ( $type == 'post' ) {
          $blocktitle = __( 'Posts', 'theme' );
        } elseif ( $type == 'page' ) {
          $blocktitle = __( 'Pages', 'theme' );
        } elseif ( $type == 'product' ) {
          $blocktitle = __( 'Products', 'theme' );
        } else {
          $blocktitle = __( 'Else', 'theme' );
        }
      ?>
        <h3><?php echo $blocktitle; ?></h3>
        <?php
      
        while( have_posts() ){
       
            the_post();
            if( $type == get_post_type() ){
                  get_template_part( 'assets/search/content', $type );
            }
        }
        rewind_posts();
        echo '</div>';
    }
}

Can someone guide me to the right direction how i can achieve it?

I've been struggling to make it work. This code works and returns post_type title and post titles inside it. I found it here

But i need to have some sort of formatting and excerpt. Currently I cannot make it work with get_post_type_object . I'd very much like to use regular search loop with while and get custom templates for post types with get_template_part()

<?php

    $types = array( 'product', 'post', 'page', 'useful' );
    $posts_titles = [];
    while ( have_posts() ) {
        the_post();

        $type = $post->post_type;
        if ( !isset($posts_titles[$type]) )
            $posts_titles[$type] = [];

        $posts_titles[$type][] = get_the_title();
    }
    rewind_posts();

    foreach( $types as $type ) : 

        if ( !isset($posts_titles[$type]) )
            continue;

        ?>
        <div class="search_products_wrap search-results-post-type-item post-type-<?php echo $type ?>">
            <header class="post-type-header">
            <div class="search-block-title">
                <h2 class="post-type-title">
                    <?php
                        $post_type_obj = get_post_type_object( $type );
                        echo $post_type_obj->labels->name;
                    ?>
                </h2>
            </div>
                
            </header>
            <div class="search-results-list">

                <?php foreach( $posts_titles[$type] as $title ) : ?>             
                    <div class="search-results-list-item">
                        <h3 class="entry-title"><?php echo htmlspecialchars($title); ?></h3>
                    </div>

                <?php endforeach; ?>

            </div>
        </div>

    <?php endforeach; 
?>

This code kind of works but it lacks proper implementation. It returns post type titles when there are no results.

if( have_posts() ){
    $types = array('product', 'post', 'page', 'useful');
    foreach( $types as $type ){
        echo '<div class="clearfix post-type-wrap '. $type .'">';
        if ( $type == 'post' ) {
          $blocktitle = __( 'Posts', 'theme' );
        } elseif ( $type == 'page' ) {
          $blocktitle = __( 'Pages', 'theme' );
        } elseif ( $type == 'product' ) {
          $blocktitle = __( 'Products', 'theme' );
        } else {
          $blocktitle = __( 'Else', 'theme' );
        }
      ?>
        <h3><?php echo $blocktitle; ?></h3>
        <?php
      
        while( have_posts() ){
       
            the_post();
            if( $type == get_post_type() ){
                  get_template_part( 'assets/search/content', $type );
            }
        }
        rewind_posts();
        echo '</div>';
    }
}

Can someone guide me to the right direction how i can achieve it?

Share Improve this question edited Sep 13, 2022 at 8:06 m2itz asked Sep 9, 2022 at 11:47 m2itzm2itz 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Solution found by counting! It counts if there are any posts and if not then skips.

<?php if ( have_posts() ) : ?>
    <?php
    $types = array('product' => 0, 'post' => 0, 'page' => 0);
    while ( have_posts() ) {
        the_post();
        if (array_key_exists(get_post_type(), $types)) {
            $types[get_post_type()]++;
        }
    }
    ?>
    <?php foreach ($types AS $type => $nb_of_type) : ?>
        <?php if ($nb_of_type > 0) : ?>
            <div class="post-type-wrap <?php echo $type; ?>">
                <div class="search-block-title"><h2><?php echo $blocktitle; ?></h2></div>
                
                    <?php while ( have_posts() ) : the_post(); ?>
                        <?php if ( get_post_type() === $type ) : ?>
                            <?php get_template_part('assets/search/content', $type); ?>
                        <?php endif; ?>
                    <?php endwhile; ?>                       

            </div>
        <?php endif; ?>
    <?php endforeach; ?>
    <?php else :
            _e( 'Sorry, no posts were found.' );
        ?>
<?php endif; ?>

本文标签: theme developmentWordpress search results grouped by post type