【问题标题】:Alphabetical filtering of custom post type in Wordpress results在 Wordpress 结果中按字母顺序过滤自定义帖子类型
【发布时间】:2020-12-24 05:41:45
【问题描述】:

我正在处理自定义帖子类型存档,我想添加一个 A-Z 过滤菜单。 我设法在这个线程 (Create alphabetical 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>
                <div class="exposant__thumb-content">
                    <h2 class="heading--as-h4 heading--no-margin"><?php the_title(); ?></h2>
                    <?php 
                    $related = get_field('pieces_liees');
                    if($related) {
                        echo '<p class="exposant__categories">';
                        foreach ($related as $k=>$category) { 
                            if($k) echo '/';
                            ?>
                            <a href="<?php echo get_the_permalink($category); ?>"><?php echo get_the_title($category); ?></a>
                        <?php }
                     }
                ?></p>
                    <p class="exposant-thumb__excerpt"><?php echo wp_trim_words( get_field('description'), 16, '...' ); ?></p>
                    <a href="<?php the_permalink(); ?>" class="btn"><?php _e('Plus de détails', 'festival'); ?></a>
                </div>
            </div>

            <?php
        }
    } 
        wp_reset_postdata() ?>
    </div>

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

提前感谢您的宝贵支持。

编辑!

这是完美运行的解决方案!

所以在functions.php中

<?php
add_filter( 'posts_where', 'tomtom_posts_where', 10, 2 );
function tomtom_posts_where( $args, $wp_query_obj )
{
    global $wpdb;
    $starts_with = $wp_query_obj->get( 'starts_with' );
    if ( $starts_with ) {
        $args .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $starts_with ) ) . '%\'';
    }
    return $args;
}

这是页面

<?php

    /**
     * Outputs an alphabetic paginator.
     *
     * @return void
     */
    function tomtom_output_alphabetic_paginator() {
            $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 );

            echo "<a href=\"/exposants/#exposants\">" . __('Tout', 'festival') . "</a>";
            foreach ( $firstLetters as $letter ) {
                    echo "<a href=\"?lettre={$letter}#exposants\">{$letter}</a>";
            }
    }

    /**
     * Gets an array of exposant custom post types.
     *
     * @return array
     */
    function tomtom_get_exposants() {
            $args = array(
                    'posts_per_page' => -1,
                    'post_type' => 'exposant',
                    'orderby' => 'title',
                    'order' => 'ASC',
            );
            if ( !empty( $_GET['lettre'] ) ) {
                    $args['starts_with'] = $_GET['lettre'];
            }
            $wp_query = new WP_Query( $args );
            return $wp_query->get_posts();
    }       

    /**
     * Outputs links related to exposant custom post type object.
     *
     * @return void
     */
    function tomtom_output_related( $related ) {
            foreach ( $related as $k => $category ) { 
                    if ( $k ) {
                            echo ' / ';
                    }
                    echo "<a href=\"" . get_the_permalink( $category ) . "\">" . get_the_title( $category ) . "</a>";
            }
    }

    ?>
    <div class="exposant__filter" id="exposants">   
            <?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 ): ?>
            <?php $logo = get_field('logo', $exposant->ID); ?>
            <?php $related = get_field('pieces_liees', $exposant->ID); ?>
            <?php $description = get_field('description', $exposant->ID); ?>

            <div class="exposant__thumb col--padding-right">
                    <div class="exposant__logo">
                            <img src="<?php echo $logo['url'] ?>" alt="<?php echo $logo['alt'] ?>">
                    </div>
                    <div class="exposant__thumb-content">
                            <h2 class="heading--as-h4 heading--no-margin"><?php echo $exposant->post_title; ?></h2>
                            <?php if ( $related ): ?>
                            <p class="exposant__categories">
                                    <?php tomtom_output_related( $related ); ?>
                            </p>
                            <?php endif; ?>
                            <p class="exposant-thumb__excerpt"><?php echo wp_trim_words( $description, 20, '...' ); ?></p>
                            <a href="<?php get_permalink( $exposant->ID ); ?>" class="btn"><?php _e( 'Plus de détails', 'festival' ); ?></a>
                    </div>
            </div>
    <?php endforeach; ?>
    </div>

【问题讨论】:

    标签: php wordpress search filtering alphabetical


    【解决方案1】:

    如果我正确理解了这个问题,这里有一个稍微优化过的代码版本。为了清楚起见,我冒昧地尽可能多地将您的 php 和您的 html 解耦。

    它没有经过测试,可能需要一些调整,但总的来说我希望这能解决问题!

    在你的functions.php文件中,添加这个,这将允许你通过首字母过滤你的数据库查询:

    add_filter( 'posts_where', 'tomtom_posts_where', 10, 2 );
    function tomtom_posts_where( $args, $wp_query_obj )
    {
        global $wpdb;
        $starts_with = $wp_query->get( 'starts_with' )
        if ( $starts_with ) {
            $args .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $starts_with ) ) . '%\'';
        }
        return $where;
    }
    

    然后:

    <?php
    
    /**
     * Outputs an alphabetic paginator.
     *
     * @return void
     */
    function tomtom_output_alphabetic_paginator() {
        $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 );
    
        echo "<a href=\"/exposants/#exposants\">" . __('Tout', 'festival') . "</a>";
        foreach ( $firstLetters as $letter ) {
            echo "<a href=\"?lettre={$letter}\">{$letter}</a>";
        }
    }
    
    /**
     * Gets an array of exposant custom post types.
     *
     * @return array
     */
    function tomtom_get_exposants() {
        $args = array(
            'posts_per_page' => -1,
            'post_type' => 'exposant',
            'orderby' => 'title',
            'order' => 'ASC',
        );
        if ( ! empty( $_GET['lettre'] ) ) {
            $args['starts_with'] = $_GET['lettre'];
        }
        return new WP_Query( $args );
    }
    
    /**
     * Adds fields to an exposant custom post type object.
     *
     * @param WP_Post $exposant Exposant custom post type.
     * @return void
     */
    function tomtom_hydrate_exposant( &$exposant ) {
        $exposant->logo        = get_field( 'logo', $exposant->ID );
        $exposant->related     = get_field( 'pieces_liees', $exposant->ID );
        $exposant->description = get_field( 'description', $exposant->ID );
    }
    
    /**
     * Outputs links related to exposant custom post type object.
     *
     * @return void
     */
    function tomtom_output_related( $exposant ) {
        if ( ! isset( $exposant->related ) ) {
            tomtom_hydrate_exposant( $exposant );
        }
        foreach ( $exposant->related as $k => $category ) { 
            if ( $k ) {
                echo '/';
            }
            echo "<a href=\"" . get_the_permalink( $category ) . "\">" . get_the_title( $category ) . "</a>";
        }
    }
    
    ?>
    <div class="exposant__filter" id="exposants">   
        <?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 ): ?>
        <?php tomtom_hydrate_exposant( $exposant ); ?>
        
        <div class="exposant__thumb col--padding-right">
            <div class="exposant__logo">
                <img src="<?php echo $exposant->logo['url'] ?>" alt="<?php echo $exposant->logo['alt'] ?>">
            </div>
            <div class="exposant__thumb-content">
                <h2 class="heading--as-h4 heading--no-margin"><?php echo $exposant->post_title; ?></h2>
                <?php if ( $exposant->related ): ?>
                <p class="exposant__categories">
                    <?php tomtom_output_related( $exposant->related ); ?>
                </p>
                <?php endif; ?>
                <p class="exposant-thumb__excerpt"><?php echo wp_trim_words( $exposant->description, 16, '...' ); ?></p>
                <a href="<?php get_permalink( $exposant->ID ); ?>" class="btn"><?php _e( 'Plus de détails', 'festival' ); ?></a>
            </div>
        </div>
    <?php endforeach; ?>
    </div>
    

    【讨论】:

    • 我试过了,结果只显示T字母。我将探索您的解决方案。
    猜你喜欢
    • 2017-09-08
    • 2014-10-13
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    • 2012-11-02
    • 1970-01-01
    相关资源
    最近更新 更多