Link a dynamic headline to the date archives

Dynamic data in GeneratePress are quite powerful. And when some dynamic options are not available, there are always powerful filters to complement functionality. Take, for instance, the ability to link dynamic elements to the post archives. With post archives, I am referring to the[year]/[month]/[day]/ URL where all articles from a certain day are displayed. For example a URL of would display an archive with all posts published on the 30th of January 2024. Of course, your WordPress installation might have a different date format (eg: US) but you get the point.

Both headline dynamic options (accessed from the block toolbar) and dynamic data set in the section on the right of the block editor do not have dynamic link sources that could link to the date archives.

Headline dynamic link sources.
Headline dynamic link sources.

Thankfully, we can use the generateblocks_dynamic_content_output to modify the output of our headline. Just select the Post date from the CONTENT SOURCE dropdown, set the dynamic link from the LINK SOURCE to anything you like (this is needed for the filter to work), assign a class to your headline (eg: date-archives), and use the following PHP inside your child theme’s functions.php file:

add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){
    if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'date-archives' ) !== false ) {
         // Get the current post's date.
        $post_date = get_the_date('Y/m/d');

        // Generate the archive link for the post date
        $archive_link = get_day_link( substr($post_date, 0, 4), substr($post_date, 5, 2), substr($post_date, 8, 2) );

        // Return the content wrapped in an  archives archives archives archives archives archivesanchor tag linking to the archive
        return '<a href="' . esc_url($archive_link) . '">' . $content . '</a>';
    return $content;
}, 10, 3);
Post date as headline content source
Post date as headline content source.

Now, depended where you use this headline, it will link to the current post’s date archive!