• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
WP Akuten

WP Akuten

Take it easy - it will be fine

  • Tips
  • Development WordPress
  • Troubleshooting
Boka ett möte
Ring
Sök
  • Tips
  • Development WordPress
  • Troubleshooting

Show next or previous post

WP Akuten > Development WordPress > Show next or previous post

Post navigation

skrivet den 31 October, 2023, uppdaterat den 20 April, 2024 av Karin H Olsson

Many themes include a navigation underneath a post where you can move on to the next or previous post. The disadvantage is often that the links lead to recent posts regardless of the category they are in.

post navigation

If you have created a good structure among your posts, you are more likely to lead the visitor to the next or previous post in the same category. .

To do this, you need to add some code.

Link to next post

You can put the code in your functions.php in your theme if you have a child theme. Otherwise, you can create an extension that contains code specific to your website, a site plugin.

add_filter( 'next_post_link',     'my_post_link', 10, 5 );
add_filter( 'previous_post_link', 'my_post_link', 10, 5 );

function my_post_link( $output, $format, $link, $post, $adjacent )
{
	$previous = 'previous' === $adjacent;

	if ( ! ( $previous && is_attachment() ) ) {
		$post = get_adjacent_post( true, '', $previous, 'category' );
	}

	if ( ! $post ) {
		$output = '';
	} else {
		$title = $post->post_title;

		if ( empty( $post->post_title ) ) {
			$title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
		}

		$title = apply_filters( 'the_title', $title, $post->ID );

		$date = mysql2date( get_option( 'date_format' ), $post->post_date );
		$rel  = $previous ? 'prev' : 'next';

		$string = '<a href="' . get_permalink( $post ) . '" rel="' . $rel . '">';
		$inlink = str_replace( '%title', $title, $link );
		$inlink = str_replace( '%date', $date, $inlink );
		$inlink = $string . $inlink . '</a>';

		$output = str_replace( '%link', $inlink, $format );
	}

	return $output;
}

Karin H Olsson

Jag arbetar med teknisk utveckling i WordPress – framförallt när standardlösningar inte räcker.

Jag har en bakgrund som konstruktör inom realtidssystem (de system som finns i bilar, flygplan m.m.) och har följt utvecklingen hela vägen till dagens moderna webb.

Med utbildning inom både systemvetenskap och psykologi arbetar jag i gränslandet mellan teknik och användning – där systemen inte bara ska fungera, utan också fungera för människor.

→ Läs mer KoBoToLo – Om KoBoToLo en webbyrå i Karlskoga

Previous Post:Keep track of changes
Kategori: Development WordPress

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Sidebar

Today’s words of wisdom.

Economics: the murky science.Thomas Carlyle

Popular

  • Link to phone number website
  • Scroll forever – infinite scroll
  • My images are cut (cropped) – about image…
  • Searching and editing in WordPress
  • Line breaks on the web – absolutely not!

Du hittar mig i Värmland bland skogar, vargar och sjöar


Mail: info@kobotolo.se

Vill du istället att vi kontaktar dig så skicka ett mail så ringer vi upp. Du kan också fylla i vårt kontaktformulär:


Be mig kontakta dig


Maila mig



Tel: 070 – 49 34 191

Onsdagar 9.00 till 10.00 är vår stående hjälptimme. Då ger vi tips och råd per telefon till kunder och icke-kunder. Ingen fråga är fel!

Övrig tid kan vi tyvärr inte ge support per telefon!

Copyright © 2026 · WordPress Akuten · All Rights Reserved

  • English
  • Svenska (Swedish)