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

fix: only show category link anchor tags if the category has a link #863

Merged
merged 2 commits into from
Sep 8, 2021

Conversation

dkoo
Copy link
Contributor

@dkoo dkoo commented Sep 8, 2021

All Submissions:

Changes proposed in this Pull Request:

Homepage Posts and Post Carousel front-end templates render category terms with a link. This omits the a anchor tag in the unlikely event that a term lacks a link. This could happen if the term being rendered is a "pseudo term", or a term that's appended programmatically to a post's term output based on some condition other than a true taxonomy term applied to the post.

The use case this comes from: a "featured" post meta toggle which programmatically shows a "featured" category label alongside the post's other categories. In this case the "featured" category label is not tied to a true category term, therefore it has no term link.

Bonus: also ensures that if a post with no categories (not even "Uncategorized") gets rendered by Homepage Posts or Post Carousel with categories enabled, the editor renders nothing instead of 0.

How to test the changes in this Pull Request:

  1. Set up code to add a "pseudo term" to a post. In your theme's functions.php, temporarily add the following filter:
function newspack_add_pseudo_label( $categories ) {
	$pseudo_term = new \WP_Term(
		(object) [
			'name'     => 'Pseudo Category',
			'slug'     => 'my-fake-category',
			'taxonomy' => 'category',
		]
	);

	// Add pseudo label in front of true categories.
	array_unshift( $categories, $pseudo_term );

	return $categories;
}
add_filter( 'get_the_categories', 'newspack_add_pseudo_label' );
  1. Add Homepage Posts and Post Carousel blocks to a post. Ensure that both blocks render some posts.
  2. On master, observe on the front-end that the "Pseudo Category" labels get rendered with empty anchor tags (<a href="">Pseudo Category</a>).
  3. Check out this branch, refresh, observe that the "Pseudo Category" labels are rendered as styled plain text without links.

Test no categories

  1. Modify your filter above to render no categories, not even the default category:
function newspack_add_pseudo_label( $categories ) {
	return [];
}
add_filter( 'get_the_categories', 'newspack_add_pseudo_label' );
  1. Add Homepage Posts and Post Carousel blocks to a post. Ensure that both blocks render some posts and that "Show Categories" is turned on.
  2. On master, observe that items in the editor show 0 in place of categories:

Screen Shot 2021-09-08 at 12 02 34 PM

  1. Check out this branch, refresh the editor, confirm that nothing is shown instead of 0.

Other information:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran tests with your changes locally?

@dkoo dkoo added the enhancement New feature or request label Sep 8, 2021
@dkoo dkoo requested a review from a team September 8, 2021 17:56
@dkoo dkoo self-assigned this Sep 8, 2021
Copy link
Member

@miguelpeixe miguelpeixe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working as described! 💯

@dkoo dkoo merged commit 549f03a into master Sep 8, 2021
@dkoo dkoo deleted the fix/no-link-categories branch September 8, 2021 18:46
matticbot pushed a commit that referenced this pull request Sep 8, 2021
# [1.37.0-alpha.2](v1.37.0-alpha.1...v1.37.0-alpha.2) (2021-09-08)

### Bug Fixes

* only show category link anchor tags if the category has a link ([#863](#863)) ([549f03a](549f03a))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.37.0-alpha.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

matticbot pushed a commit that referenced this pull request Sep 8, 2021
# [1.37.0](v1.36.0...v1.37.0) (2021-09-08)

### Bug Fixes

* currency symbol handling ([#850](#850)) ([9637d89](9637d89))
* only show category link anchor tags if the category has a link ([#863](#863)) ([549f03a](549f03a))
* show all saved post types in specific posts field ([#857](#857)) ([42c25a9](42c25a9))

### Features

* make Donate Block 'thank you' text editable ([#854](#854)) ([7e8f1ea](7e8f1ea))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.37.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants