如何在不手动创建新页面的情况下按自定义字段排序?
问题描述:
这里有一点与WordPress的问题。坦率地说,我总是从头开始设计我的网站,并从头开始“编码”。最近我一直在尝试与WP合作,因为我听说过它的好处。如何在不手动创建新页面的情况下按自定义字段排序?
看起来WP给你很多免费的东西(例如基于CATEGORIES的动态“页面”)。不过,我想知道如何操作这些赠品而不用重新发明轮子。例如,我想让我的子菜单显示一个帖子类别列表。但我想用CUSTOM FIELD对这些类别进行排序。现在,我可以重新发明轮子并为每一种类型手动创建(并链接到)一个新页面,等等等等(但是我根本不介意这样做),但是,我希望有通过插件或其他方式解决这个问题。我已经看到了几个关于自定义查询的教程,但是他们没有实现它们 - 他们只是简单地给出查询,而没有准确地告知是创建一个新页面还是将它插入到某个函数中。
任何输入将不胜感激。
最好。
答
在category.php模板在主题的根目录上,以下内容添加到您的自定义排序字段添加到查询:
<?php
function is_valid_custom_sort_field($field)
{
// implementation left as an exercise for the questioner
return true;
}
if ($_REQUEST['sort_custom_field'] && is_valid_custom_sort_field($_REQUEST['sort_custom_field'])) {
query_posts($query_string . '&orderby='.$_REQUEST['sort_custom_field']);
}
参见: http://codex.wordpress.org/Function_Reference/query_posts
如果没有按主题没有category.php,这里是一个简单的默认模板(以包含twentyten主题为基础):
<?php
/**
* The template for displaying Category Archive pages.
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<h1 class="page-title"><?php
printf(__('Category Archives: %s', 'twentyten'), '<span>' . single_cat_title('', false) . '</span>');
?></h1>
<?php
$category_description = category_description();
if (! empty($category_description))
echo '<div class="archive-meta">' . $category_description . '</div>';
/* Run the loop for the category page to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-category.php and that will be used instead.
*/
get_template_part('loop', 'category');
?>
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
你是如何使用custo米类与类别? – superUntitled 2011-01-12 02:34:49
我不是。但我想。我很抱歉不这么说,但我正在使用自定义主题:AgentPress(http://www.studiopress.com/themes/agentpress)。注意演示中的子菜单。我有一个类似的设置。但是,一旦我点击一个,我希望这些值按主题提供的自定义字段排序(例如,价格,位置,邮政编码等)。 – 2011-01-12 03:19:05