- This topic has 14 replies, 2 voices, and was last updated 3 years, 8 months ago by
Fernando.
-
AuthorPosts
-
September 5, 2022 at 11:08 am #2334150
Fergal
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,
FergalSeptember 5, 2022 at 7:56 pm #2334361Fernando Customer Support
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-titleto 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; }September 7, 2022 at 5:24 pm #2336438Fergal
I will give this a try, thanks Fernando!
September 7, 2022 at 7:57 pm #2336490Fernando Customer Support
You’re welcome Fergal!
September 12, 2022 at 9:43 pm #2341174Fergal
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 );September 12, 2022 at 9:46 pm #2341175Fernando Customer Support
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.
September 12, 2022 at 9:58 pm #2341183Fergal
Ok great. It is outputting bool(false).
September 12, 2022 at 10:00 pm #2341186Fernando Customer Support
I see. Glad you found the cause!
September 12, 2022 at 10:11 pm #2341189Fergal
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?
September 12, 2022 at 10:15 pm #2341192Fernando Customer Support
I see. What plugin are you using for your custom fields? It would be best to reach out to them regarding this.
September 13, 2022 at 9:10 am #2341879Fergal
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,
FergalSeptember 13, 2022 at 9:58 am #2341922Fergal
On a side note,
replacing $post->ID with get_the_ID() seems to do the trick:
get_post_meta(get_the_ID(), ‘filledexpired’, true);
September 13, 2022 at 5:00 pm #2342171Fernando Customer Support
Glad you resolved the issue.
Yes, please start a new topic for the other issues.
September 13, 2022 at 5:24 pm #2342181Fergal
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
September 13, 2022 at 5:34 pm #2342186Fernando Customer Support
Alright! I’ll reply there.
-
AuthorPosts
- You must be logged in to reply to this topic.