web-dev-qa-db-ja.com

サブカテゴリからすべての投稿を除外

ワードプレスタスクで助けが必要

サブカテゴリからの投稿はすべて除外します。

例:

  • ケーキ
    • パイ
      • 林檎
      • バナナ

バナナに投稿したとしても、それをPieやCakeに表示したくないのです。バナナで投稿された投稿は、トップカテゴリーではなく、バナナで表示したいだけです。

これどうやってするの?

私はそれがfunctions.phpに入れるためのコードを見つけました、そしてそれは最初のカテゴリーでトリックを行いますが、2番目ではありません。

function fb_filter_child_cats($query) {
$cat = get_term_by('name', $query->query_vars['category_name'], 'category');
$child_cats = (array) get_term_children( &$cat->term_id, 'category' );
// also possible
// $child_cats = (array) get_term_children( get_cat_id($query->query_vars['category_name']), 'category' );
if ( !$query->is_admin )
$query->set( 'category__not_in', array_merge($child_cats) );
return $query;
}
add_filter( 'pre_get_posts', 'fb_filter_child_cats' );
2
Mwild

テンプレートを変更しないでください、そしてnotuse query_postsを実行してください。

これをあなたのfunction.phpに追加してください:

add_action('pre_get_posts', 'filter_out_children');

function filter_out_children( $query ) {
  if ( is_main_query() && is_category() && ! is_admin() ) {
     $qo = $query->get_queried_object();
     $tax_query = array(
       'taxonomy' => 'category',
       'field' => 'id',
       'terms' => $qo->term_id,
       'include_children' => false
     );
     $query->set( 'tax_query', array($tax_query) );
  }
}
2
gmazzap

最も簡単な方法は、カテゴリテンプレートを使用することです。

http://codex.wordpress.org/Category_Templates

基本的には、category.phpページを作成してから変更します。

<?php while ( have_posts() ) : the_post(); ?>

これに

<?php while (have_posts()) : the_post(); if (in_category($cat)) { ?>

そして

<?php endwhile; ?>

<?php } endwhile; ?>

詳細については http://motioncity.com.ar/2008/wordpress-tip-how-to-exclude-children-categories-on-a-template/ を参照してください。

1
Rohit Pande

今はテストできませんが、次のコードで試すことができます。

$current_cat = intval( get_query_var('cat') );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
   'category__and' => array($current_cat),
   'paged' => $paged,
   'caller_get_posts'=> 1
);
query_posts($args);

?>

<?php if (have_posts()) : ?>

   Your content - here!

wp_reset_query();

投稿を表示したいテンプレートファイルを編集して、このコードを追加する必要があります。

0
Dido Kotsev

あなたはこれを試すことができます、それは私のために働いた

<?php 

// Subcategories IDs as an array

// In this example, the parent category ID is 3
$subcategories = get_categories('child_of=3');

$subcat_array = array();
foreach ($subcategories as $subcat) {
    $subcat_array[] = '-' . $subcat->cat_ID;
}

// we also include parent category ID in the list
$subcat_array[] = '-3';

// and then call query_posts
query_posts(array('category__not_in' => $subcat_array));

?>

からコピー

http://www.maratz.com/blog/archives/2009/07/13/exclude-articles-from-a-category-tree-on-your-wordpress-homepage/

0
Asif Raza