diff --git a/inc/related-content.php b/inc/related-content.php
index bab039e71..d0d8fa3af 100644
--- a/inc/related-content.php
+++ b/inc/related-content.php
@@ -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 = "
";
-
- $title_ul = apply_filters( 'largo_related_topics_title_ul', __( 'Related Topics:' , 'largo' ) );
- $out .= '- ' . $title_ul . '
';
-
- // spit out the subcategories
- $cats = _subcategories_for_category( $cat_id );
-
- foreach ( $cats as $c ) {
- $out .= sprintf( '- %s
',
- get_category_link( $c->term_id ), $c->name
- );
- }
+ foreach ( $cats as $c ) {
+ $outarray[] = sprintf( '- %s
',
+ 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( '- %s
',
- get_tag_link( $t->term_id ), $t->name
- );
- }
- }
+ foreach ( $tags as $t ) {
+ $outarray[] = sprintf( '- %s
',
+ get_tag_link( $t->term_id ), $t->name
+ );
+ }
+ }
+
+ $out = '';
+
+ // Generate the
+ if ( count( $outarray ) > 0 ) {
+ $out = "";
+ $title_ul = apply_filters( 'largo_related_topics_title_ul', __( 'Related Topics:' , 'largo' ) );
+ $out .= '- ' . $title_ul . '
';
+ $out .= join( '', $outarray );
+ $out .= "
";
+ }
- $out .= "
";
- return $out;
+ return $out;
}
function _tags_associated_with_category( $cat_id, $max = 5 ) {
diff --git a/partials/archive-category-related.php b/partials/archive-category-related.php
index 6a53663f5..332880131 100644
--- a/partials/archive-category-related.php
+++ b/partials/archive-category-related.php
@@ -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() ) != '' ) { ?>
+ if ( is_category() && largo_get_related_topics_for_category( get_queried_object() ) != '' ) { ?>
-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() {