{"id":10182,"date":"2023-10-31T16:24:28","date_gmt":"2023-10-31T15:24:28","guid":{"rendered":"https:\/\/wp-akuten.se\/okategoriserad\/show-next-or-previous-post\/"},"modified":"2024-04-20T09:07:55","modified_gmt":"2024-04-20T07:07:55","slug":"show-next-or-previous-post","status":"publish","type":"post","link":"https:\/\/wp-akuten.se\/en\/development-wordpress\/show-next-or-previous-post\/","title":{"rendered":"Show next or previous post"},"content":{"rendered":"\n<p>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.<\/p>\n<style>.kb-image8948_0915a3-31 .kb-image-has-overlay:after{opacity:0.3;}.kb-image8948_0915a3-31 img.kb-img, .kb-image8948_0915a3-31 .kb-img img{box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<figure class=\"wp-block-kadence-image kb-image8948_0915a3-31 size-large\"><img decoding=\"async\" width=\"1024\" height=\"241\" src=\"https:\/\/wp-akuten.se\/wp-content\/uploads\/2023\/10\/postnavigering-1024x241.png\" alt=\"post navigation \" class=\"kb-img wp-image-8949\" srcset=\"https:\/\/wp-akuten.se\/wp-content\/uploads\/2023\/10\/postnavigering-300x71.png 300w, https:\/\/wp-akuten.se\/wp-content\/uploads\/2023\/10\/postnavigering-768x181.png 768w, https:\/\/wp-akuten.se\/wp-content\/uploads\/2023\/10\/postnavigering-1024x241.png 1024w, https:\/\/wp-akuten.se\/wp-content\/uploads\/2023\/10\/postnavigering.png 1324w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n<p>If you have created a <a href=\"https:\/\/wp-akuten.se\/en\/tips-wp\/creating-structure-in-wordpress-tags-and-categories\/\" class=\"ek-link\">good structure<\/a> among your posts, you are more likely to lead the visitor to the next or previous post in the same category. .<\/p>\n\n<p>To do this, you need to add some code.<\/p>\n\n<h2 class=\"wp-block-heading\">Link to next post<\/h2>\n\n<p>You can put the code in your functions.php in your theme if you have a <a href=\"https:\/\/wp-akuten.se\/tips\/vad-ar-ett-barntema-och-varfor-du-ska-ha-ett\/\" class=\"ek-link\">child theme<\/a>. Otherwise, you can create an extension that contains code specific to your website, a site plugin.<\/p>\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">add_filter( 'next_post_link',     'my_post_link', 10, 5 );\nadd_filter( 'previous_post_link', 'my_post_link', 10, 5 );\n\nfunction my_post_link( $output, $format, $link, $post, $adjacent )\n{\n\t$previous = 'previous' === $adjacent;\n\n\tif ( ! ( $previous &amp;&amp; is_attachment() ) ) {\n\t\t$post = get_adjacent_post( true, '', $previous, 'category' );\n\t}\n\n\tif ( ! $post ) {\n\t\t$output = '';\n\t} else {\n\t\t$title = $post-&gt;post_title;\n\n\t\tif ( empty( $post-&gt;post_title ) ) {\n\t\t\t$title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );\n\t\t}\n\n\t\t$title = apply_filters( 'the_title', $title, $post-&gt;ID );\n\n\t\t$date = mysql2date( get_option( 'date_format' ), $post-&gt;post_date );\n\t\t$rel  = $previous ? 'prev' : 'next';\n\n\t\t$string = '&lt;a href=\"' . get_permalink( $post ) . '\" rel=\"' . $rel . '\"&gt;';\n\t\t$inlink = str_replace( '%title', $title, $link );\n\t\t$inlink = str_replace( '%date', $date, $inlink );\n\t\t$inlink = $string . $inlink . '&lt;\/a&gt;';\n\n\t\t$output = str_replace( '%link', $inlink, $format );\n\t}\n\n\treturn $output;\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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. If you have created a good structure among your posts, you are more likely to lead the visitor &hellip;<\/p>\n","protected":false},"author":2,"featured_media":10136,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[119],"tags":[],"class_list":{"2":"type-post","7":"category-development-wordpress","8":"entry"},"taxonomy_info":{"category":[{"value":119,"label":"Development WordPress"}]},"featured_image_src_large":["https:\/\/wp-akuten.se\/wp-content\/uploads\/2023\/10\/postnavigering-1024x241.png",800,188,true],"author_info":{"display_name":"Karin H Olsson","author_link":"https:\/\/wp-akuten.se\/en\/author\/adaro3r\/"},"comment_info":0,"category_info":[{"term_id":119,"name":"Development WordPress","slug":"development-wordpress","term_group":0,"term_taxonomy_id":119,"taxonomy":"category","description":"","parent":0,"count":16,"filter":"raw","meta":[],"cat_ID":119,"category_count":16,"category_description":"","cat_name":"Development WordPress","category_nicename":"development-wordpress","category_parent":0}],"tag_info":false,"acf":[],"_links":{"self":[{"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/posts\/10182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/comments?post=10182"}],"version-history":[{"count":4,"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/posts\/10182\/revisions"}],"predecessor-version":[{"id":10186,"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/posts\/10182\/revisions\/10186"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/media\/10136"}],"wp:attachment":[{"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/media?parent=10182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/categories?post=10182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp-akuten.se\/en\/wp-json\/wp\/v2\/tags?post=10182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}