如果是嵌套数组中的语句

Abh*_*hik 0 php wordpress

我想要做的是使用搜索表单中的元键查询WordPress自定义帖子类型.搜索表单接受用户输入并根据匹配条件显示结果.表单的某些字段可能为空,因此我需要确保不将任何空白值传递给查询参数.我需要在arguments数组中使用if.

我会感激任何类型的帮助.

这是我正在使用的代码,但收到错误消息.

Parse error: syntax error, unexpected T_IF, expecting ')'

这是我的代码:

        if (isset($POST['stock']) && !empty($POST['stock'])) {
            $stock = $POST['stock'];
        }
        if (isset($POST['mineral']) && !empty($POST['mineral'])) {
            $mineral = $POST['mineral'];
        }
        if (isset($POST['species']) && !empty($POST['species'])) {
            $species = $POST['species'];
        }
        if (isset($POST['color']) && !empty($POST['color'])) {
            $color = $POST['color'];
        }
        if (isset($POST['chemicalclass']) && !empty($POST['chemicalclass'])) {
            $chemicalclass = $POST['chemicalclass'];
        }
        if (isset($POST['locality']) && !empty($POST['locality'])) {
            $locality = $POST['locality'];
        }
        if (isset($POST['description']) && !empty($POST['description'])) {
            $description = $POST['description'];
        }
        if (isset($POST['size']) && !empty($POST['size'])) {
            $size = $POST['size'];
        }
        if (isset($POST['pricegt'])) {
            $pricegt = $POST['pricegt'];
        } else {
            $pricegt = 0;
        }
        if (isset($POST['pricelt'])) {
            $pricelt = $POST['pricelt'];
        } else {
            $pricelt = 999999;
        }

        $args = array(
            'post_type'         => 'products',
            'productspecies'    => $species,
            'localities'        => $locality,
            'meta_query' => array(
                //'relation' => 'OR',
                array(
                    'key' => '_price',
                    'value' => array( $pricegt, $pricelt ),
                    'compare' => 'BETWEEN',
                    'type' => 'numeric',
                ),
                if ($mineral) {
                array(
                    'key' => '_components',
                    'value' => $mineral,
                ),
                }
                if ($stock) {
                array(
                    'key' => '_lotnum',
                    'value' => $stock,
                ),
                }
                if ($color) {
                array(
                    'key' => '_color',
                    'value' => $color,
                ),
                }
                if ($description) {
                array(
                    'key' => '_smalldesc',
                    'value' => $description,
                    'compare' => 'LIKE',
                ),
                }
                if ($size) {
                array(
                    'key' => '_size',
                    'value' => $size,
                ),
                }
                if ($chemicalclass) {
                array(
                    'key' => '_chemicalclass',
                    'valeu' => $chemicalclass,
                ),
                }
            ),
            );
    ?>
        <?php $query = new WP_Query( $args ); ?>

        <div class="postcount">We Found A Total Of <span><?php echo $query->post_count;?></span> Items Maching Your Search</div>

    <?php if ( $query->have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
Run Code Online (Sandbox Code Playgroud)

我做错了什么?

Ulr*_*rtz 5

您正在尝试将if语句作为参数传递给array()函数.PHP不允许这样做.您可以做的一件事是在没有可选部件的情况下构建阵列,然后在必要时添加它们:

if ($stock) {
    $args['metaquery'][] = array(
        'key' => '_lotnum',
        'value' => $stock
    );
}
Run Code Online (Sandbox Code Playgroud)