代码之家  ›  专栏  ›  技术社区  ›  Fernando Souza

列出WordPress特定类别的帖子url

  •  0
  • Fernando Souza  · 技术社区  · 7 年前

    有没有一种方法可以在没有插件的情况下列出WordPress中某个类别的所有帖子URL?

    2 回复  |  直到 7 年前
        1
  •  1
  •   git-e-up    7 年前

    回顾 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
    } 
    
        2
  •  1
  •   giolliano sulit    7 年前

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

    假设这是一个精简版本的WP_查询,它将返回您想要的帖子数组。

    $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] =>
    )