0

大家好! 好吧,我有这段代码,它显示了每个类别的一篇文章,但我只需要显示一篇文章,并且需要设置我正在尝试的类别的“名称”:

    <?php 
$cat_args = array(
  'orderby' => 'name',
  'order' => 'ASC',
  'child_of' => 0
);

$categories =   get_categories($cat_args); 

foreach($categories as $category) { 
    echo '<dl>';
    echo '<dt> <a href="' . get_category_link( $category->name ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';

     $post_args = array(
      'numberposts' => 1,
      'category' => $category->term_id 
    );

    $posts = get_posts($post_args);

    foreach($posts as $post) {
    ?>
        <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
                <div class="entry">
                            <?php the_content(); ?>
                        </div>
    <?php 
    } 
    echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
    echo '</dl>';
} 
?>

然后我搜索了法典并没有找到按名称获取类别的方法,有人可以帮助我吗?

4

1 回答 1

1

您正在寻找的是get_cat_ID

更改$post_args为这样的内容:

$post_args = array(
    'posts_per_page' => 1,
    'cat' => get_cat_ID( 'My Category Name' )
);

让我知道事情的后续。:)

于 2013-02-08T20:05:50.610 回答