2016-04-28 110 views
0

我需要一个简短代码来列出某个类别的所有帖子。如何列出wordpress短代码中所有类别的帖子?

我发现这个PHP代码的页面模板工程,但只要我添加它的简它不工作(我真的需要它在简码格式):

<ul>  
<?php 
$catPost = get_posts(get_cat_ID("31")); //change this 
foreach ($catPost as $post) : setup_postdata($post); ?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
<?php endforeach;?> 
</ul> 

那么怎么可以做它?

回答

0

应该是这样的(添加到您的functions.php文件)

function posts_in_category_func($atts) { 
    $category_id = $atts['cat']; 
    ?> 
     <ul>  
     <?php 
     $args = array('category' => $category_id, 'post_type' => 'post'); 
     $catPost = get_posts($args); 
     foreach ($catPost as $post) : setup_postdata($post); ?> 
     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
     <?php endforeach;?> 
     </ul> 
    <? 

} 
add_shortcode('posts_in_category', 'posts_in_category_func'); 

你可以把它像[posts_in_category cat=1]

相关问题