簡體   English   中英

如何在“最近的帖子”小部件wordpress中顯示帖子的類別?

[英]How to show categoy of the post in recents posts widget wordpress?

我在wordpress網站的右側欄上有一個“最近發布的帖子”小部件,它顯示來自多種帖子類型的最新發布的帖子。 我希望此小部件還顯示帖子的類型或類別。 我該如何實現?

在循環中使用此方法get_the_category($id)

要手動執行此操作(即通過創建自己的函數),請將其添加到函數中,並在需要時調用它

<ul>
    <?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
        <li><?php get_the_category(the_ID())->name; // caetgory name ?></li>

        <?php 
    endwhile;
    wp_reset_postdata();
    ?>
</ul>

您可以在sidebar.php中為最近的帖子編碼

<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args ); ?>
<ul>
    <?php foreach( $recent_posts as $recent ){ ?>

            <li>
                <a href="<?php echo get_permalink( $recent["ID"] ); ?>">
                    <img src="<?php echo get_the_post_thumbnail_url( $recent["ID"] ); ?>">
                    <div class="recent_post_meta">
                        <span>
                        <?php $category_detail = get_the_category( $recent["ID"] );//$post->ID
                            foreach( $category_detail as $cd ){
                            echo $cd->cat_name;
                            } ?>
                        </span>
                        <div class="post_side_title">
                            <?php echo get_the_title( $recent["ID"] ); ?>
                        </div>
                    </div>
                </a>
            </li>

    <?php } ?>
    </ul>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM