12

自分のテーマがあり、特定のカテゴリの投稿をホームページに表示したいと思います。

これまでのところ、私は次のようにそれを達成しました:

<?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; 
?>

しかし、そのスラッグでカテゴリを取得したい場合はどうなりますか?または、管理パネル内からカテゴリ選択ボックスを作成することは可能ですか?

4

3 に答える 3

43

categoryパラメータを次のように置き換えます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をご覧ください。

于 2012-11-03T08:32:50.280 に答える
4

カテゴリ名が「icecakes」でカテゴリスラッグが「ice-cakes」であるとすると、カテゴリ「icecakes」の下の投稿を取得するコードは次のようになります。

<?php
              $args = array( 'posts_per_page' => 3,
               'category_name' => 'ice-cakes' );

              $icecakes = get_posts( $args );
              foreach ( $icecakes as $post ) : setup_postdata( $post ); ?>
                  <li>
                      <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                  </li>
              <?php endforeach; 
              wp_reset_postdata(); ?>
于 2018-01-05T09:51:22.227 に答える
0

あなたget_postsのカテゴリーのスラッグがice-cake

$args = array('numberposts' => 10, 'category' => 'ice-cake');
$posts = get_posts($args);

詳細情報:https ://developer.wordpress.org/reference/functions/get_posts/

于 2019-09-21T08:28:35.170 に答える