Skip to content

Commit

Permalink
Merge pull request #1289 from INN/1288-related-topics-empty
Browse files Browse the repository at this point in the history
Do better detection of no related topics in Largo's category template
  • Loading branch information
benlk authored Sep 7, 2016
2 parents ecdc31c + deda1d0 commit 09367b1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
78 changes: 42 additions & 36 deletions inc/related-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,58 @@
* Show related tags and subcategories for each main category
* Used on category.php to display a list of related terms
*
* @since 1.0
* @since 0.5.5
* @return String HTML '' if there are no related topics or a UL if there are related topics
*/

function largo_get_related_topics_for_category( $obj ) {
$MAX_RELATED_TOPICS = 5;
$MAX_RELATED_TOPICS = 5;

if (!isset($obj->post_type)) {
$obj->post_type = 0;
}
if (!isset($obj->post_type)) {
$obj->post_type = 0;
}

if ( $obj->post_type ) {
if ( $obj->post_type == 'nav_menu_item' ) {
$cat_id = $obj->object_id;
}
if ( $obj->post_type ) {
if ( $obj->post_type == 'nav_menu_item' ) {
$cat_id = $obj->object_id;
}
} else {
$cat_id = $obj->cat_ID;
}

}else {
$cat_id = $obj->cat_ID;
}
// spit out the subcategories
$outarray = array();
$cats = _subcategories_for_category( $cat_id );

$out = "<ul>";

$title_ul = apply_filters( 'largo_related_topics_title_ul', __( 'Related Topics:' , 'largo' ) );
$out .= '<li><strong>' . $title_ul . '</strong></li>';

// spit out the subcategories
$cats = _subcategories_for_category( $cat_id );

foreach ( $cats as $c ) {
$out .= sprintf( '<li><a href="%s">%s</a></li>',
get_category_link( $c->term_id ), $c->name
);
}
foreach ( $cats as $c ) {
$outarray[] = sprintf( '<li><a href="%s">%s</a></li>',
get_category_link( $c->term_id ), $c->name
);
}

if ( count( $cats ) < $MAX_RELATED_TOPICS ) {
$tags = _tags_associated_with_category( $cat_id,
$MAX_RELATED_TOPICS - count( $cats ) );
if ( count( $cats ) < $MAX_RELATED_TOPICS ) {
$tags = _tags_associated_with_category( $cat_id,
$MAX_RELATED_TOPICS - count( $cats ) );

foreach ( $tags as $t ) {
$out .= sprintf( '<li><a href="%s">%s</a></li>',
get_tag_link( $t->term_id ), $t->name
);
}
}
foreach ( $tags as $t ) {
$outarray[] = sprintf( '<li><a href="%s">%s</a></li>',
get_tag_link( $t->term_id ), $t->name
);
}
}

$out = '';

// Generate the <ul>
if ( count( $outarray ) > 0 ) {
$out = "<ul>";
$title_ul = apply_filters( 'largo_related_topics_title_ul', __( 'Related Topics:' , 'largo' ) );
$out .= '<li><strong>' . $title_ul . '</strong></li>';
$out .= join( '', $outarray );
$out .= "</ul>";
}

$out .= "</ul>";
return $out;
return $out;
}

function _tags_associated_with_category( $cat_id, $max = 5 ) {
Expand Down
6 changes: 3 additions & 3 deletions partials/archive-category-related.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

// category pages show a list of related terms
if ( defined( 'SHOW_CATEGORY_RELATED_TOPICS' ) && SHOW_CATEGORY_RELATED_TOPICS ) {
if ( is_category() && largo_get_related_topics_for_category( get_queried_object() ) != '<ul></ul>' ) { ?>
if ( is_category() && largo_get_related_topics_for_category( get_queried_object() ) != '' ) { ?>
<div class="related-topics">
<?php echo largo_get_related_topics_for_category( get_queried_object() ); ?>
</div>
<?php
<?php
}
}
}
4 changes: 4 additions & 0 deletions tests/inc/test-related-content.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ function setUp() {
function test_largo_get_related_topics_for_category() {
$this->markTestIncomplete('This test has not been implemented yet.');

// if a category has no related topics (in cats or tags), it outputs empty string
// if a category has fewer than 5 related categories, and it has tags, it backfills with tags
// if it has 5 related categories and any number of tags, it just outputs categories
// in any case where there are related terms, the output contains apply_filters( 'largo_related_topics_title_ul', __( 'Related Topics:' , 'largo' ) );
}

function test__tags_associated_with_category() {
Expand Down

0 comments on commit 09367b1

Please sign in to comment.