将WP\\u查询对象包装在函数中,这样就不必更改所有查询$参数。
该函数将传递自定义字段值的参数和按自定义字段值排序的参数。
以下是您的操作方法:
<?php
function custom_query($divided_cf_val=array(), $order_by_cf_val) {
?>
<div id="listings">
<?php
$args = array(
\'post_type\' => \'listing\',
\'post_status\' => \'publish\',
\'posts_per_page\' => \'96\',
\'meta_key\' => \'price_value\',
\'orderby\' => $order_by_cf_val,
\'order\' => \'ASC\',
\'meta_query\' => $divided_cf_val
);
$custom = new WP_Query( $args ); ?>
<h3 class="bar">Category 1 (<?php echo $custom -> found_posts; ?>)</h3>
<?php $margincounter = 1; ?>
<?php if ($custom->have_posts()) : while ($custom->have_posts()) : $custom->the_post(); ?>
<?php include get_template_directory() . \'/includes/listings_grid.php\'; ?>n in y
<?php endwhile; else: ?>
<?php endif; wp_reset_query(); ?>
</div>
<?php
}
?>
并在模板中调用此函数,如下所示:
<?php
$meta_field1 = array(
array(
\'key\' => \'category_value\',
\'value\' => \'category 1\',
),
array(
\'key\' => \'sold_value\',
\'value\' => \'No\'
)
);
$meta_field2 = array(
array(
\'key\' => \'category_value\',
\'value\' => \'category 2\',
),
array(
\'key\' => \'sold_value\',
\'value\' => \'Yes\'
)
);
$meta_field3 = array(
array(
\'key\' => \'category_value\',
\'value\' => \'category 3\',
),
array(
\'key\' => \'sold_value\',
\'value\' => \'Maybe\'
)
);
custom_query($meta_field1, $order_by_cf_val);
custom_query($meta_field2, $order_by_cf_val);
custom_query($meta_field3, $order_by_cf_val);
custom_query($meta_field4, $order_by_cf_val);
?>
等等。。。