Skip Navigation

[Resolved] Adding next button to post type single

This support ticket is created 7 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Ole Andreas Vekve 7 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#419320

I am trying to:
Add next / prev button on each side of the featured image for all singles within a custom post type.

I have programmed this earlier using the following:

<?php
                                $prev_post = get_previous_post();
                                if($prev_post) {
                                   $prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
                                   echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" class="btn btn-darkgrey grow littluft leftfloat floatnonemobil">&laquo;<strong>'. $prev_title . '</strong></a>' . "\n";
                                }
                            ?>

<?php
                            $next_post = get_next_post();
                                if($next_post) {
                                   $next_title = strip_tags(str_replace('"', '', $next_post->post_title));
                                   echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" class="btn btn-darkgrey grow littluft rightfloat floatnonemobil">'. $next_title . '&raquo;</a>' . "\n";
                                }
                            ?>

Instead, I got:
hidden link

But PHP is not supported in the visual editor / html editor within Layouts. So not sure how I should do this.

Looking farward to hear from you,

Best regards

#419406

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ole,

Thank you for contacting our support forum.

In order to do this you will need to add it directly to the single.php file that displays the posts.

If you do this with php then it needs to be added to the file template itself.

Are you using a content template to display the information ?

Please let me know.
Thanks
Shane

#419519

Hello,

Thanks,

I am using Layouts, and not Content Template. But maybe I should add this in the Content Template?

As I have read, the loops should be removed, and the layouts should be added in the single.php. Like this:

<?php
/*
Single Post Template: Prosjekt
*/

register_taxonomy_for_object_type('post_tag', 'page');
get_header(); 

?>

<article>
<div class="clearfix">
<div class="holder grey blacktext">
    <div class="container">
        <div class="row">
        	<div class="col-md-12">
            	<?php the_ddlayout( 'default-layout' ); ?>
            </div>
        </div>
    </div>
</div>
</div>
</article>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

This means I am not able to add the buttons, as they are apart of the loop.

How can I solve this?

#419710

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ole,

I understand a bit more clearly what you are trying to achieve and the best way to do this is by using a shortcode.

This shortcode will contain the code for the Next and Previous buttons. Example

// Add Shortcode
function pagination() {

	 previous_post_link();
	 next_post_link();
	 
	

}
add_shortcode( 'pagination', 'pagination' );

Add the above to your functions.php file and then you can call the shortcode by doing this [pagination]

This is the most basic example that I can give in order to assist you. For more information on the post pagination you can just have a look at the link below.
https://codex.wordpress.org/Next_and_Previous_Links

Please let me know if this helps.
Thanks,
Shane

#419852

Thank you,

That's close. How could I make one button for the prev and one for the next? They are going to be placed on each side of the featured image.

Also want to include a .png that is an arrow pointing left / right. And the text with the title "Next" and "Prev".

Looking forward to hear from you again,

#420036

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Ole,

What you can do is to just separate the functions into different shortcodes like this .

// Add Shortcode
function prev_link() {
 
     previous_post_link();

      
     
 
}
add_shortcode( 'prev_link', 'prev_link' );


// Add Shortcode
function next_link() {
 
          next_post_link();

      
     
 
}
add_shortcode( 'next_link', 'next_link' );


To customize the links I would highly recommend taking a look here
https://codex.wordpress.org/Next_and_Previous_Links

Please let me know if this helps.
Thanks,
Shane

#420227

Thank you very much. Solved the problem.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.