【问题标题】:How to display only certain categories in Wordpress如何在 Wordpress 中仅显示某些类别
【发布时间】:2019-11-26 10:38:06
【问题描述】:

我在 Wordpress 中工作并实现了一些 PHP 来显示页面上的所有类别(通过简码)。通过单击一个类别,它会链接到一个显示该类别所有帖子的新页面。

如何仅显示某些类别,例如按类别的 id 和/或名称?

一篇文章有​​两个类别(a:A & B 或 A & C)。所以一篇帖子a总是有一个类别A,一个类别BC

这是我的代码:

function swerft_categories(  ){
  ob_start(); 
  $categories = get_categories();

  echo '<div class="swerft_cat">';
  foreach($categories as $category) {
    echo '<div class="swerft_cat_single col-lg-3 col-md-3 col-sm-6 col-xs-12">';

      echo '<div class="swerft_cat_single_inner">';

        $thim_group_custom_title_bg_img = get_term_meta( $category->term_id, 'thim_group_custom_title_bg_img', true );
        if ($thim_group_custom_title_bg_img) {
          $image_id = $thim_group_custom_title_bg_img['id'];

          if ($image_id) {
            $post_thumbnail_img = wp_get_attachment_image_src( $image_id, 'full' );
            echo '<a href="' . get_category_link($category->term_id) . '"><img src="' . $post_thumbnail_img[0] . '" alt="' . $category->name . '" /></a>';
          }
        }

        echo '<a href="' . get_category_link($category->term_id) . '"><h5>'. $category->name .'</h5></a>';
        echo '<p>'. $category->description . $category->count . '<span> Seminare </span>' . '</p>';

      echo '</div>';

    echo '</div>';
  }
  echo '</div>';

  return ob_get_clean();
}
add_shortcode( 'swerft_categories', 'swerft_categories' );

例如,我在前几行中尝试过,但没有成功:

function swerft_categories($args){
  ob_start();

  $args = array('hide_empty'=> 1,
                'name' => 'B');

  $categories = get_categories($args);

1) 我只想显示一个特定的关系。假设:只有 a 的关系:A & B 2)我希望计数仅显示基于上述关系的帖子数量。 3)通过点击基于这种关系的类别,我当然希望只显示那些帖子。

【问题讨论】:

标签: php wordpress get categories


【解决方案1】:

我与一位同事一起为上述问题找到了解决方案。请参阅下面的代码,我希望它可能对某人有所帮助;并感谢任何考虑这项任务的人;)

// Function to only show individual seminars after click on category on category-page

function swerft_filter_posts_open_individual_seminare( $query ) {
  $offene_seminare = isset($_GET['offene_seminare']) ? boolval($_GET['offene_seminare']) : false;
  $individual_seminars_category_id = 91;
  if($query->is_category() && $query->is_main_query()) {
    if ($offene_seminare) {
      $query->set( 'category__not_in', $individual_seminars_category_id );
    } else {
      $query->set( 'category__in', $individual_seminars_category_id );
    }
  }
}
add_action( 'pre_get_posts', 'swerft_filter_posts_open_individual_seminare' );

// Function to only count individual seminars in overview

function swerft_count_individual_seminars_in_category($category_id) {
  $individual_seminars_category_id = 91;
  $query_args = array(
    'post_type'       => 'post',
    'category__and' => array($individual_seminars_category_id, $category_id)
  );
  $query = new WP_Query( $query_args );
  $count = $query->found_posts;
  return $count;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多