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">«<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 . '»</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
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
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?
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
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,
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
Thank you very much. Solved the problem.