如何在不手动创建新页面的情况下按自定义字段排序? [英] How do I sort by a custom field without manually creating a new page?

查看:107
本文介绍了如何在不手动创建新页面的情况下按自定义字段排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处的Wordpress存在一些问题.老实说,我总是从头开始设计网站,并从头开始编码".最近,我一直在尝试使用WP,因为我听说过有关它的好消息.

Having a bit of an issue with Wordpress here. In all honesty I've always designed my sites from scratch and "coded" from the ground up. Lately I've been trying to work with WP as I've heard good things about it.

看来,WP免费提供了许多功能(例如,基于类别的动态页面").但是,我想知道如何在不重新发明轮子的情况下操纵这些免费赠品.例如,我想让我的子菜单显示一个帖子类别列表.但是我想按自定义字段"对这些类别进行排序.

It would appear that WP gives you many things for free (e.g. dynamic "pages" based on CATEGORIES). However, I would like to know how to manipulate these freebies without reinventing the wheel. For example, I would like to have my SUB-MENU display a list of post categories. But I would like to sort those categories by a CUSTOM FIELD.

现在,我可以重新发明轮子,并为每种类型手动创建(并链接到)新页面,依此类推,等等(我根本不介意这样做),但是,我希望有一个通过插件或其他方式解决此问题.我看过几本有关自定义查询的教程,但是它们都缺乏实现-它们只是给出查询而没有确切告知是要创建新页面还是将其插入某个函数中.

Now, I could reinvent the wheel and manually create (and link to) a new page for each sort, so on and so forth, (which I don't fundamentally mind doing) however, I'm hoping there is a way around this via plugins or otherwise. I've seen several tutorials on custom queries, but they stop short of implementation -- they simply give the query without telling exactly whether to create a new page or plug it into a function somewhere.

任何输入将不胜感激.

最好.

推荐答案

在主题根目录中category.php模板的顶部,添加以下内容以将自定义排序字段添加到查询中:

At the top of category.php template in your theme's root directory, add the following to add your custom sort field to the query:

<?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,这是一个简单的默认模板(从随附的二十个主题复制):

If your theme doesn't have a category.php, here is a simple default template to base it on (copied from the included twentyten theme):

<?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(); ?>

这篇关于如何在不手动创建新页面的情况下按自定义字段排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆