简体   繁体   English

如何在WordPress单个帖子中添加类别(帖子)

[英]How to add category(posts) in wordpress single post

嘿,任何人都知道如何在wordpress single post.m中添加特定类别的帖子,并给出使用它的网站链接http://www.animetv.org/anime/one-piece-dub/这是一个包含以下内容的帖子特定类别的帖子。要求相同,即带有帖子缩略图。谢谢。

Just put this code at your single.php file you will get your desired result: 只需将此代码放在您的single.php文件中,您将获得所需的结果:

<?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;} 然后根据您的选择将ul放在css中,例如li {float:left;}

If you want to display the posts from a specific category in single.php page, please add the following code to the the file. 如果要在single.php页面中显示特定类别的帖子,请在文件中添加以下代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM