Archived topic
Hook inside content
21 replies · Started by epickenyan on March 2, 2019
Hi Tom. Did you ever get a solution to this?
Hi there,
there is this solution where the user wanted the featured image appearing after the 2nd paragraph, the same principles apply with any content you wish to add:
https://generatepress.com/forums/topic/featured-image-inside-the-article/
Thanks, David, Will try it out. Btw, would you kindly help in solving this https://generatepress.com/forums/topic/how-to-show-updated-posts-with-plugin/#post-826692? Tom might have overlooked it. Google is also not picking my site's updated date.
You're welcome. Looks like Tom has provided a resolution for that topic.
The code shared also includes an author gravatar code. I just need the specific code to stop showing double last updated dates.
Brian's code in another support question worked. Will wait to see if Google will pick up the last updated date then revert.
This code is worth trying:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">%4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_the_modified_date() )
);
return sprintf( '<span class="posted-on">%s</span> ', // WPCS: XSS ok, sanitization ok.
$time_string
);
}, 10, 2 );
Hi Tom. As already pointed out, that code (I tried the others too) shows double dates as shown in the screenshot I attached in the other post. Only Brian's code (woorkup) seems to display properly.
That code only prints one date. If you tried it and had multiple dates, you likely have another filter conflicting with it.
I don't actually. I just had the CSS code that you recommend for showing updated date. Then adding the PHP code makes the date appear twice.
Hmm, that's strange. If you'd like to link me to the page I can take a closer look. Otherwise, the code Brian shared works as well, it's just not as future-proof.
Since it is a function, I don't know how to target only a certain demo page so I can share here.
I just gave the function a shot on my localhost: http://prntscr.com/mtav81
Only 1 <time> element is output in the source.
Did you remove Brian's code once you added the new function?
Hi Tom, removing these two lines of code stopped showing double updated dates.
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
I also used this css
.posted-on .updated{display: inline-block;}
.posted-on .updated + .entry-date{display:none;}
.posted-on .updated:before{
content:"Updated On: ";}
Will removing that function affect my site negatively? Here is the source code after removing the function.
<span class="posted-on"><a href="https://www.example.com/url/" title="12:43 pm" rel="bookmark"><time class="updated" datetime="2019-02-27T12:43:59+03:00" itemprop="dateModified">Feb 27, 2019</time><time class="entry-date published" datetime="2019-02-27T12:43:23+03:00" itemprop="datePublished">Feb 27, 2019</time></a></span><span class="cat-links"><a href="https://www.example.com/category/example/" rel="category tag">Example</a></span><span class="posted-on"></span>
As I said earlier, I tested the code two weeks ago before I even discovered Brians code yesterday and it was still showing double dates. I reinstalled the theme and it still showed double dates until I removed the code above.
You shouldn't need to make either of those adjustments. The code should work as is. I did make some changes yesterday, so make sure you re-add it from here: https://generatepress.com/forums/topic/hook-inside-content/#post-827528
I used your updated code. Still showed two updated dates. I removed the CSS code except this one .posted-on .updated + .entry-date{display:none;} and one date disappeared, but now I cannot add a prefix to the new date. Here is the source code after doing that:
<span class="posted-on"><a href="https://www.example.com/url/" title="12:55 pm" rel="bookmark"><time class="updated" datetime="2019-02-22T20:23:56+03:00" itemprop="dateModified">Feb 22, 2019</time><time class="entry-date published" datetime="2018-12-20T12:55:01+03:00" itemprop="datePublished">Dec 20, 2018</time></a></span><span class="cat-links"><a href="https://www.example.com/category/example/" rel="category tag">Example</a></span><span class="posted-on"><time class="entry-date updated-date" datetime="2019-02-22T20:23:56+03:00" itemprop="dateModified">Feb 22, 2019</time></span>
I was able to show the updated date on Google using Brian's code (checked recently crawled URLs). But I cannot get this one of yours to stop showing dates twice.
----------------------------------------------------------------------------------------
I did get the prefix to work with this css code though.
.posted-on .updated + .entry-date{display:none;}
.entry-date.updated-date:before{content:"Updated On: ";}
And this shows up in structured data testing tool http://prntscr.com/mtraf2 and http://prntscr.com/mtrcia