0

次のコードを使用して、今日の日付以上の日付のイベントのリストを出力しようとしています。

$args = array('post_type' => 'event') // setup my custom post type    
$todaysdate = blah blah //setup for today's date

// the wp loop  
query_posts($args); 

if ( (have_posts() && $eventdate >= $todaysdate)  ) : while (have_posts()) : the_post();

$eventdate = blah blah // setup for the date of the event;

echo $event;

endwhile; endif;

ご覧のとおり、問題はIFがループ内の変数に依存していることです。

最初にループの外側に変数を設定するための最良の方法は何ですか?

4

2 に答える 2

0

する代わりに

if stuff from while iteration that doesn't exist yet
    while

その後、あなたはする必要があります

while
    if stuff from current while iteration

あなたが言うように:「あなたが見ることができる問題は、IFがループ内にある変数に依存しているということです。」

現在の実行の時点で必要なものが存在するようにコードを構造化するだけです。依存関係を適切な場所に移動して、使用する前に存在するようにします。

于 2013-03-26T16:26:47.247 に答える
0

その場合、実際にはループ内にある必要があります。

$args = array('post_type' => 'event') // setup my custom post type    
$todaysdate = "blah blah"; //setup for today's date

// the wp loop  
query_posts($args); 

if ( (have_posts()) : while (have_posts()) : the_post();

$eventdate = "blah blah"; // setup for the date of the event;

if($eventdate >= $todaysdate))

echo $event;

endif;

endwhile; endif;

編集:

Thanks, this works fine, however, I want to output a set number of events (5 in total) which I've defined in $args. So $args sets up 5 events, but the second IF then filters out old events so I end up with less than 5.

しかし、そもそもそれがあなたがそれを持っているのとまったく同じ理由です。ただし、その日付以降の5つのイベントを表示する必要がある場合は、wp_queryそれ自体を変更する必要があります。ここに例を参照してください

于 2013-03-26T16:31:40.150 に答える