WordPress.org

Ready to get started?Download WordPress

Forums

List category posts
[resolved] List only posts that belong to two or more categories (Solution) (4 posts)

  1. hvianna
    Member
    Posted 1 year ago #

    First of all, thanks picandocodigo for the great plugin!

    With a few modifications I achieved a way to list posts that belong to two or more categories. Here's my solution:

    In the shortcode, use + instead of the comma to separate category IDs or names, like this:

    [catlist id=3+4] or [catlist name=sports+baseball] will list only posts that belong to both categories.

    You'll need to make the following modifications in the CatList.php file, inside the include folder.

    IMPORTANT - the changes proposed below are for the version 0.28 of the plugin. Make a backup copy of the original files!

    Locate this: (line 29 in the original file)

    $args = array('cat'=> $this->lcp_category_id);

    And change to:

    if (is_array($this->lcp_category_id))
      $args = array('category__and' => $this->lcp_category_id);
    else
      $args = array('cat'=> $this->lcp_category_id);

    Replace the function get_lcp_category() (lines 113-137 in the original file) for this:

    private function get_lcp_category(){
        if ( isset($this->params['categorypage']) &&
          $this->params['categorypage'] == 'yes' ):
          $this->lcp_category_id = $this->lcp_get_current_category();
        elseif ( !empty($this->params['name']) ):
          if (preg_match('/\+/', $this->params['name'])):
            $categories = array();
            $cat_array = explode("+", $this->params['name']);
            foreach ($cat_array as $category) :
              $id = $this->get_category_id_by_name($category);
              $categories[] = $id;
            endforeach;
            $this->lcp_category_id = $categories;
          elseif (preg_match('/,/', $this->params['name'])):
            $categories = '';
            $cat_array = explode(",", $this->params['name']);
            foreach ($cat_array as $category) :
              $id = $this->get_category_id_by_name($category);
              $categories .= $id . ",";
            endforeach;
            $this->lcp_category_id = $categories;
          else:
            $this->lcp_category_id = $this->get_category_id_by_name($this->params['name']);
          endif;
        elseif ( isset($this->params['id']) && $this->params['id'] != '0' ):
          if (preg_match('/\+/', $this->params['id']))
            $this->lcp_category_id = explode("+", $this->params['id']);
          else
            $this->lcp_category_id = $this->params['id'];
        endif;
      }

    http://wordpress.org/extend/plugins/list-category-posts/

  2. fernandobt
    Member
    Plugin Author

    Posted 1 year ago #

    Awesome, thanks! I'm adding some stuff to release a 0.29 version soon, so I'll check this out.

    Regards,

  3. fernandobt
    Member
    Plugin Author

    Posted 1 year ago #

    hvianna this has been added to version 0.29, again thank you very much! I see you have a user with the same handle on GitHub, check the plugin's repo over there and contribute as much code as you want :D

    GitHub's Pull Requests are awesome, and you get more credit for the code you contribute :)

    Thanks!

  4. hvianna
    Member
    Posted 1 year ago #

    Nice, glad I could help. And thank you again for the helpful plugin!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.