Archived topic
Strike through Post Title based on custom field value
14 replies · Started by Fergal on September 5, 2022
Hey there,
I'd like to be able to strike through post title text based on whether a custom field is true or false. I think I've identified the components to make this work, but having trouble putting it together:
- I've added a custom field "Job Filled" and will use the text values of "true" or "false"
- Can use php snippet to detect the custom field value like: if (get_post_meta($post->ID, 'Job Filled', true)) {... if true then add the css class
- Add the new CSS class to simple CSS with strike through value for the true class
I don't know how to complete the php and CSS portions though. Please help.
Thanks,
Fergal
Hi Fergal,
You can try adding this PHP:
add_filter( 'render_block', function( $block_content, $block ) {
if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-title' ) !== false ) {
$my_meta = get_post_meta($post->ID, 'Job Filled', true);
if( $my_meta ) {
$my_new_class = 'my-strikethrough';
$myreplace1 = 'class="';
$myinsert1 = 'class=" ' . $my_new_class . ' ';
$block_content = str_replace( $myreplace1, $myinsert1 , $block_content );
}
}
return $block_content;
}, 10, 2 );
Add my-title to the class list of the post title block.
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
Then, add this CSS in Additional CSS:
.my-strikethrough {.
text-decoration:line-through !important;
}
I will give this a try, thanks Fernando!
You're welcome Fergal!
Hey Fernando,
Sorry to circle back to this topic, but I'm now trying it out. Turns out the meta ID is 'filledexpired' and has either values of 1 or 0. 1 if the post title should apply strike through. I adapted your code as such, but it doesn't seem to be working. Any suggestions please? I've added the my-title class to the post title block and added the CSS code.
// strike through post titles if filledexpired == 1
add_filter( 'render_block', function( $block_content, $block ) {
if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'my-title' ) !== false ) {
$my_meta = get_post_meta($post->ID, 'filledexpired', true);
if( $my_meta == 1) {
$my_new_class = 'my-strikethrough';
$myreplace1 = 'class="';
$myinsert1 = 'class=" ' . $my_new_class . ' ';
$block_content = str_replace( $myreplace1, $myinsert1 , $block_content );
}
}
return $block_content;
}, 10, 2 );
Try to add this line:
var_dump( $my_meta );
under this:
$my_meta = get_post_meta($post->ID, 'filledexpired', true);
Let's first check if it's retrieving the field value and if it's correct.
Ok great. It is outputting bool(false).
I see. Glad you found the cause!
Actually, I'm not sure how to use this information :)
It is outputting bool(false) for all posts, even ones where 'filledexpired' has been set and I'd expect the value to be 1.
Any ideas?
I see. What plugin are you using for your custom fields? It would be best to reach out to them regarding this.
It is metabox.io. I got it working with their function $my_meta = rwmb_meta('filledexpired', get_the_ID());
However, I would like to use get_post_meta() that you provided so I've reached out to their support. I'll keep you posted.
I have a couple more dynamic class insertions that I could use your help with please. Want me to open a new topic for each one or can we knock them out one at a time here?
Thanks,
Fergal
On a side note,
replacing $post->ID with get_the_ID() seems to do the trick:
get_post_meta(get_the_ID(), 'filledexpired', true);
Glad you resolved the issue.
Yes, please start a new topic for the other issues.
Sounds great Fernando. I just started a new topic https://generatepress.com/forums/topic/add-opacity-to-image-and-display-text-based-on-custom-field-value
Alright! I'll reply there.