簡體   English   中英

按自定義字段對Wordpress foreach結果進行排序

[英]Sort Wordpress foreach results by custom field

我整日都在這個問題上受困,而我一直對此一無所知,希望有人能提供幫助!

我正在為一家餐廳建立一個站點,該餐廳有多個位置,需要列出每個特定位置存在的每種飲料。 然后,需要對這些飲料進行分類(棕色,白色,番茄,啤酒,葡萄酒)。 我覺得我已經非常接近解決方案了,但是最后一刻我一直在head頭。

這是我的代碼:

$drinks = get_posts('post_type=drinks&numberposts=-1');
        $show_brown_title   = false;
        $show_white_title   = false;
        $show_tomato_title  = false;
        $show_wine_title    = false;
        $show_beer_title    = false;

        if ( $drinks ) {
            foreach( $drinks as $drink ) {

                $id             = $drink->ID;
                $drink_location = $drink->drink_location;

                if($drink->drink_category == 'Brown' && $drink_location && in_array($site_slug, $drink_location)) {
                    if($show_brown_title == false) {
                        echo '<h4><span>Brown</span> Cocktails</h4>';
                        echo '<ul>';

                        $show_brown_title = true;
                    }

                    echo '<li>';
                    echo '<span class="drink_title">'.$drink->post_title.'</span>';
                    echo '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
                    echo '<span class="drink_price">'.$drink->price_oz.'</span>';
                    echo '</li>';
                }

                if($drink->drink_category == 'White' && $drink_location && in_array($site_slug, $drink_location)) {
                    if($show_white_title == false) {
                        echo '<h4><span>White</span> Cocktails</h4>';
                        echo '<ul>';

                        $show_white_title = true;
                    }

                    echo '<li>';
                    echo '<span class="drink_title">'.$drink->post_title.'</span>';
                    echo '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
                    echo '<span class="drink_price">'.$drink->price_oz.'</span>';
                    echo '</li>';
                }
           }
}

在大多數情況下,這是可行的。 但是,我遇到了兩個問題。

  1. 完成每個類別后,我不知道如何關閉無序列表。
  2. 大部分情況下,這都是按類別分組的,但是,如果我以后要喝一杯,它實際上並不會放入正確的類別中,它只會進入底部的任何類別。 我不確定這是因為我沒有關閉無序列表,還是因為從WP中提取帖子的順序。

讓我知道是否正在解釋這個問題,非常感謝你們能提供的任何幫助!

ž

應該使用WP_Query代替get_posts。 信息: https : //wordpress.stackexchange.com/questions/50761/when-to-use-wp-query-query-posts-and-pre-get-posts/50762#50762

$show_brown_title   = false;
$show_white_title   = false;
$show_tomato_title  = false;
$show_wine_title    = false;
$show_beer_title    = false;
$args = array(
    'post_type'     => 'drinks',
    'numberposts'   => '-1'
);
$query = new WP_Query( $args );
if( $query->have_posts() ):
    while( $query->have_posts() ): $query->the_post();
        echo '<pre>';
        print_r( $query );
        echo '<pre>';
    endwhile;
endif;
wp_reset_postdata();

在循環中,您將再次構造清單,逐步使用print_r,將對象元素巧妙使用。


方法2:

$show_brown_title   = false;
$show_white_title   = false;
$show_tomato_title  = false;
$show_wine_title    = false;
$show_beer_title    = false;
foreach((get_the_category()) as $cat) {
    $args = array(
        'post_type'     => 'drinks',
        'numberposts'   => '-1',
        'cat'           => $cat->cat_ID
    );
    $query = new WP_Query( $args );
    if( $query->have_posts() ):
        echo '<ul>';
        while( $query->have_posts() ): $query->the_post();
            echo '<h4><span>'. $query->category_name .'</span> Cocktails</h4>';
            echo '<li>';
            echo '<span class="drink_title">'.$query->post_title.'</span>';
            echo '<span class="drink_ingredients">'.$query->drink_ingredients.'</span>';
            echo '<span class="drink_price">'.$query->price_oz.'</span>';
            echo '</li>';
        endwhile;
        echo '</ul>';
    endif;
    wp_reset_postdata();
}

使用$ query-> category_name的規范,也許應該以其他方式編寫。 使用print_r查看對象的正確字段名稱/元素。

貝婁是我的解決方案。

注意:get_posts使用orderby參數按Drink_category排序帖子。

該代碼被簡化為僅在Drink_category更改時開始<ul> ,並且在類別更改時結束</ul> 代碼還檢查要輸出的html是否已分配了值,並在最后添加</ul> ,以防未附加值。

$drinks = get_posts('post_type=drinks&numberposts=-1&orderby=drink_category');

$current_category = '';
$html = '';
if ( $drinks ) {
    foreach( $drinks as $drink ) {
        $id             = $drink->ID;
        $drink_location = $drink->drink_location;

        if ($current_category != $drink->drink_category) {
            if ($current_category != '') {
                $html .= '</ul>';           
            }
            $current_category = $drink->drink_category;
            $html .= '<h4><span>' . $current_category . '</span> Cocktails</h4>';
            $html .= '<ul>';
        }

        if($drink_location && in_array($site_slug, $drink_location)) {
            $html .= '<li>';
            $html .= '<span class="drink_title">'.$drink->post_title.'</span>';
            $html .= '<span class="drink_ingredients">'.$drink->drink_ingredients.'</span>';
            $html .= '<span class="drink_price">'.$drink->price_oz.'</span>';
            $html .= '</li>';
        }
    }
}

if (strlen($html) > 0 && !endsWith($html, '</ul>')) {
    $html .= '</ul>';
}

function endsWith($haystack, $needle)
{
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }
    return (substr($haystack, -$length) === $needle);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM