2012-08-10 95 views
9

我在我的頁面模板使用這種通過其類別獲得職位:WP - 按類別獲取文章?

<?php 
     if (is_page(19)){ 
      ?> 
      <ul> 
      <?php 
       global $post; 
       $args = array('category' => 'Testimonial'); 
       $myposts = get_posts($args); 
       foreach($myposts as $post) : setup_postdata($post); ?> 
        <li class="testimonial"><?php the_content(); ?></li><br/> 
       <?php endforeach; ?> 
      </ul> 
     <?php } ?> 

但它檢索所有的職位來代替。不僅僅是標有「見證」的標籤。任何想法我做錯了什麼?

回答

19

「CATEGORY_NAME」 =>「這貓也適用,但在WP文檔

+0

現在是 - http://codex.wordpress.org/Template_Tags/get_posts – Ciprian 2014-10-16 15:42:22

1
add_shortcode('seriesposts', 'series_posts'); 

function series_posts($atts) 
{ ob_start(); 

$myseriesoption = get_option('_myseries', null); 

$type = $myseriesoption; 
$args=array( 'post_type' => $type, 'post_status' => 'publish', 'posts_per_page' => 5, 'caller_get_posts'=> 1); 
$my_query = null; 
$my_query = new WP_Query($args); 
if($my_query->have_posts()) { 
echo '<ul>'; 
while ($my_query->have_posts()) : $my_query->the_post(); 
echo '<li><a href="'; 
echo the_permalink(); 
echo '">'; 
echo the_title(); 
echo '</a></li>'; 
endwhile; 
echo '</ul>'; 
} 
wp_reset_query(); 




return ob_get_clean(); } 

//這將產生一個短碼功能,在您的網站上使用[seriesposts]

0

創建一個分類字段類別(字段名= post_category),並在您的模板導入,如下圖所示:

<?php 
      $categ = get_field('post_category'); 
      $args = array('posts_per_page' => 6, 
     'category_name' => $categ->slug); 
      $myposts = get_posts($args); 
      foreach ($myposts as $post) : setup_postdata($post); ?> 
      //your code here 
      <?php endforeach; 
      wp_reset_postdata();?>