Loop – how to get previous/next post for first/last post?

You can loop the slides quite simply: $prev_index = ( $loop->current_post == 0 ) ? count( $loop->posts ) – 1 : $loop->current_post – 1; $next_index = ( $loop->current_post == count( $loop->posts ) – 1 ) ? 0 : $loop->current_post + 1; $prev_post = $loop->posts[ $prev_index ]; $next_post = $loop->posts[ $next_index ]; echo $next_post->post_title; echo $next_post->post_title; … Read more

Posts archive index pagination in a static page custom query

When you perform a custom query such as this one, you have to do a bit of a “hack” to get pagination to work properly. Change this: <?php query_posts( “category_name=news&orderby=date&order=ASC&posts_per_page=2” ); ?> …to a WP_Query call: <?php $news_query = new WP_Query( “category_name=news&orderby=date&order=ASC&posts_per_page=2” ); ?> And then you need to *move your custom query object into … Read more

How to make Next and Previous attached image navigation on the attachment page? [duplicate]

Use previous_image_link and next_image_link in a image.php or attachment.php file. <nav id=”image-navigation” class=”navigation image-navigation”> <div class=”nav-links”> <?php previous_image_link( false, ‘<div class=”previous-image”>’ . __( ‘Previous Image’, ‘$text_domain’ ) . ‘</div>’ ); ?> <?php next_image_link( false, ‘<div class=”next-image”>’ . __( ‘Next Image’, ‘$text_domain’ ) . ‘</div>’ ); ?> </div><!– .nav-links –> </nav><!– #image-navigation –> Source: Twenty Fourteen

Get next/previous cousin page

I solved this by getting all pages, assigning them by level, and then getting the next page of the same level. If you also want the previous page then you should probably cache the functionality that groups the pages by level. function wpse16875_next_page_same_level_link() { $next_page_same_level = wpse16875_next_page_same_level(); if ( $next_page_same_level ) { return ‘<a href=”‘ … Read more

Get previously visited page ID

Break this down into two parts: First, we create a variable that stores that last-visited page URL, like this: $prev_url = isset($_SERVER[‘HTTP_REFERER’]) ? $_SERVER[‘HTTP_REFERER’] : ”; Then, you could either use substr and strpos to trim down everything between ?= and the / after the ID number. like this: $prev_url=”http://www.yoursite.com/?p=123″; $id_block = substr($prev_url, strpos($prev_url, “?p=”)+1); … Read more