按变量PHP的值循环

时间:2012-04-03 17:32:17

标签: php wordpress

我有一个wordpress循环,可以从表格中提取所有数据......

            $query = new WP_Query( $args );// The Loop
                $i = 0; $i = -1;

while ( $query->have_posts() )
{
$i++;
$query->the_post();
if ( $keys = get_post_custom_keys() )
{
    echo "<div class='clearfix card-prod ".($i==0?'first':'')."'><div class='top-dets'><span class='card-title'>";
    echo the_title();
    echo "</span>";



    $order_value = get_post_custom_values('order');
    foreach ( $order_value as $key => $value ) {
    echo  $value ;}


// Network query 
    $network_value = get_post_custom_values('srchnetwork');
    foreach ( $network_value as $key => $value ) {
    echo '<span class="srch-val-">'. $value . '</span>'; }

// Pricing Query
    $pricing_value = get_post_custom_values('srchpricing');
    foreach ( $pricing_value as $key => $value ) {
    echo '<span class="srch-val-1">'. $value . '</span>'; }

// Setup Query
    $setup_value = get_post_custom_values('srchsetupfee');
    foreach ( $setup_value as $key => $value ) {
    echo '<span class="srch-val-2">'. $value . '</span>'; }

// Services Query
    $services_value = get_post_custom_values('srchservices');
    foreach ( $services_value as $key => $value ) {
    echo '<span class="srch-val-3">'. $value . '</span></div>'; }

// Big Card Query
    $bigcard_value = get_post_custom_values('bigcard');
    foreach ( $bigcard_value as $key => $value ) {
    echo '<img src="wp-content/themes/cafc/images/cards/'. $value . '" />'; }


echo '<img src="wp-content/themes/cafc/images/top-choice.jpg" alt="Top Choice" class="topchoice">';
echo the_excerpt()."</div>";    

    }
}

我的变量$ order_value按照它们应该列出的顺序从数据库中提取一个数字值...

是否可以根据此值对结果进行排序?

$order_value = get_post_custom_values('order');
    foreach ( $order_value as $key => $value ) {
    echo  $value ;}

考虑@David Nguyen建议我将代码粘贴到此...

$args = array('orderby' => 'meta_value_num', 'meta_key' => 'order', 'order' => 'DESC',
            'meta_query' => array(
            array(
                'key' => $customkey,
                'value' => $customvalue,
                'compare' => '='
            ),
            array(
                'key' => $customkey1,
                'value' => $customvalue1,
                'compare' => '='
            )
            )
            );

只有它似乎不起作用......

1 个答案:

答案 0 :(得分:3)

将此添加到WP_Query中的args:

'orderby' => 'meta_value_num', 'meta_key' => 'order', 'order' => 'DESC'

http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters