使查询在wordpress中作为主查询

时间:2014-05-02 23:32:30

标签: wordpress

我在我的网站上查询了作者帖子或类别帖子。 这是显示帖子的功能:

function latest_posts_mansory(){

$output = '';

$this_category = get_category( get_query_var( 'cat' ) );
$cat_id = $this_category->cat_ID;
$args = array( 'post_status' => 'publish' );
$args['showposts'] = 12;
if (is_author()) {
$args['author'] = get_the_author_meta('ID');
} else {
$args['category'] = $cat_id;
}
$recent_posts = wp_get_recent_posts( $args );
$num_posts = count($recent_posts);
$curtitle = get_category_parents( get_query_var('cat') , false , ' - ' );
if (!is_author()) {
$output .= '<div class="section_title underline" id="section-title-5314721809a17" style="border-color: #dddddd;"><span class="title">'.substr($curtitle , 0, -2).'</span></div>';
} else {
$output .= '<div class="section_title underline" id="section-title-5314721809a17" style="border-color: #dddddd;"><span class="title">Προβολή Άρθρων</span></div>';
}
$output .= '<div class="post_list masonry isotope" style="display: block; position: relative; overflow: hidden; height:494px;">';
$j=0;
if ($num_posts == 0 && is_author()){
$output .='<div class="no results" style="text-align:center;position: inherit;" align="center"><h5 ALIGN="CENTER">Δεν υπάρχουν άρθρα απ\'τον συγκεκριμένο συντάκτη</h5></div>';
}elseif ($num_posts == 0){
$output .='<div class="no results" style="text-align:center;position: inherit;" align="center"><h5 ALIGN="CENTER">Δεν υπάρχουν άρθρα σε αυτήν την κατηγορία</h5></div>';
}else{
for ($i=0; $i<$num_posts; $i++){

$recent_posts_usage = $recent_posts[$i]['ID'];
$this_ID = $recent_posts[$i]['ID'];
$post_class = join(" ", get_post_class('', $this_ID));
$this_title = get_the_title ($this_ID);
$category = get_the_category ($this_ID);
$this_category = $category[0]->cat_name;
$this_thumb = get_the_post_thumbnail( $this_ID, 'medium');
$this_permalink = get_permalink($this_ID);
$page_data = get_page( $this_ID );
$excerpt = T2T_Toolkit::truncate_string(apply_filters('the_content', strip_tags($page_data->post_content)), 200);

$output .= '<div class="callout_box with_post one_fourth isotope-item';
if($j==3){
$output .= ' column_last';
$j=0;
} else {
$j++;
}
$output .= '" style="position: absolute; left: 0px; top: 0px; -webkit-transform: translate3d(476px, 0px, 0px);">';
$output .= '<article id="post-'.$this_ID.'" class="'.$post_class.'">';
$output .= '    <a href="'.$this_permalink.'">  '.$this_thumb.' </a>';
$output .= '<div class="callout_box_content">';
$output .= '<h3>    <a href="'.$this_permalink.'">'.$this_title.' </a></h3>';
$output .= $excerpt;
$output .='</div>';
$output .='</div>';
$output .='</article>';
}
}
$output .='</div>';
return $output;
}

add_shortcode('show_latest_posts_mansory', 'latest_posts_mansory');

问题是,无论阅读设置是什么,或者即使我在功能中使用此功能,此功能始终只显示10个最新帖子

function limit_change_posts_archive($query){
 $query->set('posts_per_page', 12);
 return;
}

add_filter('pre_get_posts', 'limit_change_posts_archive', 1);

那么我做错了什么?我应该改变功能以某种方式工作,就像它的页面的主要查询?分页也不起作用

2 个答案:

答案 0 :(得分:0)

在管理员中有一个全局设置,请检查

管理员&gt;设置&gt;读

编辑:重新阅读您的问题,看到您已经尝试过了。

在你的代码中,我认为你将错误的参数传递给args。来自codex:

<?php $args = array(
    'numberposts' => 10,
    'offset' => 0,
    'category' => 0,
    'orderby' => 'post_date',
    'order' => 'DESC',
    'include' => ,
    'exclude' => ,
    'meta_key' => ,
    'meta_value' =>,
    'post_type' => 'post',
    'post_status' => 'draft, publish, future, pending, private',
    'suppress_filters' => true );

    $recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>

所以你需要改变

$args['showposts'] = 12;$args['numberposts'] = 12;

如果要显示分页,则需要使用paginate_links($ args);查看Codex

我的某个网站的示例:

global $wp_query;

if ( $wp_query->max_num_pages <= 1 )
    return;
?>
    <?php 

    $big = 9999999;

    $args = array(
    'base'         => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format'       => '?paged=%#%',
    'total'        => $wp_query->max_num_pages,
    'current'      => max( 1, get_query_var('paged') ),
    'show_all'     => False,
    'end_size'     => 2,
    'mid_size'     => 1,
    'prev_next'    => True,
    'prev_text'    => __('« '),
    'next_text'    => __(' »'),
    'type'         => 'list',
    'add_args'     => False,
    'add_fragment' => ''
    );

    echo paginate_links( $args );
 ?>

答案 1 :(得分:0)

谢谢你们我使用WP_Query,现在一切都很棒。这是我的新功能:

function latest_posts_mansory(){
$output = '';
            $this_category = get_category( get_query_var( 'cat' ) );
            $cat_id = $this_category->cat_ID;
            $args = array( 'post_status' => 'publish' );
            if (is_author()) {
                        $args['author'] = get_the_author_meta('ID');
            } else {
                        $args['cat'] = $cat_id;
            }
            $args['paged'] = get_query_var( 'paged' );
            $curtitle = get_category_parents( get_query_var('cat') , false , ' - ' );

            //echo headers
            if (!is_author()) {
                        $output .= '<div class="section_title underline" id="section-title-5314721809a17" style="border-color: #dddddd;"><span class="title">'.substr($curtitle , 0, -2).'</span></div>';
            } else {
                        $output .= '<div class="section_title underline" id="section-title-5314721809a17" style="border-color: #dddddd;"><span class="title">Προβολή Άρθρων</span></div>';
            }
            $output .= '<div class="post_list masonry isotope" style="display: block; position: relative; overflow: hidden; height:494px;">';


            $recent_posts = new WP_Query( $args );
            if ( $recent_posts->have_posts() ) {
                while ( $recent_posts->have_posts() ) {
                    $recent_posts->the_post();
                    // do stuff

                            $num_posts = count($recent_posts);

                            //get ID,title,permalink etc
                            $j=0;
                            $recent_posts_usage = get_the_ID();
                            $this_ID = get_the_ID();
                            $post_class = join(" ", get_post_class('', $this_ID));
                            $this_title = get_the_title ();
                            $category = get_the_category ();
                            $this_category = $category[0]->cat_name;
                            $this_thumb = get_the_post_thumbnail( $this_ID, 'medium');
                            $this_permalink = get_the_permalink();
                            $page_data = get_page( $this_ID );
                            $excerpt = T2T_Toolkit::truncate_string(apply_filters('the_content', strip_tags($page_data->post_content)), 200);

                            //echo content
                            $output .= '<div class="callout_box with_post one_fourth isotope-item'; 
                            if($j==3){ 
                                        $output .= ' column_last';
                                        $j=0;
                            } else {
                                        $j++;   
                            }
                            $output .= '" style="position: absolute; left: 0px; top: 0px; -webkit-transform: translate3d(476px, 0px, 0px);">';
                            $output .= '<article id="post-'.$this_ID.'" class="'.$post_class.'">';
                            $output .= '    <a href="'.$this_permalink.'">  '.$this_thumb.' </a>';
                            $output .= '<div class="callout_box_content">';
                            $output .= '<h3>    <a href="'.$this_permalink.'">'.$this_title.' </a></h3>';
                            $output .= $excerpt;
                            $output .='</div>';
                            $output .='</article>';
                            $output .='</div>';



                }
                wp_reset_postdata();
            } else {
                    // none were found
                        if (is_author()){
                                    $output .='<div class="no results" style="text-align:center;position: inherit;" align="center"><h5 ALIGN="CENTER">Δεν υπάρχουν άρθρα απ\'τον συγκεκριμένο συντάκτη</h5></div>';
                        }else{
                                    $output .='<div class="no results" style="text-align:center;position: inherit;" align="center"><h5 ALIGN="CENTER">Δεν υπάρχουν άρθρα σε αυτήν την κατηγορία</h5></div>';
                        }
            }

            $output .='</div>';
            return $output;
}