1
votes

I'm working in Wordpress and implemented some PHP to display all categories on a page (by a shortcode). By clicking on a category it links to a new page displaying all posts of that very category.

How do I display only certain categories, for instance by id and/or name of the cateogry?

One post has two categories (a : A & B or A & C). So one post a always has one category A, and one category B or C.

Here's my code:

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' );

I tried this in the first few lines for example with no success:

function swerft_categories($args){
  ob_start();

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

  $categories = get_categories($args);

1) I want only one certain relation to be displayed. Let's say: only the relation of a : A & B 2) I want the count to only show the amount of posts based on the relation above. 3) By clicking on a category based on this relation, I want only those posts to be displayed of course.

1
Hey, thanks a lot for the link. I've found a solution meanwhile ;)Xandru

1 Answers

0
votes

I was able to find a solution for the above mentioned problem together with a colleague. See the code below, I hope it may help someone maybe; and thanks for anyone considering this task though ;)

// 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;
}