[Support request] "read-more" link inline

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support "read-more" link inline

This topic contains 10 replies, has 3 voices, and was last updated by  Tom 3 months, 1 week ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #6847

    Jonathan
    Participant

    I’m not able to create my own “read-more” links in wp-show-posts. The following works for the blog page but does nothing for the “Show Posts” output.

    `
    add_filter( ‘the_content_more_link’, ‘generate_custom_content_more’, 15 );
    function generate_custom_content_more( $more ) {
    return ‘ <a class=”read-more content-read-more” href=”‘. get_permalink( get_the_ID() ) . ‘”>’ . __(‘[continued…]’, ‘generate’) . ‘</a>’;
    }
    `

    Here’s where it works on a blog page: https://www.staging2.classical-scene.com/topics/news/
    Here the 2nd column is output for WP Show Posts but there is no “read more” link:
    https://www.staging2.classical-scene.com/

    I can put the “read more text” in the Output section of “WP Show Posts” to get a link but this shows up on the next line. I tried using float but that didn’t work. I think float would work if the content paragraphs were wrapped in a div.

    Any suggestions?

    #6850

    Jonathan
    Participant

    I found the problem. It’s a bug in the plugin. On line 473 of wp_show_posts.php it has the following:

    <?php the_content(false, false); ?>

    The first argument is supposed to be null, not false (apparently these are not equivalent in php). If I change it to null (or just call the_content with no arguments) then the filter works.

    https://developer.wordpress.org/reference/functions/the_content/
    https://stackoverflow.com/questions/137487/null-vs-false-vs-0-in-php

    #6852

    Tom
    Keymaster

    It’s not so much a bug as a way to create consistency. Having that value not set to false would mean the WPSP content read more link would be open to filters set by themes and other plugins.

    What we can do is filter the content itself.

    The downside of this is we need to target which pages we want to filter, as the WP content filter doesn’t have a way to know whether it’s in WPSP or not.

    For example, if our list is on a page with the ID of 10, we could do this:

    add_filter( 'the_content', function( $content ) {
        if ( is_page( 10 ) ) {
            return $content . '<a href="' . get_permalink() . '">Read more</a>'; 
        }
      
        return $content;
    } );
    #6854

    Jonathan
    Participant

    Adding something using the “the_content” filter is very tricky (maybe if you’re really good at writing regular expressions you can do it). I can just add the “read-more” link to the end of the content. But then I have two paragraphs at the end, the last paragraph of the content following by a paragraph for the link and this ends up having the “read-more” link on a separate line. I could do that without using the filter by just using the action hook to put output the link after the content. The whole point of this exercise is to get the “read-more” link on the same line.

    Maybe you could add an option so that it doesn’t pass false as the first argument?

    Why do you try to stop other plugins or themes from using filters on the more link? You don’t do that in the GeneratePress theme code!

    • This reply was modified 3 months, 2 weeks ago by  Jonathan.
    • This reply was modified 3 months, 2 weeks ago by  Jonathan.
    #6859

    Clayton
    Participant

    Amazing! Is there some more detailed hook documentation anywhere?

    Thanks,
    Clayton

    #6861

    Jonathan
    Participant

    If you really want to prevent filtering, then the first argument to the_content should be an empty string rather than false. The only valid thing to pass in is a string or a null. Anything else is not guaranteed to have the behavior you want. I tend to believe that this was not your intention since I see in the past you have suggested using the the_content_more_link filter.

    https://wpshowposts.com/support/topic/displaying-read-more-button-with-read-more-tag/

    #6863

    Tom
    Keymaster

    Any empty string in PHP is equal to null, so doing that wouldn’t have the effect we’re going for. It would print the default WP more link: https://developer.wordpress.org/reference/functions/get_the_content/

    What we can do is pass a filter so you can filter in your own content more link. I can have this added to the next version.

    Find this line: https://github.com/tomusborne/wp-show-posts/blob/1.1.3/wp-show-posts.php#L473

    And replace it with this:

    <?php
    $content_more_link = apply_filters( 'wpsp_content_more_link', false, $settings );
    
    the_content( $content_more_link ); 
    ?>

    Then you can do this:

    add_filter( 'wpsp_content_more_link', function() {
        return 'Read more';
    } );
    #6866

    Jonathan
    Participant

    Hi Tom,

    The empty string (“”) is not the same as null but false does get treated as an empty string so that’s why false worked as the first argument to “the_content”.

    After adding the code changes you suggested it still didn’t work. To get it to work, I added the following code (in addition to your change to wp-show-posts.php):

    add_filter( 'the_content_more_link', function() {
        return 'Read more';
    } );
    add_filter( 'wpsp_content_more_link, function() {
        return null;
    } );

    The reason your suggested addition didn’t work was probably related to the following:
    plugins/gp-premium/blog/functions/generate-blog.php: add_filter( ‘the_content_more_link’, ‘generate_blog_content_more’, 15 );

    Thanks,
    Jon

    #6867

    Tom
    Keymaster

    Ah yea, that makes sense. That’s also the reason we don’t set it to null by default, as themes (including GeneratePress) typically have their own filter.

    The above solution seems like the best way forward, a WPSP filter to set it to null if needed, and the the_content_more_link to take it from there.

    Thanks!

    #6870

    Jonathan
    Participant

    I also need to make sure the “Read More Text” value is empty in the WP Show Posts settings. Otherwise, I see the link twice in the WP Show Posts output, one link for the one specified in the WP Show Posts settings panel and once for the text specified in my “the_content_more_link” filter.

    This problem does not exist in the GeneratePress Pro theme. There if I override the “read-more” link using the “the_content_more_link” filter and one is specified in the GeneratePress Pro settings panel then I don’t see the link appear twice. Only the one specified in the filter shows up. The one in the Generate Press Pro settings panel is ignored when I override using the “the_content_more_link” filter.

    #6904

    Tom
    Keymaster

    The read more button in WPSP is just an HTML hooked into element – it doesn’t use any filters to output.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.