2012-08-10 43 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();?>