How to add category(posts) in wordpress single post

こ雲淡風輕ζ 提交于 2020-01-16 21:35:38

问题


Hey any one know how to add a specific category posts in wordpress single post.m giving a link of website that is using it http://www.animetv.org/anime/one-piece-dub/ it is a post that contains posts of a particular category.It is demanded as same i.e. with post thumbnails.Thanks.


回答1:


Just put this code at your single.php file you will get your desired result:

<?php $category = get_the_category(); ?>
<ul>
<?php
$args = array('category' => $category[0]->term_id );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail();?></a><br/>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
<?php endforeach; 
wp_reset_postdata();?>
</ul> 

And put css for ul li as per your choice like li{float : left;}




回答2:


If you want to display the posts from a specific category in single.php page, please add the following code to the the file.

<?php
    $posts = new WP_Query();
    $posts->query( "category_name='{enter your category slug here}'&posts_per_page={enter the number of posts to display here}" );
    if($posts->have_posts()) :
        while ($posts->have_posts()) : $posts->the_post();
?>
            <div>
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a>
            </div>
            <div>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </div>
<?php
        endwhile;        
    endif;
    wp_reset_postdata();
?>


来源:https://stackoverflow.com/questions/26377527/how-to-add-categoryposts-in-wordpress-single-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!