From a1fc62573bd2f521ec083cdc8768fc0c1f6962e4 Mon Sep 17 00:00:00 2001 From: Klemens Starybrat Date: Mon, 20 Aug 2018 09:43:03 +0200 Subject: [PATCH] Add support for multiple categories in categorypage --- doc/FAQ.md | 7 ------- include/lcp-category.php | 4 +++- tests/lcpcategory/test-currentCategory.php | 24 ++++++++++++++++++++-- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/doc/FAQ.md b/doc/FAQ.md index 1b01c6a..073809a 100644 --- a/doc/FAQ.md +++ b/doc/FAQ.md @@ -9,7 +9,6 @@ * [How do I remove the bullets from the list?](#bullets) * [The plugin doesn't work on servers with PHP < 5](#php5) * [Plugin could not be activated because it triggered a fatal error](#fatal-error) - * [If a post has many categories, categorypage=yes only detects one of them](#categorypage) ## How can I remove the hyperlink of the title of the post? @@ -124,9 +123,3 @@ protected accessors. Try updating your server or using an [older version](http:/ Please check: http://wordpress.stackexchange.com/questions/9338/list-category-posts-plugin-upgrade-fails-fatal-error/9340#9340 - -## If a post has many categories, categorypage=yes only detects one of them - -This is how it works in the current implementation, there are no shortcode parameters to change this behavior. -We have already received feature requests to make `categorypage=yes` work with multiple categories and it should be -implemented at some stage. diff --git a/include/lcp-category.php b/include/lcp-category.php index 69cced5..91186d7 100644 --- a/include/lcp-category.php +++ b/include/lcp-category.php @@ -71,7 +71,9 @@ public function current_category(){ $categories = get_the_category($post->ID); } if ( !empty($categories) ){ - return $categories[0]->cat_ID; + return implode(',', array_map(function($cat) { + return $cat->cat_ID; + }, $categories)); } else { return [0]; // workaround to display no posts } diff --git a/tests/lcpcategory/test-currentCategory.php b/tests/lcpcategory/test-currentCategory.php index 073c545..0906162 100644 --- a/tests/lcpcategory/test-currentCategory.php +++ b/tests/lcpcategory/test-currentCategory.php @@ -3,8 +3,10 @@ class Tests_LcpCategory_CurrentCategory extends WP_UnitTestCase { protected static $test_post; + protected static $test_post_2; protected static $test_page; protected static $test_cat; + protected static $test_cat_2; public static function wpSetUpBeforeClass($factory) { // Create some random categories @@ -18,17 +20,28 @@ public static function wpSetUpBeforeClass($factory) { )); } - // Create test category, test post and test page (no categories for page) + // Create test categories, test posts and test page (no categories for page) self::$test_cat = $factory->term->create(array( 'taxonomy' => 'category', 'name' => 'Lcp test cat' )); + self::$test_cat_2 = $factory->term->create(array( + 'taxonomy' => 'category', + 'name' => 'Lcp test cat 2' + )); + self::$test_post = $factory->post->create(array( 'post_title' => 'Lcp test post', 'post_category' => array(self::$test_cat) )); + // Post with 2 categories + self::$test_post_2 = $factory->post->create(array( + 'post_title' => 'Lcp test post', + 'post_category' => array(self::$test_cat, self::$test_cat_2) + )); + self::$test_page = $factory->post->create(array( 'post_title' => 'Lcp test page', 'post_type' => 'page' @@ -53,8 +66,15 @@ public function test_single_post_page() { $this->go_to('/?p=' . self::$test_post); $this->assertQueryTrue('is_singular', 'is_single'); - $this->assertSame(self::$test_cat, $lcpcategory->current_category()); + $this->assertSame((string) self::$test_cat, $lcpcategory->current_category()); $this->assertSame('Lcp test cat', get_category($lcpcategory->current_category())->cat_name); + + // More than one category + $cat_ID_1 = self::$test_cat; + $cat_ID_2 = self::$test_cat_2; + $this->go_to('/?p=' . self::$test_post_2); + $this->assertQueryTrue('is_singular', 'is_single'); + $this->assertSame("${cat_ID_1},${cat_ID_2}", $lcpcategory->current_category()); } public function test_single_page_with_no_categories() {