开发者

WordPress categories and posts priting

开发者 https://www.devze.com 2023-02-23 12:24 出处:网络
What is the syntax for printing the sequence of category name and its follow up posts i开发者_JS百科n my new template in a WordPress site? I have tried a lot via Google but all are not working properl

What is the syntax for printing the sequence of category name and its follow up posts i开发者_JS百科n my new template in a WordPress site? I have tried a lot via Google but all are not working properly.


There are many ways to do this, but here is a simple one (you will have to improve it with the category and posts links at least):

<?php $categories=  get_categories(); 
    if( !empty($categories) ):
?>
<ul>
    <?php foreach ($categories as $category): ?>
    <li>
        <?=$category->cat_name?>
        <?php $posts = get_posts($category->cat_id);
            if( !empty($posts) ):
        ?>
            <ul>
            <?php foreach( $posts as $post ): ?>
                <li><?= $post->post_title; ?></li>
            <?php endforeach; ?>
            </ul>
        <?php endif; ?>
    </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>
0

精彩评论

暂无评论...
验证码 换一张
取 消