第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Wordpress 結(jié)果中自定義帖子類型的按字母順序過濾

Wordpress 結(jié)果中自定義帖子類型的按字母順序過濾

PHP
慕桂英546537 2023-12-15 15:51:15
我正在處理自定義帖子類型存檔,我想添加 A-Z 過濾菜單。 我設(shè)法按照這個(gè)線程(Create letterical Pagination in wordpress)讓它正常工作,但我不明白我的初始頁面(/exposants)如何顯示所有結(jié)果。<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];} 但我不知道用什么替換。預(yù)先感謝您的寶貴支持。
查看完整描述

1 回答

?
慕少森

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊

如果我正確理解了問題,這里是您的代碼的稍微優(yōu)化的版本。為了清楚起見,我冒昧地盡可能地將你的 php 和 html 解耦。


它沒有經(jīng)過測(cè)試,可能需要一些調(diào)整,但總的來說我希望這能成功!


在您的functions.php文件中,添加以下內(nèi)容,這將允許您按首字母過濾數(shù)據(jù)庫查詢:


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>



查看完整回答
反對(duì) 回復(fù) 2023-12-15
  • 1 回答
  • 0 關(guān)注
  • 159 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)