Site logo

Archived topic

Changing post title programmatically

8 replies · Started by Rekindle on October 4, 2021

Viewing posts 1–9 of 9

Hi, I'm trying to remove a 6 letter suffix from some of my post titles. I discovered that the_title also changes nav menu items in the process.

Searching here, I saw Tom's post: https://generatepress.com/forums/topic/change-page-title-programmatically/#post-1377184

Can you help me "add my own function into the page using Elements"?

By the way, one suggestion I have for your team: please add more documentation in this area, on how to combine PHP/ code snippets into Elements to achieve things. Some examples or links to threads would also be helpful.

Thanks!

Hi there,

The easy solution to this is to turn your nav menu items into custom links so you can add the labels manually. This way, the filter doesn't affect the nav menu labels.

The more tedious way of doing this is by using a child theme to create a template for your sfwd-quiz post type and create a content-sfwd-quiz.php copying the default content.php here - https://github.com/tomusborne/generatepress/blob/master/content-single.php - but edit this line - https://github.com/tomusborne/generatepress/blob/2c7c8a76d3b58b0b97e82b3a7a5ebf022a1ddfbe/content-single.php#L36-L40 - to do the substr() of get_the_title() instead of filtering it.

Thanks Elvin. Both of these seem like what I could do if I weren't using GeneratePress :-) What is the approach that uses Elements that Tom is referring to, in his post?

Thanks.

p.s. just to clarify, I'm not averse to using one of these 2 solutions if it solves my problem. But it seems that, with the first approach, someone else could accidentally use a Page nav menu item and not know why it's looking all messed up on the front end.

With the second approach, I'm worried that it's scattering where functionality is coming from. I have Elements, code snippets, plugins, and now a child theme template file. I'm hoping an Element should be able to do it so it's easier for me to keep track of all our changes.

Thanks.

Ah right yes that's actually a third option.

I believe the context of what Tom mentioned on the topic you've liked was:

To put simply, it's basically replacing the default title rendered by the theme with your own custom code placed on a Hook element hooked to specific hook (generate_before_entry_title) with a Display rule location set to your custom post type. :D

This way, the nav menu items will not be affected because you won't be using filters as you'll be inserting your own title.

Example: https://share.getcloudapp.com/04ux79Nq

The code -

<?php
$title = get_the_title(get_the_ID());
$title_without_code = substr( $title, 0, strlen( $title ) - 6 );

echo '<h1 class="entry-title">'. $title_without_code .'</h1>';
?>

Got it. Just one catch: my "Execute PHP" is turned off because of this: "Unable to execute PHP as DISALLOW_FILE_EDIT is defined.", probably from a security plugin.

Is there a way to put the PHP inside a code snippet and have it appear in the element? Or any other way around this?

Is there a way to put the PHP inside a code snippet and have it appear in the element? Or any other way around this?

We can skip the Hook element and do it manually with a code snippets plugin.

First, make sure the default title is disabled.

You then add this PHP snippet.

add_action('generate_before_entry_title',function(){
    if( get_post_type === 'sfwd-quiz' ) {
        $title = get_the_title(get_the_ID());
        $title_without_code = substr( $title, 0, strlen( $title ) - 6 );
        
        echo '<h1 class="entry-title">'. $title_without_code .'</h1>';
    }
});

This basically inserts the h1 element with the stripped title of the post on the same hook where the removed default title was added IF the current single post page's post type is sfwd-quiz.

Got it. Thanks Elvin!

No problem. :D

This archived topic is closed to new replies.