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

Homepage Posts: add category, tag classes to each article #487

Merged
merged 3 commits into from
May 28, 2020

Conversation

laurelfulford
Copy link
Contributor

All Submissions:

Changes proposed in this Pull Request:

This PR adds classes based on each post's assigned category and tag to the <article> tag, using the same format that is output by the [post_class()](https://developer.wordpress.org/reference/functions/post_class/) function. (I opted not to use that function as it's more likely to output classes -- like type-post or has-post-thumbnail -- that themes could already be using. The tag and category classes are most likely to be used on a site-by-site basis to customize specific taxonomies).

This change will make it possible to add custom CSS to posts with specific categories or tags within a block, rather than assigning custom CSS to all posts in a block based on a class added to that block.

Closes #363.

How to test the changes in this Pull Request:

  1. Apply the PR and run npm run build.
  2. Add a Homepage Posts block to a page and publish.
  3. Inspect the block in the editor and confirm that each <article> tag has classes assigned based on the categories the post has (using the format category-[slug]), and classes assigned based on the tags the post has (using the format tag-[slug]).
  4. Inspect the block on the frontend and confirm that each <article> tag has the same classes assigned based on each post's categories and tags.

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?

foreach ( $tags as $tag ) {
$classes[] = 'tag-' . $tag->slug;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Could this logic of assigning tag and category classes be moved to Newspack_Blocks (like prepare_authors is) to avoid duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that's a good call, Adam! I've updated it to live in Newspack_Blocks in 04f01c3.

Copy link
Member

@adekbadek adekbadek left a comment

Choose a reason for hiding this comment

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

Looking good, there's just a doc comment change needed!

@@ -411,6 +411,32 @@ public static function prepare_authors() {
);
}

/**
* Prepare an array classes based on assigned tags.
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick: tags and categories ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gah, thanks for catching that Adam! 🤦‍♀️ Fixed in 2bf0c3d.

* Prepare an array classes based on assigned tags.
*
* @param string $post_id Post ID.
* @return array Array of CSS classes.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @return array Array of CSS classes.
* @return string CSS classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Fixed in 2bf0c3d, too -- the comment cleanup commit 😄

Copy link
Member

@adekbadek adekbadek left a comment

Choose a reason for hiding this comment

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

👍

@laurelfulford
Copy link
Contributor Author

Yay! Thanks for help hammer this into shape!

@laurelfulford laurelfulford merged commit d7c4fcd into master May 28, 2020
@laurelfulford laurelfulford deleted the feat/363-add-cat-tag-classes branch May 28, 2020 20:38
matticbot pushed a commit that referenced this pull request Jun 2, 2020
# [1.7.0](v1.6.0...v1.7.0) (2020-06-02)

### Features

* add category and tag classes to each homepage post ([#487](#487)) ([d7c4fcd](d7c4fcd))
@matticbot
Copy link
Contributor

🎉 This PR is included in version 1.7.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.

Homepage Posts: Add classes for posts categories and tags
3 participants