• Resolved Mindfill

    (@noahfischer)


    Hi, I’m building a child theme in Genesis, trying to modify code placed into the PHP Widget plugin to display posts titles from categories in a sidebar, but I need to exclude one category. I am new to PHP so although I can read about how its done in threads ,I don’t have enough general bearings on it to implement the exclude into what I pasted into my PHP widget and get anything working yet. (The rest is working fine) So…would much appreciate if anyone can take a look.

    Below I will paste the code I am using. My question: where to add in the exclude function (for category 1)?

    Basically I am trying to add something like this to my whole query:
    http://wordpress.org/support/topic/exclude-category-name-from-the-get_the_category-function?replies=10

    Here is my code: Thanks.

    <?php
    if ( is_single() ) {
    
    $categories = get_the_category();
    $currentID = get_the_ID();
    $parent = get_cat_name($category[0]->category_parent);
    
      if ($categories) {
        foreach ($categories as $category) {
          // echo "<pre>"; print_r($category); echo "</pre>";
    
          $cat = $category->cat_ID;
          $args=array(
            'cat' => $cat,
            'post__not_in' => array($post->ID),
    'post__not_in' => array($currentID),
    
            'posts_per_page'=>7,
            'caller_get_posts'=>1
          );
    
          $my_query = null;
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
    
     echo '<h4>more projects from</h3>';
    echo  $categories[0]->cat_name ;
    
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php  the_title_attribute(); ?>"><?php the_title();?></a></p>
             <?php
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
      } //if ($categories)
    
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
Viewing 1 replies (of 1 total)
  • Thread Starter Mindfill

    (@noahfischer)

    Ok, I figured it out from this thread:
    http://wordpress.stackexchange.com/questions/102989/how-to-exclude-specific-category-from-the-get-the-category-array

    I guess it’s not a complicated thing to exclude a category in this situation, just have to add one line:

    if($category->name !== ‘Uncategorized’)

    So the code now looks like this, and it works!

    <?php
    if ( is_single() ) {
    
    $categories = get_the_category();
    $currentID = get_the_ID();
    $parent = get_cat_name($category[0]->category_parent); 
    
      if ($categories) {
        foreach ($categories as $category) {
    
    if($category->name !== 'Uncategorized'){
    
          // echo "<pre>"; print_r($category); echo "</pre>";
    
          $cat = $category->cat_ID;
          $args=array(
            'cat' => $cat,
            'post__not_in' => array($post->ID),
    'post__not_in' => array($currentID),
    
            'posts_per_page'=>7,
            'caller_get_posts'=>1
          );
    
          $my_query = null;
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
    
     echo '<h4>more projects from</h3>';
    echo  $categories[0]->cat_name ;
    
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php  the_title_attribute(); ?>"><?php the_title();?></a></p>
             <?php
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
    }//$category->cat_ID !
      } //if ($categories)
    
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
Viewing 1 replies (of 1 total)

The topic ‘Exclude a Category from PHP Widget’ is closed to new replies.