列出 WordPress 特定类别的 post url

List WordPress certain category's post url

有没有办法在没有插件的情况下列出 WordPress 中某个类别的所有帖子 url? 我不熟悉 PHP,但我在想,如果有什么办法,我可以使用一个页面模板,其中一个方法会调用所有这个类别的帖子 url(例如:让我们调用类别 "blog").

回顾 WP_Query 和 WordPress 循环。如果我正确理解了您的询问(包裹在 php 标签中),这样的事情应该会起作用:

$the_query = new WP_Query( array( 'category_name' => 'blog' ) );

if ( $the_query->have_posts() ) {
 echo '<ul>';
 while ( $the_query->have_posts() ) {
    $the_query->the_post();
    echo '<li>' . get_permalink() . '</li>';
 }
 echo '</ul>';
 /* Restore original Post Data */
 wp_reset_postdata();
} else {
 // no posts found
} 

您可以使用 get_posts (https://codex.wordpress.org/Template_Tags/get_posts)。

将此视为 WP_Query 的精简版,它将 return 一组您想要的帖子。

$categoryPosts = get_posts(array(
    // Note: The category parameter needs to be the ID of the category, and not the category name.
    'category' => 1,
    // Note: The category_name parameter needs to be a string, in this case, the category name.
    'category_name' => 'Category Name',
));

然后你可以循环浏览帖子:

foreach($categoryPosts as $categoryPost) {
    // Your logic
}

$categoryPost 将默认包含以下内容(如果您有自定义字段,则更多字段),这些字段显然会被填充,但这是您在数组中可用的内容:

WP_Post Object
(
    [ID] =>
    [post_author] => 
    [post_date] => 
    [post_date_gmt] => 
    [post_content] => 
    [post_title] => 
    [post_excerpt] => 
    [post_status] => 
    [comment_status] => 
    [ping_status] => 
    [post_password] => 
    [post_name] => 
    [to_ping] => 
    [pinged] => 
    [post_modified] => 
    [post_modified_gmt] => 
    [post_content_filtered] => 
    [post_parent] => 
    [guid] => 
    [menu_order] => 
    [post_type] => 
    [post_mime_type] => 
    [comment_count] => 
    [filter] =>
)