空查询会中断其他查询

时间:2015-10-05 作者:th3rion

您好,我有一个cpt查询,根据日期显示1个最新事件。它工作正常-问题是日期何时为<;今天(没有要显示的未来事件)此查询为空,并且还会中断站点上的其他查询。

<?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() ) : ?>

<?php 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\'));

}
else {
}
?>

</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\')); ?>

<?php $datedo = DateTime::createFromFormat(\'Ymd\', get_field(\'data_imprezy_do\')); ?>

<span class="data">

<?php echo $date->format(\'d-m-Y\'); ?>

<?php if( get_field(\'data_imprezy_do\') ): ?>

- <?php echo $datedo->format(\'d-m-Y\'); ?>

<?php endif; ?>

</span> 

<div style="clear:both;"></div>

</div>

</div>

<?php endwhile; ?>                                              

<?php $wp_query = null; $wp_query = $temp; wp_reset_query(); ?>
<?php endif; ?> 

1 个回复
SO网友:Pieter Goosen

在我们开始之前,您应该正确地缩进代码并删除php垃圾邮件。这使您的代码更易于阅读和理解。其次,使用卷发({})而不是语法:, endifendwhile. 它更易于调试和阅读。另一个提示是,注释代码以备将来参考,这样可以更容易地记住您在一个月或一年前所做的事情

你的代码应该是这样的

<?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
?> 

相关推荐

JqueryUi对话框给出未捕获的TypeError:This._addClass不是函数错误

我有一个网站,我们需要一些自定义php编码来连接到外部数据库,以获取几个销售我们产品的供应商的产品评论URL。我们试图实现的基本想法是让用户注册他们的产品,然后如果他们愿意留下评论,就延长保修期。我正在使用XYZScript。com的“插入PHP”插件来实现这一点。该主题最初只加载了jQuery,以避免创建子主题,我们正在php脚本中加载jQueryUI。因此,我们将jQuery加载到文档的标题中,将jQueryUI加载到文档的正文中。我不太确定这是因为加载脚本的顺序造成的,还是其他一些冲突的javasc