• Hi,

    Learning WP and eventually want to build a site. I want to display only the first 2 words of a post title to the user but full title needs to be in the source for SEO. For example, if I make a recipe website and one of my recipes is “Vegetarian Burger – beans or tofu or veggies” but on archive pages or custom pages using a query loop, I only want to display “Vegetarian Burger” and not the entire title.

    How would this be achieved? I tried ChatGPT and it gave some php code and instructed me to paste that in the functions.php file. I read somewhere on this forum that Twenty Twenty Four theme doesn’t use functions.php. So I have no idea how to go about achieving this. Building the site with purely 2024 blocks so the title is H1 class=”wp-block-post-title

    There have been some examples of this but these are quite old posts on various websites like stachoverflow etc. Not sure if any new solution is there. I am not a developer so I am not sure who to go about it.

    Thanks.

    • This topic was modified 7 months, 3 weeks ago by pradg.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello there @pradg !

    Learning WP and eventually want to build a site.

    That’s great, welcome!

    ?I want to display only the first 2 words of a post title to the user but full title needs to be in the source for SEO. For example, if I make a recipe website and one of my recipes is “Vegetarian Burger – beans or tofu or veggies” but on archive pages or custom pages using a query loop, I only want to display “Vegetarian Burger” and not the entire title.

    With what you’d like to do, Categories may be a good fit. For example, make “Vegetarian Burger” a post category. And assign the post “beans or tofu or veggies” to the “Vegetarian Burger” category.
    You can also create an archive page just with categories using the Categories Block, so on that page only the category name “Vegetarian Burger” is visible, and only then when you click “Vegetarian Burger” will open and display all posts under that category like the “beans” post.

    If you’re referring to something else, please share more details or examples.

    Thread Starter pradg

    (@pradg)

    Hi @kel-dc

    With what you’d like to do, Categories may be a good fit. For example, make “Vegetarian Burger”?a post category. And assign the post “beans or tofu or veggies” to the “Vegetarian Burger” category.
    You can also create an archive page just with categories using the?Categories Block, so on that page only the category name “Vegetarian Burger” is visible, and only then when you click “Vegetarian Burger” will open and display all posts under that category like the “beans” post.
    If you’re referring to something else, please share more details or examples.

    Thanks for responding. Categories is not what I am looking for. I am looking for a solution to just show the first 2 words of the post title when the posts are displayed in a grid on a custom page or an archive page. Whatever the post title might be (let’s say post title is “How to display only the first 2 words of a post title”, I only want the end user to see “How to” on the site), the end user should only see the first 2 words of the title but the full title should be present in the source for SEO.

    Unsure of how to do it with code as I am a non-developer. I would say logically this will be some code which will scan the post title and split it into as many words it is and creates a new string containing only the first two words and then that is displayed on the website.

    Thanks,

    Prad

    Hey @pradg! Consistency might be something to consider here. If site visitors expect to see the full post title, but only see a shortened version, it could cause confusion.

    Can you share more details why you’d like to only show the first two texts of the post title “How to” instead of the full post title “How to display only the first 2 words of a post title”?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to display only the first 2 words of a post title’ is closed to new replies.