首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列出WordPress某些类别的帖子url

问列出WordPress某些类别的帖子url
EN

Stack Overflow用户
提问于 2017-11-01 03:38:35
回答 2查看 46关注 0票数 0

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-01 04:12:52

检查查询和WordPress循环。如果我正确地理解了您的查询(用php标记包装),这样的东西应该能工作:

代码语言:javascript
复制
$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
} 
票数 1
EN

Stack Overflow用户

发布于 2017-11-01 04:55:28

您可以使用get_posts (帖子)。

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

代码语言:javascript
复制
$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',
));

然后,您可以遍历这些帖子:

代码语言:javascript
复制
foreach($categoryPosts as $categoryPost) {
    // Your logic
}

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

代码语言:javascript
复制
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] =>
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47047861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档