Site logo

[Resolved] Strike through Post Title based on custom field value

Home Forums Support [Resolved] Strike through Post Title based on custom field value

Home Forums Support Strike through Post Title based on custom field value

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #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:

    1. I’ve added a custom field “Job Filled” and will use the text values of “true” or “false”
    2. 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
    3. 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

    #2334361
    Fernando
    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-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;
    }
    #2336438
    Fergal

    I will give this a try, thanks Fernando!

    #2336490
    Fernando
    Customer Support

    You’re welcome Fergal!

    #2341174
    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 );
    #2341175
    Fernando
    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.

    #2341183
    Fergal

    Ok great. It is outputting bool(false).

    #2341186
    Fernando
    Customer Support

    I see. Glad you found the cause!

    #2341189
    Fergal

    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?

    #2341192
    Fernando
    Customer Support

    I see. What plugin are you using for your custom fields? It would be best to reach out to them regarding this.

    #2341879
    Fergal

    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

    #2341922
    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);

    #2342171
    Fernando
    Customer Support

    Glad you resolved the issue.

    Yes, please start a new topic for the other issues.

    #2342181
    Fergal
    #2342186
    Fernando
    Customer Support

    Alright! I’ll reply there.

Viewing 15 posts - 1 through 15 (of 15 total)
  • You must be logged in to reply to this topic.