繁体   English   中英

如何结合这个 php 语句来获得多个变量输入的结果?

[英]How can I combine this php statement to get the results of multiple variable inputs?

这是我的 WordPress 查询,但不是与 wordpress 相关的问题。 它显示了 meta_key 为extra1和 meta_value 为test的帖子

<?php $customkey1 = extra1; ?>
<?php $customvalue1 = test; ?>
<?php query_posts('meta_key=' . $customkey1 . '&meta_value=' . $customvalue1 . '');  ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php the_title(); ?>

<?php endwhile; ?>
<?php endif; ?>

我的问题是我如何才能在同一个查询中显示将extra1作为元键并test作为元值的帖子,以及将extra2作为元键和test2作为元值的帖子。 两个或多个变量的组合。

所以你想结合两个查询的结果? 检查一下您可能只需要创建两个查询对象并将它们组合起来。

类似的东西(注意,我没有安装 WordPress,也没有使用 WordPress。但假设它的 API 不是谎言):

<?php

// The Query
$the_query = new WP_Query( $args );
$the_second_query = new WP_Query( $args );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// The Second Loop
while ( $the_second_query->have_posts() ) : $the_second_query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Post Data
wp_reset_postdata();
?>

注意:这是可怕的,丑陋的,并且会起作用,但在大多数情况下,专业人士应该认为是错误的。 更优雅的是,至少我会使用 function 来解析帖子。 最优雅的是,我会做其他地方建议的事情,将组合查询封装在 MySQL 位中。 但是鉴于操作人员的知识,这似乎是快速解决此问题的“最佳方法”,希望只有一次。 (这种方法的重复应用会变成一场混乱的噩梦)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM