Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do better detection of no related topics in Largo's category template #1289

Merged
merged 4 commits into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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