如何使用 slug 从类别中获取帖子? [英] How do I get posts from category using the slug?

查看:36
本文介绍了如何使用 slug 从类别中获取帖子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的主题,我想在我的主页上显示来自特定类别的帖子.

I have my own theme and I'd like to display posts on my home page from a specific category.

到目前为止,我已经做到了:

So far I've achieved it like this:

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category' => 6 );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
?>

    <divs with the_title() the_excerpt() etc ></div>

<?php 
    endforeach; 
?>

但是如果我想通过它的 slug 来获取类别怎么办?或者是否可以简单地从管理面板中创建一个类别选择框?

But what if I want to get the category by a its slug? Or is it possible to simply make a category selection box in from within the admin panel?

推荐答案

将您的 category 参数替换为 category_name>

Replace your category parameter with category_name

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category_name' => 'cat-slug' );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
?>

<divs with the_title() the_excerpt() etc ></div>

<?php endforeach; ?>

更多信息:http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

这篇关于如何使用 slug 从类别中获取帖子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆