• Resolved glenlipka

    (@glenlipka)


    Wordpress 6.01
    Theme: Twenty Twenty Two
    Goal: To make the post excerpt clickable
    Site: https://commadot.com
    Related Plugin: Twentig

    Summary: I just want to make the whole post excerpt clickable. I tried installing TC Custom Scripts to add this:

    $(document).ready(function($) {
    $(“.wp-block-post”).on(“click”, function() {
    location.href = $(this).find(“h2 a”).attr(“href”);
    });
    });

    But I got this error in incognito:
    Uncaught ReferenceError: jQuery is not defined

    Am I doing something wrong?
    Should I enqeue jQuery in the functions of the theme? Is there a way to do this through the editor?

    Thanks for the help.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Kathryn Presner

    (@zoonini)

    Hi @glenlipka – may I ask where you got that code? If it’s older, it may not be compatible with the version of jQuery that ships with core.

    Rather than JavaScript, I borrowed a PHP function from “solution 1” found here, which worked perfectly on my test site. All I had to do to get it working was change the class name to match Twenty Twenty-Two, i.e. wp-block-post-excerpt__excerpt

    I popped the code in a plugin file called clickable-excerpts.php and it worked perfectly:

    <?php
       /*
       Plugin Name: Wrap excerpt in a link
       Description: Adds a link around post excerpts
       Version: 1
       URL: https://wordpress.org/support/topic/make-post-excerpt-clickable/?view=all
       */
    
    function clickable_excerpt( $excerpt ) {
    	return '<a href="'. get_the_permalink() .'" class="wp-block-post-excerpt__excerpt">'. $excerpt .'</a>';
    }
    add_filter( 'get_the_excerpt', 'clickable_excerpt' );

    If you want to remove the default formatting (i.e. colour & underline, etc.) you can add some custom CSS as well.

    Let me know how it goes!

    Thread Starter glenlipka

    (@glenlipka)

    I ended up going a different route. I added in a cover block wrapping the H1 and Post Excerpt using the Stretch Link property. It would be smart for the theme editors to make this property available on the group block. Or just give a property to the post excerpt block called “Link to post”.

    Details here: https://commadot.com/wordpress-twenty-twenty-two-part-2/

    Moderator Kathryn Presner

    (@zoonini)

    Thanks for sharing your workaround! I went ahead and put it an enhancement request for your idea:

    https://github.com/WordPress/gutenberg/issues/42935

    Feel free to share other enhancements or report bugs there in the future. You have some great insights and you’re very welcome to share them directly with the folks working on the editor!

    I’ll mark this as resolved for now but feel free to post again if you need help with something else.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Make Post Excerpt Clickable’ is closed to new replies.