在我们开始之前,您应该正确地缩进代码并删除php垃圾邮件。这使您的代码更易于阅读和理解。其次,使用卷发({}
)而不是语法:
, endif
和endwhile
. 它更易于调试和阅读。另一个提示是,注释代码以备将来参考,这样可以更容易地记住您在一个月或一年前所做的事情
你的代码应该是这样的
<?php
$today = date("Y-m-d");
$argsmi = array(
\'posts_per_page\' => 1,
\'post_type\' => \'imprezy\',
\'orderby\' => \'meta_value_num\',
\'order\' => ASC,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'data_imprezy\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'datetime\'
),
array(
\'key\' => \'data_imprezy_do\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'datetime\'
)
)
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( $argsmi );
if ( $wp_query->have_posts() ) {
while( $wp_query->have_posts() ) {
$wp_query->the_post(); ?>
<div class="singleevthumbdiv">
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(\'thumbnail\', array(\'class\' => \'singleevthumb\'));
}
?>
</a>
<div class="cbmob"></div>
<div class="datatitle">
<a href="<?php the_permalink(); ?>" class="singleevtitle"><?php the_titlesmall(\'\', \'...\', true, \'25\') ?></a><div style="clear:both;"></div>
<?php
$date = DateTime::createFromFormat(\'Ymd\', get_field(\'data_imprezy\'));
$datedo = DateTime::createFromFormat(\'Ymd\', get_field(\'data_imprezy_do\'));
?>
<span class="data">
<?php
echo $date->format(\'d-m-Y\');
if( get_field(\'data_imprezy_do\') ){
echo \'- \' . $datedo->format(\'d-m-Y\');
}
?>
</span>
<div style="clear:both;"></div>
</div>
</div>
<?php
}
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
}
?>
要解决您的问题,请使用
$wp_query
哈克很脏,我真的不喜欢。您永远不应该弄乱全局值,因为这会在为全局值设置外部值时破坏全局值。即使您稍后重置了该值,这仍然是一个肮脏的黑客行为。这是一样的什么
query_posts
是你永远不应该使用的
您应该在全局范围变量之外使用自定义变量。
基于以上内容,您的代码应该如下所示
<?php
$today = date("Y-m-d");
$argsmi = array(
\'posts_per_page\' => 1,
\'post_type\' => \'imprezy\',
\'orderby\' => \'meta_value_num\',
\'order\' => ASC,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'data_imprezy\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'datetime\'
),
array(
\'key\' => \'data_imprezy_do\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'datetime\'
)
)
);
$q = new WP_Query( $argsmi );
if ( $q->have_posts() ) {
while( $q->have_posts() ) {
$q->the_post(); ?>
<div class="singleevthumbdiv">
<a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(\'thumbnail\', array(\'class\' => \'singleevthumb\'));
}
?>
</a>
<div class="cbmob"></div>
<div class="datatitle">
<a href="<?php the_permalink(); ?>" class="singleevtitle"><?php the_titlesmall(\'\', \'...\', true, \'25\') ?></a><div style="clear:both;"></div>
<?php
$date = DateTime::createFromFormat(\'Ymd\', get_field(\'data_imprezy\'));
$datedo = DateTime::createFromFormat(\'Ymd\', get_field(\'data_imprezy_do\'));
?>
<span class="data">
<?php
echo $date->format(\'d-m-Y\');
if( get_field(\'data_imprezy_do\') ){
echo \'- \' . $datedo->format(\'d-m-Y\');
}
?>
</span>
<div style="clear:both;"></div>
</div>
</div>
<?php
} //endwhile
wp_reset_postdata();
} //endif
?>