2014-05-02 26 views
0

我在我的網站中查詢了顯示作者帖子或類別帖子。 這是顯示的職位功能:將查詢作爲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); 

那麼,我做錯了什麼?我應該改變這個功能,以便像其頁面的主要查詢一樣工作嗎?也分頁不工作或者

+0

退房'WP_Query' http://codex.wordpress.org/Class_Reference/WP_Query – henrywright

+0

感謝,現在一切的偉大工程 – CDrosos

回答

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; 
} 
0

有一個在管理這個全局設置,檢查

管理員>設置>閱讀

編輯:只是重讀你的問題,看到你已經試過了。

在你的代碼中,我認爲你傳遞了錯誤的參數給參數。食典:

<?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($參數);從我的站點之一檢查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); 
?>