Define orderby in url

时间:2011-10-27 作者:v3nt

有没有办法通过url设置帖子的顺序?

/订货人/日期/订单/说明/

我用add\\u rewrite\\u rule whiteout success尝试了几件事。

add_action( \'init\', \'wpse13483_init\' );
function wpse13483_init()
{
    add_rewrite_rule( \'category/(.+?)/orderby/([^/]+)/order/([^/]+)(/page/?([0-9]{1,}))?/?$\', \'index.php?category_name=$matches[1]&paged=$matches[5]&orderby=$matches[2]&order=$matches[3]\', \'top\' );
}
最好的,丹。

2 个回复
最合适的回答,由SO网友:v3nt 整理而成

将此添加到函数中。php文件工作正常。只是remember to re-save your permalinks &;清空缓存几次以查看更改。

add_action( \'init\', \'wpse13483_init\' );
function wpse13483_init() {
    add_rewrite_rule( \'category/(.+?)/orderby/([^/]+)?/?$\', \'index.php?category_name=$matches[1]&orderby=$matches[2]&order=asc\', \'top\' );
    add_rewrite_rule( \'category/(.+?)/orderby/([^/]+)/order/([^/]+)?/?$\', \'index.php?category_name=$matches[1]&orderby=$matches[2]&order=$matches[3]\', \'top\' );
//  with pagination;
    add_rewrite_rule( \'category/(.+?)/orderby/([^/]+)/order/([^/]+)/page/([0-9]{1,})?/?$\', \'index.php?category_name=$matches[1]&orderby=$matches[2]&order=$matches[3]&paged=$matches[4]\', \'top\' );
}

SO网友:Chip Bennett

创建URL重写规则与使用URL查询变量对循环查询排序完全不同。

在担心前者之前,我建议先解决后者。

首先,您需要注册URL查询变量,例如functions.php):

function mytheme_loop_sort_queryvars( $qvars ) {
    $qvars[] = \'loop_orderby\';
    $qvars[] = \'loop_order\';
    return $qvars;
}
add_filter( \'query_vars\', \'mytheme_loop_sort_queryvars\' );
因此,现在您已经注册了查询变量,可以使用它们修改默认的循环查询。

首先,准备查询变量,例如(在适当的模板文件中):

<?php

// Orderby Query Filter
$loop_orderby = \'\';
// Array of valid \'orderby\' parameters, for validating the URL query string
$valid_loop_orderbys = array( \'ID\', \'author\', \'title\', \'date\', \'modified\', \'parent\', \'rand\', \'comment_count\', \'menu_order\' ); //note: excluding \'meta_value\', for simplicity
// Do something only if the query var is set
if ( isset( $wp_query->query_vars[\'loop_orderby\'] ) ) {
    // Make sure that only a valid \'orderby\' parameter is used
    // If URL string value isn\'t valid, use default \'date\'
    $title_filter = ( in_array( $wp_query->query_vars[\'loop_orderby\'], $valid_loop_orderbys ) ? $wp_query->query_vars[\'loop_orderby\'] : \'date\' );
}
// Order Query Filter
$loop_order = \'\';
// Array of valid \'order\' paramters, for validating the URL query string
$valid_loop_orders = array( \'ASC\', \'DESC\' );
// Do something only if the query var is set
if ( isset( $wp_query->query_vars[\'loop_order\'] ) ) {
    // Make sure that only a valid \'order\' parameter is used
    // If URL string value isn\'t valid, use default \'DESC\'
    $title_filter = ( in_array( $wp_query->query_vars[\'loop_order\'], $valid_loop_orders ) ? $wp_query->query_vars[\'loop_order\'] : \'DESC\' );
}
?>
现在,使用查询变量修改循环。首先,设置自定义参数数组:

<?php
$custom_orderby_query_args = array();

if ( \'\' != $filter_loop_orderby ) {
    $custom_orderby_query_args[\'orderby\'] = $filter_loop_orderby;
}
if ( \'\' != $filter_loop_order ) {
    $custom_orderby_query_args[\'order\'] = $filter_loop_order;
}
?>
现在,将自定义参数数组与默认循环查询参数合并:

<?php
// Globalize the default query
global $wp_query;
// Merge arguments
$customized_query_args = array_merge( $wp_query->query, $custom_orderby_query_args );
// Modify the query output
query_posts( $customized_query_args );
?>
注意:您必须准确地确定如何应用URL查询字符串。这段代码只在查询字符串就位后使用它们。

结束

相关推荐

query_post order desc

根据:http://codex.wordpress.org/Function_Reference/query_posts要显示类别中的所有帖子,请执行以下操作:query_posts( array ( \'category_name\' => \'The Category Name\', \'posts_per_page\' => -1 ) ); 很好,但如果我想显示帖子按ID描述订购有人知道怎么做吗?