自定义WP循环的正确PHP语法 [英] Correct PHP Syntax for custom WP loop
问题描述
我正在尝试将排序选项插入我的wordpress网站.我已经可以使用它了,但是需要在wordpress循环中正确使用它的帮助.目前,我有:
I am trying to insert a sorting option into my wordpress site. I already have it working, but need help using it with the wordpress loop correctly. Currently, I have:
在页面上,有一些选项可以按字母或时间顺序排序:
<a href="?sort=date">Newest</a>
<a href="?sort=title">Alphabetical</a>
排序代码从此处开始,位于循环上方:
<?php $sort= $_GET['sort'];
if($sort == "title") { $order= "'orderby'=>'title','order'=>ASC'"; }
elseif($sort == "date") { $order= "'orderby'=>'date'"; }
else{ $order= "'orderby'=>'date','order'=>'DESC'"; }
?>
注意:我很确定问题出在变量$ order
使用变量$order
作为参数的Wordpress循环
Wordpress Loop Using Variable $order
as an argument
<?php $loop = new WP_Query( array( $order, 'post_type' => 'films', 'post_parent' => 0, 'posts_per_page' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Wordpress loop stuff here
<?php endwhile; ?>
该循环正确显示项目,但排序链接不起作用.这段代码与query_posts配合得很好,但我正在尝试使其对WP_Query起作用(如上所述).这里有什么想法吗?
The loop displays items correctly, but the sorting links are not working. This code works very well with query_posts but I am trying to get this to work for WP_Query (above). Any ideas here?
更新:这种技术在使用如下所示的query_posts时效果很好(但我仍然需要它用于WP_Query):
UPDATE: This technique works great using query_posts like below (but I still need it working for WP_Query):
<?php $sort= $_GET['sort'];
if($sort == "title") { $order= "&orderby=title&order=ASC"; }
elseif($sort == "date") { $order= "&orderby=date"; }
else{ $order= "&orderby=date&order=DESC"; }
?>
<?php $posts = query_posts($query_string . $order); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
Wordpress Stuff Here
<?php endwhile; ?>
<?php endif; ?>
推荐答案
WP_Query
期望参数的关联数组(array('param' => 'value')
),而query_posts仅接受查询字符串"("param=value¶m=value"
).您将两种选择混在一起,这就是为什么它不起作用的原因.您需要将$order
变量更改为数组而不是字符串,例如:$order = array('orderby' => 'title', 'order' => ASC');
.
WP_Query
expects associative array of parameters (array('param' => 'value')
), whereas query_posts accepts only "query strings" ("param=value¶m=value"
). You are mixing both options, that's why it doesn't work. You need to change $order
variable to be array instead of string, for example: $order = array('orderby' => 'title', 'order' => ASC');
.
以上答案尚未完成,可能会引起误解. WP_Query
文档在此处: https://codex.wordpress.org/Class_Reference/WP_Query .此类接受两种样式,但是它们的形式必须有所不同.我不知道该怎么做,因为它不是用类文档编写的,但是最好使用数组,所以:
Answer above is not complete and may be misleading. WP_Query
docs are here: https://codex.wordpress.org/Class_Reference/WP_Query . This class accepts boths styles, but they must be somewhat different formed. I don't know how exactly to do this, because it is not written in class docs, but you better use arrays, so:
if($sort == "title") { $order = array('orderby' => 'title', 'order' => 'ASC'); }
elseif($sort == "date") { $order= array('orderby' => 'date'); }
else{ $order= array('orderby' => 'date', 'order' => 'DESC'); }
在两种类型之间转换的FYI使用函数parse_str()
: http://php.net/manual/en/function.parse-str.php 和http_build_query()
:
FYI to convert between both types use functions parse_str()
: http://php.net/manual/en/function.parse-str.php and http_build_query()
: http://www.php.net/manual/en/function.http-build-query.php .
这篇关于自定义WP循环的正确PHP语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!