Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Parsing Post Title

  • Hi there,

    this is what i see on your example post:

    2023-04-22_12-05-18

  • Yes, that is what it has always been. I am trying to change it to something like this

    King Jesus
    Acts 17:6-7

    At least a part of the problem with the code that Fernando provided is that the “dash” between Jesus and Acts is not a regular hyphen, although that is what I entered. I read last night before I quit for the day that WordPress converts a “space dash space” into a “space en-dash space”. I do not know PHP, so am struggling to find the right combination of functions to be able to split my title on the en-dash.

  • Yeah, you have to love WP for doing that
    So in Fernandos code where you see '-' you can replace that with a unicode

    If its the space en-dash space then you can use ' \u2013 ' in its place.

  • Thanks. This is better. But it is now returning

    King
    ing Jesus – Acts 17:6-7

  • OK, I think I have this figured out. WordPress was displaying an en-dash in the title. But that is not what is encoded in the string passed to this function. Instead of “King Jesus – Acts 17:6-7”, it was “King Jesus – Acts 17:6-7”. I have to look at every character individually to discover that. So now I search for the & for the first part and jump over 7 characters for the second part. And that seemed to do the trick.

    add_filter(‘generateblocks_dynamic_content_output’, function($content, $attributes, $block){
    if ( !is_admin() && ! empty( $attributes[‘className’] ) ) {
    if( strpos( $attributes[‘className’], ‘cu-post-title’ ) !== false ) {
    $content = strtok($content, “&”);
    } else if (strpos( $attributes[‘className’], ‘cu-post-sub-title’ ) !== false){
    $content = substr($content, strpos($content, “&”) + 7);
    } else {
    return $content;
    }

    }
    return $content;
    }, 10, 3);

  • I realized that if I had a & in my title, that I would have problems with the code above. So I changed it to below, and it still works. Thanks for all your help with this.

    add_filter(‘generateblocks_dynamic_content_output’, function($content, $attributes, $block){
    if ( !is_admin() && ! empty( $attributes[‘className’] ) ) {
    if( strpos( $attributes[‘className’], ‘cu-post-title’ ) !== false ) {
    $content = strtok($content, “–”);
    } else if (strpos( $attributes[‘className’], ‘cu-post-sub-title’ ) !== false){
    $content = substr($content, strpos($content, “–”) + 7);
    } else {
    return $content;
    }

    }
    return $content;
    }, 10, 3);

  • Well that was kind of a uphill battle, as these kinds dash replacements seem to always create. But i am glad to see you found one that works!

  • Yes it was. I did test this with a post title that had an & in it. And it broke there. So I replaced the & with the word “and” and will just try and remember that I cannot use that symbol in titles any longer.

    Thanks again for your help. Not sure I would have made it without the original code snippet.

  • Glad you found a solution. You’re welcome!

Viewing 9 posts - 17 through 25 (of 25 total)
  • You must be logged in to reply to this topic.