Skip to content

Commit

Permalink
Add support for multiple categories in categorypage
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-st committed Oct 7, 2018
1 parent c413cdf commit a1fc625
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
7 changes: 0 additions & 7 deletions doc/FAQ.md
Expand Up @@ -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)

## <a name="no_link"></a>How can I remove the hyperlink of the title of the post?

Expand Down Expand Up @@ -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

## <a name="categorypage"></a>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.
4 changes: 3 additions & 1 deletion include/lcp-category.php
Expand Up @@ -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
}
Expand Down
24 changes: 22 additions & 2 deletions tests/lcpcategory/test-currentCategory.php
Expand Up @@ -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
Expand All @@ -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'
Expand All @@ -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() {
Expand Down

0 comments on commit a1fc625

Please sign in to comment.