1

私はTwentyTwelveテーマに取り組んでおり、ループの前にこのスニペットを追加してインデックスファイルを変更しました

get_header(); ?>

<div id="primary" class="site-content">
    <div id="content" role="main" class="clearfix">
        <?php
             $terms = get_the_category();
             $count = count($terms);
             echo '<ul id="post-filter">';
                echo '<li><a href="#all" title="">All</a></li>';
                if ( $count > 0 ){

                    foreach ( $terms as $term ) {

                        $termname = strtolower($term->name);
                        $termname = str_replace(' ', '-', $termname);
                        echo '<li><a href="#'.$termname.'" title="" rel="'.$termname.'">'.$term->name.'</a></li>';
                    }
             }
             echo "</ul>";
        ?>
        <div id="mwrapper">

    <?php query_posts('cat=-6,-7'); ?>
    <?php if ( have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <div class="box">....

ブログ投稿をフィルタリングするフィルターを作成しようとしています。ここのデモのように。現在、エージェンシー ノート、デザイン ノート、おすすめ、ユーモア、未分類の 5 つのカテゴリがあります。カテゴリごとに少なくとも 1 つの投稿がありますが、デザイン ノートのみを取り上げているようです。

を変更してみget_the_category();ましwp_list_categories();たが、最終的にすべてのカテゴリが表示されました。

ソース私はスニペットを取得しています。

4

2 に答える 2

2

get_the_category()完全な WP インストールのカテゴリのリストではなく、現在の投稿のカテゴリ情報を取得します。

あなたが探しているのはget_categories()関数だと思います(コーデックスの詳細:http://codex.wordpress.org/Function_Reference/get_categories

<?php
     $categories=get_categories( array( 'order' => 'ASC', 'orderby' => 'name' ) );
     $count = count($terms);
     [...]
于 2013-04-30T16:22:55.700 に答える