Site logo

[Resolved] Custom link on red more button

Home Forums Support [Resolved] Custom link on red more button

Home Forums Support Custom link on red more button

Viewing 15 posts - 1 through 15 (of 24 total)
  • Author
    Posts
  • #2454508
    Carl

    If I create a new post as type “link” it would be nice, that I get to the link url via pressing the read more button or clicking on the image.
    How can I do that?

    Thank you!

    #2454592
    David
    Staff
    Customer Support

    Hi there,

    can i see the archive page with the Link format posts on it ?

    #2459025
    Carl

    https://www.denkanstösse-limburgerhof.info/1670829360831/

    The first two items are link posts, the third item is a normal post.

    I have installed this snipped:
    https://docs.generatepress.com/article/activating-read-custom-excerpt/

    So now I get on the link by klicking on the post header if I have inserted an URL in the excerpt field. But then is no read more button displayed.

    In the third post, there is a read more button displayed.

    It doesn‘t looks nice if sometimes we have and sometimes we havent a read more button.
    It would be nice, if everytime a read more button would be displayed and I can get to the url by clicking on the read more button and on the image.
    (Now I have a css display none on the read more button so that it is more uniformly).
    Thank you!

    #2459453
    David
    Staff
    Customer Support

    For the Featured Image you can try this PHP Snippet:

    
    add_filter( 'generate_featured_image_output', function( $output ) {
        $content = get_the_content();
        $has_url = get_url_in_content( $content );
        $url = ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
        return sprintf(
            '<div class="post-image">
                %3$s
                <a href="%1$s">
                    %2$s
                </a>
            </div>',
            esc_url( $url ),
            get_the_post_thumbnail(
                get_the_ID(),
                apply_filters( 'generate_page_header_default_size', 'full' ),
                $attrs
            ),
            apply_filters( 'generate_inside_featured_image_output', '' )
        );
    });

    Are you still wanting the Read More ?

    #2459622
    Carl

    Hi David,

    thank you very much!!!

    My customer still wanting the Read More 😉

    Kind regards
    Wolfgang

    #2459815
    Fernando
    Customer Support

    Hi Carl,

    You can create a Block Element with a Dynamic GB Button Block for the Read More button. Just set the link source to Single Post. Example: https://share.getcloudapp.com/p9uLPWll

    Then, set the Hook to after_entry_content for instance.

    Set it to Display on your Blog page.

    #2464907
    Carl

    Hi Fernando,

    thank you! But now the link goes to the “single post”, but it should go to the link inserted into the excerpt field (or a custom field, …), if there is inserted a link. If not, it should go to the single post, like the link on the image and on the title.

    Kind Regards
    Wolfgang

    #2465249
    David
    Staff
    Customer Support

    You can change the Link Source to Post Meta and in the field it provides add your custom field name.

    #2466640
    Carl

    OK, thank you David! It do not display the field in the pulldown-menu, but I have typed in the name and now it runs.

    I would now like that this element is only used when displaying posts with the type “link” or rather only when a link is entered in the custom field.
    How would you do that?

    My apologies for asking such beginner questions, but I think once I get the hang of it, I might be able to better utilize Generate Press’s potential.

    #2466768
    David
    Staff
    Customer Support

    Is the Content Source also set to display the Custom Field ( Post Meta ) ?
    If it is then when there is no custom field value to return it will remove the block.

    #2466902
    Carl

    Yes, that’s what I thought too, but the normal post still doesn’t display a link on the “Read more” button. The button only works if I enter the URL of the post in the link field.

    #2466934
    David
    Staff
    Customer Support

    Ok, i had to have a re-read of the topic.
    Just to be clear what is the preference for the Read More:
    a. Is it to use a the first URL in the content like we did for the Featured Image?
    b. is it to use a dynamic value from a Custom Field ?

    Let me know as both options will require some PHP and ill write that up.

    #2466960
    Carl

    The best solution would be: If the url for the featured image, headline and read more would be taken from the “link” field created via ACF. And if no URL is entered, the contribution URL should be output for all 3 places.
    It would be ideal if I could then adjust the settings for the number of words in the excerpt.

    Thank you a lot!

    #2467928
    David
    Staff
    Customer Support

    Ok, so try this:

    // Replace featured image link with custom field value
    add_filter( 'generate_featured_image_output', function( $output ) {
        $has_url = get_post_meta(get_the_ID(), 'your_custom_field', true);
        $url = ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
        return sprintf(
            '<div class="post-image">
            %3$s
            <a href="%1$s">
            %2$s
            </a>
            </div>',
            esc_url( $url ),
            get_the_post_thumbnail(
            get_the_ID(),
            apply_filters( 'generate_page_header_default_size', 'full' ),
                $attrs
            ),
            apply_filters( 'generate_inside_featured_image_output', '' )
        );
    });
    // Replace exceprt more link with custom field value
    add_filter('generate_excerpt_more_output', function(){
        $has_url = get_post_meta(get_the_ID(), 'your_custom_field', true);
        $url = ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
        return sprintf(
            ' ... <a title="%1$s" class="read-more" href="%2$s" aria-label="%4$s">%3$s</a>',
            the_title_attribute( 'echo=0' ),
            esc_url( $url ),
            __( 'Read more', 'generatepress' ),
            sprintf(
                /* translators: Aria-label describing the read more button */
                _x( 'More on %s', 'more on post title', 'generatepress' ),
                the_title_attribute( 'echo=0' )
            )
        );
    });
    // Replace title link with custom field value
    add_filter('generate_get_the_title_parameters', function($params){
        if ( ! is_singular() && ! 'link' === get_post_format() ) {
            $has_url = get_post_meta(get_the_ID(), 'your_custom_field', true);
            $url = ( $has_url ) ? $has_url : apply_filters( 'the_permalink', get_permalink() );
            $params = array(
                'before' => sprintf(
                    '<h2 class="entry-title"%2$s><a href="%1$s" rel="bookmark">',
                    esc_url( $url ),
                    'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
                ),
                'after' => '</a></h2>',
            ); 
        }
        return $params;
    });

    All 3 of the filter get the field from post meta on this line:
    $has_url = get_post_meta(get_the_ID(), 'your_custom_field', true);

    You need to update the custom field value there or you can replace the get_post_meta with the ACF equivalent get_field also.

    #2467964
    Carl

    Thank you so much David!!! I´ll try it tomorrow and give you feedback.

    Kind regards
    Wolfgang

Viewing 15 posts - 1 through 15 (of 24 total)
  • You must be logged in to reply to this topic.