【问题标题】:Sort Wordpress foreach results by custom field按自定义字段对 Wordpress foreach 结果进行排序
【发布时间】:2013-01-26 21:19:21
【问题描述】:

我整天都被这个问题所困扰,我一直无处可去,希望有人能提供帮助!

我正在为一家餐厅建立一个网站,该餐厅有多个地点,需要列出每个特定地点存在的每种饮料。然后需要按类别(棕色、白色、番茄、啤酒、葡萄酒)对这些饮料进行分类。我觉得我非常接近解决方案,但最近一段时间我一直在努力。

这是我的代码:

$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>';
                }
           }
}

在大多数情况下,这是可行的。但是,我遇到了 2 个问题。

  1. 在完成每个类别后,我不知道如何关闭无序列表。
  2. 这大部分是按类别分组的,但是,如果我有以后要喝的饮料,它实际上不会将它归入正确的类别,它只会归入底部的任何类别。我不确定这是因为我没有关闭无序列表,还是因为从 WP 中拉出帖子的顺序。

如果我解释得很好,请告诉我,我非常感谢你们提供的任何帮助!

Z

【问题讨论】:

  • 您是否尝试添加 if($show_brown_title == false) { echo ''; } 在关闭的 LI 之后?
  • 我做了,但是因为一切都在 for 循环中,它在每个 LI 实例之后添加它
  • 那么你应该在for循环之外打开它,在for循环之外也关闭它。
  • 我不确定我能不能。我只希望在这些类别中确实有饮料时显示这些类别,并且只有在我已经开始循环时才能显示它们。

标签: php wordpress loops foreach


【解决方案1】:

您应该使用 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 查看对象的正确字段名称/元素。

【讨论】:

  • 感谢 Gravuj,但我还是不太明白。这将输出所有的饮料,我如何让它只显示特定类别和位置的饮料?
  • 我只能写到为什么循环打开。我应该看看你的对象是如何构造的。
  • 我想我成功了。我为每个饮料类别创建了 args,并使用每组 args 检查位置进行了查询。非常感谢您的帮助!
【解决方案2】:

下面是我的解决方案。

注意:get_posts 使用 orderby 参数按饮料类别对帖子进行排序。

代码被简化为仅在drink_category 更改时开始&lt;ul&gt; 并在类别更改时结束&lt;/ul&gt;。代码还会检查要输出的 html 是否已分配一个值,并在其上附加一个最终的 &lt;/ul&gt;,以防尚未附加。

$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);
}

【讨论】:

  • 我想我明白这里发生了什么,但是当我插入它时它似乎并没有真正起作用。它列出了每个类别的标题,即使该类别中没有饮料。此外,它没有在正确的类别中显示该位置的正确饮料(如果这有意义,哈哈)
猜你喜欢
  • 2021-04-24
  • 1970-01-01
  • 2012-04-18
  • 1970-01-01
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
相关资源
最近更新 更多