Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在wordpress单个帖子中添加类别(帖子)_Php_Html_Wordpress_Categories - Fatal编程技术网

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

Php 如何在wordpress单个帖子中添加类别(帖子),php,html,wordpress,categories,Php,Html,Wordpress,Categories,嘿,任何人都知道如何在wordpress single post中添加特定类别的帖子。我提供了一个正在使用它的网站链接。这是一个包含特定类别帖子的帖子。要求与帖子缩略图相同。谢谢。只要将此代码放在你的single.php文件中,你就会得到你想要的结果: <?php $category = get_the_category(); ?> <ul> <?php $args = array('category' => $category[0]->term_id

嘿,任何人都知道如何在wordpress single post中添加特定类别的帖子。我提供了一个正在使用它的网站链接。这是一个包含特定类别帖子的帖子。要求与帖子缩略图相同。谢谢。

只要将此代码放在你的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> 



并根据您的选择为ul li添加css,如li{float:left;}

如果您想在
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();
?>

谢谢,但是在single.php中添加代码之后,我如何才能在帖子中添加分类?我的意思是,我必须在我的帖子中使用任何短代码来实现特定的分类吗?