• Resolved janbrokes

    (@janbrokes)


    HEllo this is part of code of previous link

    $previous_post_link = get_previous_post_link( '%link', '<span class="meta-nav">' . esc_html( _x( '&larr;', 'Previous post link', 'et_builder' ) ) . '</span> ' . $previous_link_text, $in_same_term, '', $current_taxonomy );

    I need to add at the end of url #anchor

    so that link of previous post looks like https://www.domain.com/prev-post1/#anchor

    Thansk in advance

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    This should do it (added to functions.php):

    add_filter('previous_post_link', 'jb_add_to_url');
    function jb_add_to_url( $link ) {
       return $link . '#anchor';
    }

    If you want the same for get_next_post_link(), just add this line:
    add_filter('next_post_link', 'jb_add_to_url');

    Thread Starter janbrokes

    (@janbrokes)

    Hello thanks for you reply, but I did not find it in functions.PHP. Maybe becasue I am using DIVI template by Elegant themes.

    I thing this is all code for theese links. Could you please find how to add ‘#anchor’

    $previous_link_text = '' !== $prev_text ? esc_html( $prev_text ) : '%title';
    		$next_link_text = '' !== $next_text ? esc_html( $next_text ) : '%title';
    		$in_same_term = 'on' === $in_same_term && ! is_page() ? true : false;
    
    		if ( '' === $taxonomy_name ) {
    			$current_taxonomy = is_singular( 'project' ) ? 'project_category' : 'category';
    		} else {
    			$current_taxonomy = sanitize_text_field( $taxonomy_name );
    		}
    
    		ob_start();
    		$next_post_link = get_previous_post_link( '%link', '<span class="meta-nav">' . esc_html( _x( '&larr;', 'Previous post link', 'et_builder' ) ) . '</span> ' . $previous_link_text, $in_same_term, '', $current_taxonomy );
    		$previous_post_link     = get_next_post_link( '%link', $next_link_text . ' <span class="meta-nav">' . esc_html( _x( '&rarr;', 'Next post link', 'et_builder' ) ) . '</span>', $in_same_term, '', $current_taxonomy );
    
    		if ( 'on' !== $hide_prev && '' !== $previous_post_link ) { ?>
    			<span class="nav-previous">
    				<?php echo $previous_post_link; ?>
    			</span>
    		<?php }
    
    		if ( 'on' !== $hide_next && '' !== $next_post_link ) { ?>
    			<span class="nav-next">
    				<?php echo $next_post_link; ?>
    			</span>
    		<?php }
    Moderator bcworkz

    (@bcworkz)

    Sorry, I wasn’t explicit enough. There is nothing to find in functions.php, you need to add my suggested code to the bottom of this file. Believe it or not, doing so will affect the output of the code you posted. It’s hard to explain how filters work, but they do.

    There is one caveat about adding code to your theme’s functions.php file, your addition will be lost when the theme is updated. The usual way to workaround this is to create a child theme. Except I believe your theme might be one of those themes that are installed as a child theme, taking this option away from you. If the theme’s style.css header has a “template” line, then it’s a child theme. If that’s the case, this added code can be made into a very basic plugin in order to protect your modification form theme updates. A simple custom plugin only needs to be one file in the plugins folder with a specific header format and the code I suggested.

    I suggest you first just put the code in the current functions.php to be sure it does what you want. You can move it to a custom plugin sometime later before your theme is updated. This same plugin is a good repository for other little hacks you may come up with in the future.

    Thread Starter janbrokes

    (@janbrokes)

    I see, many thanks for reply. See what happened https://www.livit.cz/obyvaci-pokoje/rustikalni-obyvaci-pokoj/
    or here
    https://snag.gy/xEMYrK.jpg

    Thanks for help

    Moderator bcworkz

    (@bcworkz)

    Ah crap! Sorry about that, I should have looked into what I’m adding ‘#anchor’ to instead of assuming. Try this version:

    add_filter('previous_post_link', 'jb_add_to_url');
    function jb_add_to_url( $link ) {
       $new = preg_replace('/href="([^"]+)"/', 'href="$1#anchor"', $link );
       return $new;
    }

    Thread Starter janbrokes

    (@janbrokes)

    thank you

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘NExt and previous post links with #anchor’ is closed to new replies.