How to implement WordPress next/previous post navigation using HTML links

I’m working on a custom WordPress theme and need help with portfolio navigation. I want to add next and previous post buttons that work within individual post content areas.

Currently I can use this code in my template files:

<div>
  <?php prev_post_link('%link', 'Previous'); ?> | 
  <?php next_post_link('%link', 'Next'); ?>
</div>

This works fine when placed in single-portfolio.php but I need the navigation to appear inside the actual post content instead of just in the template structure.

When I try adding PHP code directly in the post editor it doesn’t execute. My theme seems to strip out PHP from post content for security reasons.

Can I create a separate PHP file with custom navigation functions and somehow trigger them using regular HTML links within my post content? I’m looking for a way to make this work without modifying core template files.

You could also use a custom function hooked to the content filter. I’ve done this when shortcodes felt too manual for consistent nav placement. Add this to your functions.php:

function add_portfolio_navigation($content) {
    if (is_single() && get_post_type() == 'portfolio') {
        $nav = '<div class="portfolio-nav">';
        $nav .= get_previous_post_link('%link', 'Previous');
        $nav .= ' | ';
        $nav .= get_next_post_link('%link', 'Next');
        $nav .= '</div>';
        $content .= $nav;
    }
    return $content;
}
add_filter('the_content', 'add_portfolio_navigation');

This automatically adds navigation to every portfolio post without manual insertion. You get consistency across all posts, but lose granular control over placement compared to shortcodes.

yeah, shortcodes r great for this! just drop add_shortcode('portfolio_nav', 'your_nav_function'); into functions.php, then u can use [portfolio_nav] in ur post. it handles the nav without messing with other files - easy win!

Here’s another option: build a custom meta box for portfolio posts with a simple on/off toggle for navigation. I did this when I needed more control than automatic filters but didn’t want shortcodes messing up my content editor. Add a meta box in functions.php with a ‘Show Navigation’ checkbox, then update your single-portfolio.php template to check that value and show/hide the nav accordingly. You get per-post control without touching the content area. Best part? Navigation logic stays in templates where it should be, but you still control everything. Switch themes later and you won’t have leftover shortcodes or filter conflicts. Really handy when some portfolio pieces need nav and others don’t because of different layouts.