Archived topic
Post Badges & WP Show Posts
29 replies · Started by webcréateur on August 23, 2019
The snippet did not Work ... ):
So looks like that method won't work, the_title filter we are using cannot be specified where and where not to display. Hence the issue we are having as a lot of dynamic content use the_title for displaying content. Which is what WPSP and the sidebar links are doing.
Can you remove the second snippet and the CSS. We can keep the Custom Taxonomy and ill proivde some CSS to display the Badges. Let me know.
Can you also provide a list of the Badge names you're going to be using.
Hi there,
the second snippet and the CSS is removed.
At the beginning we will start with 5 Badges:
- Premium
- Positions
- Interview
- Letters
- Comment
So this CSS is for the WPSP and Archives:
/* Default set-up of badges */
body:not(.single) article[class*="post-badges"] h2:after {
color: #fff;
margin-left: 0.5em;
padding: 6px 4px;
font-size: 12px;
border-radius: 2px; vertical-align: top;
}
/* Individual rules for each badge - duplicate and change the slug in .post-badges-slug */
body:not(.single) .post-badges-premium h2:after{
content: 'Premium';
background-color: #f00;
}
Slight issue with the Single Post. The H1 is within the Header Element which means it is placed before the article container that carries the post-badges-slug class so we can't target it using this method. Let us know if the above works and is practical first and then we can look at some code for the single post.
THANK YOU!
This works fine on front page content area and seems to be practical. but this time the badge is not shown in sidebar widget area, also displayed with WP SHOW POSTS and WP recent Posts...
Unfortunately sidebar widgets don't carry taxonomy classes so can't be styled. Hence the original crazy idea to bend the_title() filter to our will ( and failed lol )
The simplest solution is to use one a WPSP list for your sidebar lists.
I don't know what to do anymore.
In theory our requirement is to put a simple badge behind certain posts.
WP Show Posts makes this difficult somehow, or...?
How can I show Badge on single posts?
To get this to work on single posts, we would need to first add the category classes to the <body> element.
Can you add this function and let us know?:
add_filter( 'body_class', function( $classes ) {
if ( is_single() ) {
global $post;
foreach( ( get_the_category( $post->ID ) ) as $category ) {
$classes[] = 'cat-' . $category->category_nicename;
}
}
return $classes;
} );
Thank you. I just added the function ...
Sorry the code that Tom provided only applies to the standard Categories. Can you replace it with this function:
function badge_taxonomy_in_body_class( $classes ){
if( is_singular() )
{
global $post;
$custom_terms = get_the_terms($post->ID, 'post-badges');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
$classes[] = 'badge-' . $custom_term->slug;
}
}
}
return $classes;
}
add_filter( 'body_class', 'badge_taxonomy_in_body_class' );
Once in i can test and provide you with the complete CSS to add the badges
Thank you ... just replaced the function.
Right made one tweak to this snippet:
the change is $classes[] = 'cat-' . $custom_term->slug; becomes $classes[] = 'badge-' . $custom_term->slug;
Once thats done update your CSS to include the H1 styling - so it now looks like this:
body:not(.single) article[class*="post-badges"] h2:after,
body[class*="badge-"] h1:after {
color: #fff !important;
margin-left: .5em;
padding: 2px 4px;
font-size: 12px;
border-radius: 0;
vertical-align: top
}
body:not(.single) .post-badges-premium h2:after,
.badge-premium h1:after {
content: 'Premium';
background-color: red
}
David, thank you for your patience with me.
With this solution I think we can work. :)
I marked this support topic as resolved.
But if you can think of anything else regarding the badges in the widget "Recent Post" I would be grateful.
Awesome - so glad we could be of help.
As i said with the Related Posts widget there is no category tag for us to target.
If you create a new WPSP list, then you can set that to display latest posts and add that as a sidebar widget. Then you're badges should display.