0

ウィジェット カテゴリは、ダッシュボードのプライマリ サイドバーに配置しました。よりも、コードでは、私は使用します:

<?php get_sidebar('left'); ?>

カテゴリのコードを作成します。ここで、tag_ID=6 のカテゴリとそのすべてのサブカテゴリを非表示にしたいと思います。

どうすればいいですか?

このチュートリアルを試してみましたが、行がないよう$cat_args = "orderby=name&show_count={$c}&hierarchical={$h}";ですか? WordPress の最新バージョン 3.4.2 を使用しています

4

1 に答える 1

1

チュートリアルは時代遅れのように見えるので、私はそれに依存しません。WordPress ソースをハッキングする必要はありません。適切なフィルターにフックする単純なプラグインを作成します。

あなたの場合、これらのフィルターはwidget_categories_dropdown_args(ウィジェットオプションで「ドロップダウンとして表示」を選択した場合)およびwidget_categories_args(ウィジェットがリストをリンク付きの通常のテキストとして表示する場合)です。

その知識があれば、実際のプラグインをコーディングできます (私はそれを Myplugin と呼んでいます。名前を変更する必要があると思います) - その PHP コードをファイルに入れるだけですwp-content/plugins/myplugin.php:

<?php
/**
 * @package Myplugin
 * @version 1.0
 */
/*
Plugin Name: Myplugin
Plugin URI: http://example.com
Description: 
Author: You
Version: 1.0
Author URI: http://example.com
*/

// Create a list with the ID's of all children for 
// the given category-id
function myplugin_recursive_filter($catid) {
    $result = array($catid);

    $cats = get_categories(array(
        'child_of' => $catid,
    ));

    foreach($cats as $category) {
        $result[] = $category->cat_ID;
    }

    return implode(",", $result);
}

// Actual filter function. Just set the "exclude" 
// entry to a comma separated list of category ID's 
// to hide.
function myplugin_filter_categories_args($args) {
    // 6 is the "tag_ID"
    $args['exclude'] = myplugin_recursive_filter(6);

    // or hard code the list like that:
    //$args['exclude'] = '6,10,11,12';
    // but you'd have to include the ID's of the
    // children, because "eclude" is not recursive.
    return $args;
}

// Register the filter to the relevant tags
add_filter('widget_categories_dropdown_args',
    'myplugin_filter_categories_args', 10, 1);

add_filter('widget_categories_args',
    'myplugin_filter_categories_args', 10, 1);

-entry は再帰的ではないため、この関数myplugin_recursive_filterが必要です (ウィジェット オプションで [階層を表示] をオンにしている場合を除く)。excludeカテゴリがそれほど変わらない場合は、パフォーマンスを向上させるために、関数呼び出しを (子を含む) ID のハードコーディングされたリストに置き換えることができます。

于 2012-10-16T08:40:48.397 に答える