• I would like to make post excerpt text clickable, so that users can click on it and it links to the post.

    This is the code I have, but the challenge is it makes the whole container clickable. Instead, I need to change something and make only the excerpt text clickable:

    jQuery( document ).ready( function($) {
    var body = $( ‘body’ );
    if ( body.hasClass( ‘blog’ ) || body.hasClass( ‘archive’ ) ) {
    $( ‘.type-post’ ).on( ‘click’, function() {
    var link = $( this ).find( ‘.entry-title a’ ).attr( ‘href’ );
    if ( link ) {
    window.location = link;
    }
    } );
    }
    } );

    I need to change something but I’m not sure what. I would appreciate your help!

Viewing 13 replies - 1 through 13 (of 13 total)
  • Theme Author Tom

    (@edge22)

    Hi there,

    Try changing this:

    $( '.type-post' )

    To this:

    $( '.type-post .entry-summary' )

    Thread Starter awesomeblogger

    (@awesomeblogger)

    Hey @edge22,
    thanks for your comments.
    Actually I get some error, if I try to add this code using Hooks, the code appears on the site. Do I need to add this code some other way?

    Theme Author Tom

    (@edge22)

    If the code isn’t in a javascript file, it needs to be within <script> tags:

    <script>
        Your JS in here
    </script>
    Thread Starter awesomeblogger

    (@awesomeblogger)

    Hey @edge22,

    Okay I was able to solve that by adding script at the beginning and end.

    $( ‘.type-post .entry-summary’ ) marks the whole container and not the text itself. That makes the blank space after the text clickable as well.

    Is there a way to mark only the text?

    Thread Starter awesomeblogger

    (@awesomeblogger)

    Hey @edge22,

    I also have this code, but the problem is it removes the read more button, the way its stylelized, and also the text itself including the read more button appear as a link.

    Here’s the code:

    function clickable_excerpt( $excerpt ) {
    return ‘‘. preg_replace( ‘|</?a.*?>|i’, ”, $excerpt ) .’‘;
    }
    add_filter( ‘get_the_excerpt’, ‘clickable_excerpt’ );

    Theme Author Tom

    (@edge22)

    Do you have any examples of what you’re trying to accomplish, exactly? Being able to see it may give me another idea 🙂

    Thread Starter awesomeblogger

    (@awesomeblogger)

    Hey @edge22, thanks for your message, I do have an example.

    Link: https://greatist.com

    So as you can see if you scroll down on this site, the excerpt text, the short summary of the blog posts in the archive are both clickable and they lead to the post itself when the user clicks on them, just like the read more button.

    The problem with this code above I’ve encountered is, although it does make the excerpt clickable, it marks it and also the read more button as a link, changing its style (font and color, depending on the way they’re set up in the customizer).

    And the second problem is the code above marks the whole excerpt container, within which is the text, rather than just the text/summary itself.

    Does that make sense?

    Thank you.

    Theme Author Tom

    (@edge22)

    Can you link me to what you have so far? The javascript solution you mentioned should target the div that surrounds the text. This is what any other solution will do as well.

    Thread Starter awesomeblogger

    (@awesomeblogger)

    I do not have any other solution. I wasn’t able to figure it out. That’s why I am asking for help.

    bro can u shre insta or fb?

    Theme Author Tom

    (@edge22)

    Thread Starter awesomeblogger

    (@awesomeblogger)

    Hey @edge22,
    here’s the full code:

    <script>
    jQuery( document ).ready( function($) {
    var body = $( ‘body’ );
    if ( body.hasClass( ‘blog’ ) || body.hasClass( ‘archive’ ) ) {
    $( ‘.type-post .entry-summary’ ).on( ‘click’, function() {
    var link = $( this ).find( ‘.entry-title a’ ).attr( ‘href’ );
    if ( link ) {
    window.location = link;
    }
    } );
    }
    } );
    </script>

    I know that this code targets the whole div container. That makes the blank space with no text clickable. I do not want that, but only the text clickable.
    Please read my message above in full.

    Theme Author Tom

    (@edge22)

    Hi there,

    That code *should* work without making any blank spaces. Is there any way you can link me to the page where it’s being used so I can see the issue? It’s very difficult to know why it’s not working without actually seeing it.

    Thanks!

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

The topic ‘How to make excerpt text clickable?’ is closed to new replies.