按自定义域更改页面顺序(魔域)

时间:2011-03-29 作者:D-B

我的代码列出了按帖子标题排序的子页面,并将子帖子的第一个图像附件作为缩略图。

子页面包含使用Magic fields插件创建的自定义字段。

有谁能帮我调整代码,这样我就可以将页面按名为“价格”的自定义字段降序排列?

这是我的代码:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="page" id="post-<?php the_ID(); ?>">

            <div class="pagecontent">


            <?php $child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = ".$post->ID." AND post_type = \'page\' ORDER BY post_title", \'OBJECT\');   ?>


<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>

<div class="grid"><a href="<?php echo  get_permalink($pageChild->ID); ?>" alt="<?php echo $pageChild->post_title; ?>" title="<?php echo $pageChild->post_title; ?>">
<?php $attachments = get_children( array(
                \'post_parent\'    => $pageChild->ID,
                \'post_type\'      => \'attachment\',
                \'numberposts\'    => 1, // show all -1
                \'post_status\'    => \'inherit\',
                \'post_mime_type\' => \'image\',
                \'order\'          => \'ASC\',
                \'orderby\'        => \'menu_order ASC\'
                ) );
foreach ( $attachments as $attachment_id => $attachment ) {
    echo wp_get_attachment_image( $attachment_id, \'medium\' );
} ?>
</a>

<h1><a href="<?php echo  get_permalink($pageChild->ID); ?>" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a></h1>
</div>

<?php endforeach; endif; ?>      

            </div>
        </div>

        <?php endwhile; endif; ?>
如果有任何帮助,我将不胜感激!

非常感谢。

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

我测试了这段代码,效果很好:

<?php query_posts(\'meta_key=price&orderby=meta_value_num&post_parent=\'.$post->ID.\'&post_type=page\'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <div class="page" id="post-<?php the_ID(); ?>">
        <div class="pagecontent">
            <div class="grid">
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                    <?php
                        $attachments = get_children( array (
                            \'post_parent\'    => $post->ID,
                            \'post_type\'      => \'attachment\',
                            \'numberposts\'    => 1,
                            \'post_status\'    => \'inherit\',
                            \'post_mime_type\' => \'image\',
                            \'order\'          => \'ASC\',
                            \'orderby\'        => \'menu_order ASC\'
                        ) );

                        foreach ( $attachments as $attachment_id => $attachment ) {
                            echo wp_get_attachment_image( $attachment_id, \'medium\' );
                        }
                    ?>
                </a>
                <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1>
            </div> <!-- // grid -->
        </div> <!-- // pagecontent -->
    </div> <!-- // page -->

<?php endwhile; endif; ?>
几点注意事项:

您实际上不需要使用get_results. 我将您的自定义查询限制与query_posts 在顶部呼叫query_posts 电话订购人meta_value_num, 这是按数字排序的理想选择。如果“价格”字段是字符串,请替换meta_value_num 具有meta_value.

结束

相关推荐