-
edjarrett
I am using GeneratePress 3.3 theme, GP Premium 2.3.1, and GenerateBlocks Pro 1.5.3. I have created a custom post content template block, and it is working well. But I would like to do something with the Post Title but am having a hard time finding out if it is even possible, much less how to do it.
Most of my post titles are in the form of “Title – Reference”. I would like to be able to parse this out on the dash, just within my custom content block, as two elements that I can use as the post title and a post subtitle.
First of all, is this even possible? And, if so, how would I go about it? Thanks for any help you can give on this.
-
Hi there,
I’m not sure I fully understand your question.
Do you mean you want to create a subtitle as a post meta?
As we are discussing a question related to the GP premium feature, please also add your GP premium support key to your account, once the GP badge shows in your profile, we can keep on discussing this topic 🙂
Thanks!
-
edjarrett
I do want to create a subtitle, and I know how to do that using post meta. What I want to do is parse out a post title like “Receive Sight and Follow – Matthew 20:34”. I want to use “Receive Sight and Follow” as the Post Title in my Custom Content Block and “Matthew 20:34” as a subtitle. I need somehow to be able to separate that field into two, just within the Custom Content Block.
I added the other key.
-
Fernando
Hi Edjarrett,
Add
cu-post-titleto the classlist of the GB Headline Block for the title. Addcu-post-sub-titleto the classlist of the Headline Block for the subtitle.Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/
Then, add this snippet:
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, '-') + 1); } else { return $content; } } return $content; }, 10, 3);Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
-
edjarrett
I have added those three things. but something seems to be missing. I do not see the title parsed out.
I added two new headline blocks and gave then associated the additional class names with them. I added the code as a snippet that runs everywhere. But the headline blocks remain empty. I inspected the published post and see the new fields with the associated classes. But they are empty.
-
Fernando
I modified the code above. Can you try that instead?
-
edjarrett
That still does nothing that I can see. I hardcoded a text string as the return value for the function and still saw nothing. I have tried the blocks with dynamic data set to Title and dynamic data turned off. Still nothing. I cannot tell that the function is being referenced. Am I missing something?
-
edjarrett
FWIW, the post title is using an em dash rather than a hyphen in the title. That is what WordPress seems to want to change my hyphens (when used in titles) to. I updated your code to use an em dash, but it made no difference.
-
Fernando
That’s odd. I tested the code and it’s working for me. Can you share admin login credentials through the Private Information field? I’ll try to see why it isn’t working.
-
edjarrett
Login credentials below.
The snippet is Parse Title and Subtitle
The Element is Devotional Content Block
You can use this post to test with – https://aclayjar.net/2023/04/king-jesus/
If it works the reference should go away from the top. There is a headline block under the picture that I am using to temporarily test for the subtitle.I am heading to bed soon, so I catch back up with you tomorrow.
-
Fernando
Use the GB dynamic options instead of the one from GP in the toolbar.
The snippet I provided works for the GB dynamic options.
-
edjarrett
I thought I had tried it both ways. So I should use the option in the right column, setting both fields to Title?
-
Fernando
Yes, just use the one on the right. Unset the one from the toolbar. Let’s see how it goes.
-
edjarrett
No different. You can check to see if I did it right. But I really have to go now. THanks for your help.
-
Fernando
Your site has caching. It’s showing the cached version of the page where the code hasn’t been implemented yet. Try clearing your cache first.
I also updated the code again for testing purposes.
-
edjarrett
I have learned that I need to clear my cache every time I make a change to a block element. I have been doing that throughout this process. It has not helped in being able to have this change take effect. What I see is the unparsed post title. And that is true with an incognito window as well. Do you see the modified post title?
- You must be logged in to reply to this topic.