将其他变量传递给查询

时间:2013-09-20 作者:Manish

如何向现有查询添加更多参数?假设我的主题中有

if (!$blog_query) $blog_query = $wp_query;

while( $blog_query->have_posts() ) : $blog_query->the_post();
在传递之前,我想向现有查询添加两个参数,比如:

\'orderby\' => \'post-title\',
\'order\' => \'ASC\',
如何将这些添加到现有的$blog\\u查询中?

3 个回复
SO网友:Eric Holmes

如果您正在运行自定义查询,如$blog\\u query,请将它们添加到中。如果要更改主查询,请使用pre_get_posts 更改功能的操作。

add_action( \'pre_get_posts\', \'alter_blog_order\' );
function alter_blog_order( $query ) {
    if( $query->is_main_query() and $query->is_post_type_archive( \'post\' ) ) {
        $query->set( array( \'orderby\' => \'post-title\', \'order\' => \'ASC\' ) );
    }
}

SO网友:Manish

我想LINK 是可以自我解释的。

SO网友:Manish

我的主题按日期对帖子进行排序,这是默认行为。我想按字母顺序对一些类别的帖子进行排序,因此我试图创建一个循环,如“if is\\u category(array(1,2,3){使用上面写的两个参数运行完整的循环添加到默认查询}或者{运行默认循环}”。以下是我的模板博客文件的完整代码\'

<?php 
if (!$blog_query) $blog_query = $wp_query;
$blogOptions = ($shortcode_values) ? $shortcode_values : $theLayout[\'blog\'];

while( $blog_query->have_posts() ) : $blog_query->the_post();

    // style and layout info
    $postClass = array();

    // images enabled?
    if ($blogOptions[\'blog_featured_images\']) {

        // class
        $postClass[] = \'\'; //\'style-image-left\';

        // get thumbnail image
        $thumb = get_post_thumbnail_id(); 

        // image sizes
        $imageW = $blogOptions[\'image\'][\'width\'];
        $imageH = $blogOptions[\'image\'][\'height\'];

        // crop image (default = true)
        $imageCrop = ( $imageW === 0 || $imageH === 0 ) ? false : true; // Setting to FALSE is useful when setting one value so other adjusts automatically

        // get resized image
        // this will return the resized $thumb or placeholder if enabled and no $thumb
        $image = vt_resize( $thumb, \'\', $imageW, $imageH, $imageCrop );

            // If media field is populated use lightbox for image/video on click
            $popup_link = \'\';
            if (get_meta(\'media_url\')) {
                $popup_link = \'<a href="\'. get_meta(\'media_url\') .\'" class="popup" title="\'. get_meta(\'media_title\') .\'">\';
            } 
    }

    if (!$image[\'url\']) {
        // no imge
        $postClass[] = \'noImage\';
    }

    ?>

    <li class="post-item clearfix">
        <article id="post-<?php the_ID(); ?>" <?php post_class($postClass); ?>>
            <div class="item-container">

                <?php if ($image[\'url\']) : ?>
                <div class="the-post-image">
                    <?php 
                    if ($popup_link) : 
                        echo $popup_link; 
                    else : ?>
                        <a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf( esc_attr__( \'Permalink to %s\', THEME_NAME ), the_title_attribute( \'echo=0\' ) ); ?>">
                    <?php endif; ?>
                        <figure>
                            <img src="<?php echo $image[\'url\']; ?>" width="<?php echo $image[\'width\']; ?>" height="<?php echo $image[\'height\']; ?>" />
                        </figure>
                    </a>
                </div>
                <?php endif; ?>

                <div class="the-post-content">
                    <header class="entry-header">

                        <?php
                        // Author avatar 
                        if ($blogOptions[\'author_avatar\']) :
                            ?>
                            <div class="author-avatar">
                                <a href="<?php echo get_author_posts_url(get_the_author_meta(\'ID\')); ?>"><?php echo get_avatar(  get_the_author_meta(\'ID\'), 35, \'\', get_the_author_meta(\'display_name\')); ?></a>
                            </div>
                            <?php
                        endif; ?>

                        <!-- Title / Page Headline -->
                        <h2 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf( esc_attr__( \'Permalink to %s\', THEME_NAME ), the_title_attribute( \'echo=0\' ) ); ?>"><?php the_title(); ?></a></h2>

                        <?php if ($blogOptions[\'author_name\'] || $blogOptions[\'post_date\'] || $blogOptions[\'comments_link\'] || $blogOptions[\'category_list\']) : ?>
                        <div class="post-header-info clearfix" <?php if ( !$blogOptions[\'author_avatar\'] ) : echo \'style="margin-left: 0;"\'; endif; ?>>
                            <?php                               
                            // Author name 
                            if ($blogOptions[\'author_name\']) : 
                                ?>
                                <address class="vcard author">
                                    <?php _e( \'by \', THEME_NAME ) . the_author_posts_link(); ?> 
                                </address>
                            <?php 
                            endif;
                            // Comments link
                            if ($blogOptions[\'comments_link\']) : 
                                // comments link ?>
                                <span class="comments-link"><?php comments_popup_link( __( \'Comment\', THEME_NAME ), __( \'1\', THEME_NAME ), __( \'%\', THEME_NAME ), \'\', \'\' ); ?></span>
                                <?php 
                            endif;
                            // Category list 
                            if ($blogOptions[\'category_list\']) :
                                if ( count( get_the_category() ) ) :
                                    ?>
                                    <div class="cat-links">
                                        <?php 
                                        // seperator
                                        if ($blogOptions[\'post_date\']) { echo \' <span class="meta-sep">|</span> \'; }
                                        //printf( __( \'<span class="%1$s">Posted in</span> %2$s\', THEME_NAME ), \'entry-utility-prep entry-utility-prep-cat-links\', get_the_category_list( \', \' ) );
                                        echo get_the_category_list( \', \' ); 
                                        ?>
                                    </div>
                                    <?php 
                                endif; 
                            endif;
                            // Date Published
                            if ($blogOptions[\'post_date\']) : 
                                ?>
                                <abbr class="published" title="<?php the_time(\'c\'); ?>"><span class="entry-date"><?php the_time(get_option(\'date_format\')); ?></span></abbr>
                                <?php 
                                //if ($blogOptions[\'comments_link\']) { echo \' <span class="meta-sep">|</span> \'; }  // seperator
                            endif;
                            ?>
                        </div>
                        <?php endif; ?>

                    </header>

                    <!-- Content -->
                    <div class="entry-content">
                        <?php
                            // Post content/excerpt
                            if ($blogOptions[\'use_excerpt\']) {
                                if ($blogOptions[\'excerpt_length\'] != \'-1\')  {
                                    echo customExcerpt(get_the_excerpt(), $blogOptions[\'excerpt_length\']); 
                                }
                            } else {
                                // set $more to 0 or WP will ignore the <!--more--> tags if not on the home page
                                global $more;
                                $more = 0;
                                the_content( __( \'Continue reading <span class="meta-nav">&rarr;</span>\', THEME_NAME ) );
                            }
                            // Read more link
                            if ($blogOptions[\'read_more\'] && $blogOptions[\'read_more\'] != "-1") {
                                $readMore = prep_content($blogOptions[\'read_more\'], 0, 1);
                                echo \'<div class="read-more"><a href="\'. get_permalink() .\'" title="\'. $readMore .\'" class="read-more-link">\'. $readMore .\'</a></div>\';
                            }
                        ?>
                    </div><!-- END .entry-content -->

                    <!-- Post Footer -->
                    <footer class="post-footer-info">
                        <?php 
                        // tag list
                        if ($blogOptions[\'tag_list\']) :
                            $tags_list = get_the_tag_list( \'\', \', \' );
                            if ( $tags_list ):  ?>
                            <div class="tag-links">
                                <?php printf( __( \'<span class="%1$s">Tagged</span> %2$s\', THEME_NAME ), \'entry-utility-prep entry-utility-prep-tag-links\', $tags_list ); ?>
                            </div>
                            <?php 
                            endif; 
                        endif; ?>
                    </footer><!-- END .post-footer-info -->
                </div>

            </div>
        </article>
    </li>

    <?php endwhile; ?>

</ol>

)if($blogOptions[\'paging\')get\\u pagination($blog\\u query);?>\'@rarst@eric holmes@s-ha-dum@manish

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post