Wordpress 结果中自定义帖子类型的按字母顺序过滤

我正在处理自定义帖子类型存档,我想添加 A-Z 过滤菜单。 我设法按照这个线程(Create letterical Pagination in wordpress)让它正常工作,但我不明白我的初始页面(/exposants)如何显示所有结果。

<div class="exposant__filter" id="exposants">

        <a href="/exposants/#exposants"><?php _e('Tout', 'festival'); ?></a>


    <?php 

        $posts = get_posts(array(

            'numberposts' => -1,

            'post_type' => 'exposant',

            'orderby' => 'title',

            'order' => 'ASC',

        )); 

        

        $firstLetters = array();

        foreach($posts as $post) {

            $title = $post->post_title;

            $startingLetter = substr($title, 0, 1);

            $dupeFirstLetters[] = $startingLetter;

            $firstLetters = array_unique($dupeFirstLetters);

            sort($firstLetters);

        }

        foreach($firstLetters as $letter) {

            echo "<a href=\"?lettre=$letter\">$letter</a>";

        }

        if(!empty($_GET['lettre'])) {

                $letter = $_GET['lettre'];

        }

        else {

            $letter = $firstLetters[0];

        } ?>

    </div>      


    <?php

    $exposantsArchive = new WP_Query(array(

        'posts_per_page' => -1,

        'post_type' => 'exposant',

        'orderby' => 'title',

        'order' => 'ASC',

    )); ?>


    <div class="row row--2col u-m-top--8">


        <?php   

        while($exposantsArchive->have_posts()) {

            $exposantsArchive->the_post(); 

            

            $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));

            

            $logo = get_field('logo');

            

            if($first_letter == strtoupper($letter)) { ?>

            <div class="exposant__thumb col--padding-right">

                <div class="exposant__logo">

                    <img src="<?php echo $logo['url'] ?>" alt="<?php echo $logo['alt'] ?>">

                </div>



我想我必须替换 else {$letter = $firstLetters[0];} 但我不知道用什么替换。


预先感谢您的宝贵支持。


慕桂英546537
浏览 62回答 1
1回答

慕少森

如果我正确理解了问题,这里是您的代码的稍微优化的版本。为了清楚起见,我冒昧地尽可能地将你的 php 和 html 解耦。它没有经过测试,可能需要一些调整,但总的来说我希望这能成功!在您的functions.php文件中,添加以下内容,这将允许您按首字母过滤数据库查询:add_filter( 'posts_where', 'tomtom_posts_where', 10, 2 );function tomtom_posts_where( $args, $wp_query_obj ){&nbsp; &nbsp; global $wpdb;&nbsp; &nbsp; $starts_with = $wp_query->get( 'starts_with' )&nbsp; &nbsp; if ( $starts_with ) {&nbsp; &nbsp; &nbsp; &nbsp; $args .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $starts_with ) ) . '%\'';&nbsp; &nbsp; }&nbsp; &nbsp; return $where;}然后:<?php/**&nbsp;* Outputs an alphabetic paginator.&nbsp;*&nbsp;* @return void&nbsp;*/function tomtom_output_alphabetic_paginator() {&nbsp; &nbsp; $posts = get_posts( array(&nbsp; &nbsp; &nbsp; &nbsp; 'numberposts' => -1,&nbsp; &nbsp; &nbsp; &nbsp; 'post_type'&nbsp; &nbsp;=> 'exposant',&nbsp; &nbsp; &nbsp; &nbsp; 'orderby'&nbsp; &nbsp; &nbsp;=> 'title',&nbsp; &nbsp; &nbsp; &nbsp; 'order'&nbsp; &nbsp; &nbsp; &nbsp;=> 'ASC',&nbsp; &nbsp; ) );&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $firstLetters = array();&nbsp; &nbsp; foreach ( $posts as $post ) {&nbsp; &nbsp; &nbsp; &nbsp; $title&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = $post->post_title;&nbsp; &nbsp; &nbsp; &nbsp; $startingLetter&nbsp; &nbsp; &nbsp;= substr( $title, 0, 1 );&nbsp; &nbsp; &nbsp; &nbsp; $dupeFirstLetters[] = $startingLetter;&nbsp; &nbsp; }&nbsp; &nbsp; $firstLetters = array_unique( $dupeFirstLetters );&nbsp; &nbsp; sort( $firstLetters );&nbsp; &nbsp; echo "<a href=\"/exposants/#exposants\">" . __('Tout', 'festival') . "</a>";&nbsp; &nbsp; foreach ( $firstLetters as $letter ) {&nbsp; &nbsp; &nbsp; &nbsp; echo "<a href=\"?lettre={$letter}\">{$letter}</a>";&nbsp; &nbsp; }}/**&nbsp;* Gets an array of exposant custom post types.&nbsp;*&nbsp;* @return array&nbsp;*/function tomtom_get_exposants() {&nbsp; &nbsp; $args = array(&nbsp; &nbsp; &nbsp; &nbsp; 'posts_per_page' => -1,&nbsp; &nbsp; &nbsp; &nbsp; 'post_type' => 'exposant',&nbsp; &nbsp; &nbsp; &nbsp; 'orderby' => 'title',&nbsp; &nbsp; &nbsp; &nbsp; 'order' => 'ASC',&nbsp; &nbsp; );&nbsp; &nbsp; if ( ! empty( $_GET['lettre'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $args['starts_with'] = $_GET['lettre'];&nbsp; &nbsp; }&nbsp; &nbsp; return new WP_Query( $args );}/**&nbsp;* Adds fields to an exposant custom post type object.&nbsp;*&nbsp;* @param WP_Post $exposant Exposant custom post type.&nbsp;* @return void&nbsp;*/function tomtom_hydrate_exposant( &$exposant ) {&nbsp; &nbsp; $exposant->logo&nbsp; &nbsp; &nbsp; &nbsp; = get_field( 'logo', $exposant->ID );&nbsp; &nbsp; $exposant->related&nbsp; &nbsp; &nbsp;= get_field( 'pieces_liees', $exposant->ID );&nbsp; &nbsp; $exposant->description = get_field( 'description', $exposant->ID );}/**&nbsp;* Outputs links related to exposant custom post type object.&nbsp;*&nbsp;* @return void&nbsp;*/function tomtom_output_related( $exposant ) {&nbsp; &nbsp; if ( ! isset( $exposant->related ) ) {&nbsp; &nbsp; &nbsp; &nbsp; tomtom_hydrate_exposant( $exposant );&nbsp; &nbsp; }&nbsp; &nbsp; foreach ( $exposant->related as $k => $category ) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if ( $k ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '/';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo "<a href=\"" . get_the_permalink( $category ) . "\">" . get_the_title( $category ) . "</a>";&nbsp; &nbsp; }}?><div class="exposant__filter" id="exposants">&nbsp; &nbsp;&nbsp; &nbsp; <?php tomtom_output_alphabetic_paginator(); ?></div><?php $exposantsArchive = tomtom_get_exposants(); ?><div class="row row--2col u-m-top--8"><?php foreach ( $exposantsArchive as $exposant ): ?>&nbsp; &nbsp; <?php tomtom_hydrate_exposant( $exposant ); ?>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class="exposant__thumb col--padding-right">&nbsp; &nbsp; &nbsp; &nbsp; <div class="exposant__logo">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="<?php echo $exposant->logo['url'] ?>" alt="<?php echo $exposant->logo['alt'] ?>">&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <div class="exposant__thumb-content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h2 class="heading--as-h4 heading--no-margin"><?php echo $exposant->post_title; ?></h2>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php if ( $exposant->related ): ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="exposant__categories">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php tomtom_output_related( $exposant->related ); ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php endif; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p class="exposant-thumb__excerpt"><?php echo wp_trim_words( $exposant->description, 16, '...' ); ?></p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="<?php get_permalink( $exposant->ID ); ?>" class="btn"><?php _e( 'Plus de détails', 'festival' ); ?></a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div><?php endforeach; ?></div>
打开App,查看更多内容
随时随地看视频慕课网APP