Wordpress菜单,显示特定类别中的所有帖子


Wordpress menu that shows all posts in a certain category

我正在创建一个小部件菜单(类似于w3schools的侧边栏),它应该显示特定类别中列出的所有帖子
例如,一旦用户在导航栏中点击"HTML",就会从主页转到介绍课程的页面,我(通过拖放)放了一个侧边栏自定义菜单,其中有其他名为"HTML"的同类帖子。

我希望能够制作一篇文章并将其标记在"HTML"类别下,在菜单选项中,我选择菜单以仅在具有"HTML"分类的文章上显示菜单,然后每当我在"HTML"类别下制作文章时,菜单都会自动将另一篇文章添加到列表中。简单地说,一个动态菜单,显示某个类别下的所有帖子。

到目前为止,我已经遇到了这段代码,但把它放在菜单编辑器中是行不通的:

$catPost = get_posts(get_cat_ID("NameOfTheCategory"));
foreach ($catPost as $post) : setup_postdata($post); ?>
        <div>
             <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> 
              <p><?php the_content(); ?></p>
        </div> <?php  endforeach;?>

我是WordPress的新手,我试过到处搜索,试过很多插件,但什么都找不到。

您可以使用get categories获取所有类别或特定类别,然后对每个类别运行WP_Query。

    //menu loop
function menu_loop() {
global $post;
$args= array(...);
$categories = get_categories( $args);
foreach ($categories as $category ) {
$args = array (...);
// The Query
$query = new WP_Query( $args );
    // The Loop
    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) { $query->the_post();
        }
    }
   // Restore original Post Data
    wp_reset_postdata();
}
}