Skip to content Skip to sidebar Skip to footer

How To Add Category(posts) In Wordpress Single Post

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

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


Solution 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();
?>

Post a Comment for "How To Add Category(posts) In Wordpress Single Post"