1. Home
  2. Docs
  3. Developer Guide
  4. Miscellaneous and Snippet...
  5. Add Next / Previous Links To Property Details Page

Add Next / Previous Links To Property Details Page

A feature we sometimes see on property sites is to enable users to cycle through their results after clicking into one of the properties. This removes the need to keep returning back to the results page to only then go into the next property within the results.

Although we don’t offer this out of the box at the moment, it is possible to achieve by following the below steps.

Step 1: Enable sessions

Once a user runs a search we’ll be storing their results in a session. As a result, the first thing we need to do is enable PHP sessions on our site. You can do this by adding the following to your theme’s functions.php file:

add_action('init', 'my_start_session', 1);
function my_start_session() 
{
    if (!session_id()) 
    {
        session_start();
    }
}
 
add_action('wp_logout', 'my_end_session');
add_action('wp_login', 'my_end_session');
function my_end_session() 
{
    session_destroy();
}

Step 2: Store results in a session

Now we have sessions enabled we need to add the results to it once a search is ran. Again, we can add the following to the theme’s functions.php file to achieve this:

add_action('wp', 'store_results_in_session', 1);
function store_results_in_session()
{
    if ( is_post_type_archive('property') )
    {
        global $wpdb; 
 
        $_SESSION['results_array'] = array();
 
        $explode_query = explode('LIMIT', $GLOBALS['wp_query']->request);
 
        $query = $explode_query[0];
 
        $results = $wpdb->get_results($query);
        if ( !empty($results) )
        {
            foreach ( $results as $result ) 
            {
                $_SESSION['results_array'][] = $result->ID;
            }
        }
    }
    session_write_close();
}

Step 3: Display next/previous links

Now all that’s left to do is display the ‘Next Property’ and ‘Previous Property’ links on the property details page.

For the ‘Previous Property’ link you can do something like so where you want the link to appear:

if (isset($_SESSION['results_array']))
{
    $prev_property_id = '';
    foreach ($_SESSION['results_array'] as $property_id)
    {
        if ($property_id == $post->ID) { break; }
        $prev_property_id = $property_id;
    }
    if ($prev_property_id != '')
    {
        echo '<a href="' . get_permalink($prev_property_id) . '">Previous Property</a>';
    }
}

… and similarly something like so for the ‘Next Property’ link:

if (isset($_SESSION['results_array']))
{
    $next_property_id = '';
    $do_next = false;
    foreach ($_SESSION['results_array'] as $property_id)
    {
        if ($do_next)
        {
            $next_property_id = $property_id;
            break;
        }
        if ($property_id == $post->ID)
        {
            $do_next = true;
        }
    }
    if ($next_property_id != '')
    {
        echo '<a href="' . get_permalink($next_property_id) . '">Next Property</a>';
    }
}
Was this article helpful to you? No Yes

How can we help?