0

こんにちは、私は広告 Web サイトのテーマを作成しており、投稿のカスタム フィールドとして言及されているトップ 8 の人気のある都市をリストしたいと考えています。これは、最も分類されている上位 8 都市を意味します。それはこのようになります

http://i50.tinypic.com/6f7fww.png

カスタム フィールドの並べ替えの例をいくつか見ましたが、適切に機能させることができませんでした。

4

2 に答える 2

2

WPDB と SQL でこれを行うことができます。

global $wpdb;
$cities = $wpdb->get_col( "SELECT meta_value, COUNT(*) AS c 
FROM $wpdb->postmeta 
WHERE meta_key = 'city'
GROUP BY meta_value 
ORDER BY c DESC" );

都市を実際のメタ キーに置き換えます。$cities は、対応するメタ フィールドに都市が含まれる投稿の数によって並べ替えられた配列になります。

この配列 $cities を出力するには、たとえば次のコードを使用できます。

echo '<ul>';
foreach( $cities as $city ) {
    echo '<li>' . $city . '</li>';
}
echo '</ul>';
于 2012-11-17T20:09:40.273 に答える
-1

これを行う :

            <?php

              $custom_fields = get_post_custom($post_id);
              // Retrieves your custom fields
              $my_city_custom_field = $custom_fields['city'];
              // Retrieve all values for your "city" custom field
              foreach ( $my_city_custom_field as $key => $value )
                $cities[$value]=$city[$value]+1;
                // add one to the city index of the cities table
                // So first time London comes up you get 1, second time 2, etc.

                asort($cities);
                // sorts the $cities table by value

               foreach ( $cities as $key => $value )
                echo($key);
                // print the city's names, in proper order

            ?>

私はこれを少し速くしたかもしれませんが、少なくとも、あなたはアイデアを得るでしょう

于 2012-11-18T00:37:09.550 に答える