0

I am a hubspot developer mostly, and hubspot has tokens for previous post link and previous post title, and the same for the next post and the images for these posts. You can use these to create buttons that display the title and image of the next and previous posts as well as link to these posts from the article template.

I am attempting to do this in wordpress. Things are generally a lot more technical in wordpress, and the code that I borrowed from the codex is not helping.

function getPrevNext(){
                            $pagelist = get_pages('sort_column=menu_order&sort_order=asc');
                            $pages = array();
                            foreach ($pagelist as $page) {
                                $pages[] += $page->ID;
                            }

                            $current = array_search(get_the_ID(), $pages);
                            $prevID = $pages[$current-1];
                            $nextID = $pages[$current+1];

                            echo '<div class="navigation">';

                            if (!empty($prevID)) { ?>
                                <a class="scroll_button prev" href="<?php get_permalink($prevID); ?>">
                                    <div class="page_center">
                                        <div class="arrow">
                                        </div>
                                        <div class="title">
                                            <div class="top">
                                                <span>Previous Post</span>
                                            </div>
                                            <div class="bottom">
                                                <span><?php echo get_the_title($prevID) ?></span>
                                            </div>
                                        </div>
                                    </div>
                                </a>

                            <?php }
                            if (!empty($nextID)) { ?>

                                <a class="scroll_button next" href="<?php get_permalink($nextID); ?>">
                                    <div class="title">
                                        <div class="top">
                                            <span>Next Post</span>
                                        </div>
                                        <div class="bottom">
                                            <span><?php echo get_the_title($nextID) ?></span>
                                        </div>
                                    </div>
                                    <div class="arrow">
                                    </div>
                                </a>    

                            <?php   }
                        }

                    ?>

I am testing this on the second blog post in my blog. Instead of giving me a previous link it gives me a next link. The title on the link is "Home Page" and the link goes to the blog that I am currently on.

Wordpress has

<?php
previous_post_link();
next_post_link();
?>

but these dont really allow me to customize the link with the html that I have in the above function. Any help would be appreciated.

0

If I'm understanding this correctly, you may just want to use something like:

$prev = get_previous_post();
$next = get_next_post();

Then to access the title you'd do something like

<h1><?php echo $prev->post_title ?></h1>

And if you wanted its image:

$prev_img = get_post_thumbnail_id( $prev->ID );
<img src="<?php echo $prev_img; ?>" alt="your alt here">
  • 1
    Sorry for the late check. You set the previous and next fields to a variable and then pulled the data for the functions into custom html. This worked exactly as I needed. Thanks so much for your help. – JSum Dec 16 '16 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.