-
ryan92
HI
I checked the generate_get_the_title_parameters function but I am not able to modify the content of the <h2> tag.add_filter( 'generate_get_the_title_parameters', function( $params ) { if ( ! is_singular() ) { $params = array( 'before' => sprintf( '<h3 class="entry-title"%2$s><a href="%1$s" rel="bookmark">', esc_url( get_permalink() ), 'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : '' ), 'after' => '</a></h3>', ); } return $params; } );Is there any function/hook that would allow me to modify the h2 tag on the archive pages? I can’t figure out how to use the function to modify the title output.
-
Hi there,
What is your goal? To change the h2s on archives to h3?
Let me know 🙂
-
ryan92
Hello
My goal is to modify the content inside <h2></h2>. To modify the post titles on category page. -
Alvind
Hi there,
Could you link me to one of the archive pages?
-
ryan92
I can’t share my website publicly but I’m showing the html structure
so in my site there is a category /category/recipes/
in there the post titles are displayed as such<
<article id="post-569182" class="post-569182 post type-post status-publish format-standard hentry category-recipes"> <div class="inside-article"> <header class="entry-header"> <h2 class="entry-title"><a href="link-to-recipe/" rel="bookmark">RECIPE TITLE LONG</a></h2> <div class="entry-meta"> <span class="posted-on"><time class="entry-date published" datetime="2024-10-31T14:30:21+00:00">October 31, 2024</time></span> </div> </header> <div class="entry-content"> <p><b>This is a recipe for...</b>.</p> </div> </div> </article>So I want to be able to change the content of the <h2> tag from
<h2 class="entry-title"><a href="link-to-recipe/" rel="bookmark">RECIPE TITLE LONG</a></h2>
to
<h2 class="entry-title"><a href="link-to-recipe/" rel="bookmark">RECIPE MODIFIED</a></h2> -
Do you want to change this one specific post title?
I do not understand, why not just change the post title directly?
-
ryan92
I want to shorten some post titles on the paginated pages but I want the full titles on single posts.
Can I modify the appearance of the <h2> titles on the paginated pages?
-
what do you mean by shortening, like how?
Something like limiting the title length to 5 words?
-
ryan92
Hi
As I explained
So I want to be able to change the content of the <h2> tag from
<h2 class=”entry-title”>RECIPE TITLE LONG</h2>
to
<h2 class=”entry-title”>RECIPE MODIFIED</h2>I want to be able to modify the title tag, only on the category/paginated page. Not in the post itself.
-
Hi there,
where would the modified text come from ? eg. a Custom Field ?
-
ryan92
Hi David
I want to intercept the loop in the paginated page. I simply want to replace or explode and then display part of the title as its fetched from the db.
Can I do that? -
The template simply uses the
get_the_title()function to display the title.
And you can filter the title content with thethe_titlehook:https://developer.wordpress.org/reference/hooks/the_title/
For example:
function my_archive_title_filter($title, $id = null) { // Check if we are in an archive and within the main query loop if (is_archive() && in_the_loop() && is_main_query()) { // Modify the title as needed for archive pages $title = 'Archive: ' . $title; } return $title; } add_filter('the_title', 'my_archive_title_filter', 10, 2); -
ryan92
Hello David
I don’t mean the h1 archive title. I mean to intercept the content of the h2 titles on the category paginated page. -
ryan92
What you provided modifies the header title of the paginated page I want to change
<h2>Cocktail 1 Long title</h2>
excerpt
—
<h2>Cocktail 2 Long title</h2>
excerpt
—
<h2>Cocktail 3 Long title</h2>
excerpt
—to maybe be able to display them as
<h2>Cocktail 1</h2>
excerpt
—
<h2>Cocktail 2</h2>
excerpt
—
<h2>Cocktail 3</h2>
excerpt
—I hope I am more clear now.
-
David’s code is for the h2 titles on the archive page. you can add
is_paged()condition as well.As for how to shorten the title, you need to write your own logic.
function my_archive_title_filter($title, $id = null) { // Check if we are in an archive and within the main query loop if (is_archive() && in_the_loop() && is_main_query() && is_paged()) { //Write your own logic to shorten the title $title = 'Archive: ' . $title; } return $title; } add_filter('the_title', 'my_archive_title_filter', 10, 2); -
ryan92
Thanks
Finally the answer I needed. There is no hook for this.
- You must be logged in to reply to this topic.