• Resolved chrispink

    (@chrispink)


    It would be great if I could add a custom field to my post navigation. I have a custom post type ‘courses’ and most of the listings are for courses with the same title so I would like to add the ACF field ‘date’ to the navigation label.

    There’s already a bit of code (I’ve hacked Big Blank rather than start from scratch -note to self, it doesn’t really save any time in the long run ) most of which is probably not used by me.

    function ala_post_nav() {
            // Don't print empty markup if there's nowhere to navigate.
            $previous = ( is_attachment() ) ? get_post(get_post()->post_parent) : get_adjacent_post(false, '', true);
            $next = get_adjacent_post(false, '', false);
            $is_last_page = '';
            if (!$next && !$previous) {
                return;
            }
            if (!$next) {
                $is_last_page = 'no-next';
            }
            ?>
            <nav class="navigation post-navigation <?php echo $is_last_page; ?>" role="navigation">
                <h1 class="screen-reader-text"><?php _e('Post navigation', 'ala'); ?></h1>
                <div class="nav-links">
                    <?php
                    if (is_attachment()) :
                        previous_post_link('%link', __('<span class="meta-nav">Published In</span>%title', 'ala'));
                    else :
                        previous_post_link('%link', __('<span class="meta-nav">Previous Post</span>%title', 'ala'));
                        next_post_link('%link', __('<span class="meta-nav">Next Post</span>%title', 'ala'));
                    endif;
                    ?>
                </div><!-- .nav-links -->
            </nav><!-- .navigation -->
            <?php
        }
Viewing 1 replies (of 1 total)
  • Thread Starter chrispink

    (@chrispink)

    Unfortunately (maybe, maybe not) I have the bad habit of answering my own questions. So of anyone who treads this path, here’s the solution;

    function ala_post_nav() {
            // Don't print empty markup if there's nowhere to navigate.
            $prev = get_adjacent_post(false, '', true);
    
    	    $next = get_adjacent_post(false, '', false);
    
            $is_last_page = '';
            if (!$next && !$prev) {
                return;
            }
            if (!$next) {
                $is_last_page = 'no-next';
            }
    
    	   $prev_date = array(""); $next_date = array("");
           if ($next) { $next_date = get_post_custom_values( 'date', $next->ID ); }
           if ($prev) { $prev_date = get_post_custom_values( 'date', $prev->ID ); }
    
             ?>
            <nav class="navigation post-navigation <?php echo $is_last_page; ?>" role="navigation">
                <div class="nav-links">
                    <?php
                        previous_post_link('%link', __('<span class="meta-nav">Previous Post</span>%title<br/>'.$prev_date[0], 'ala'));
    					next_post_link('%link', __('<span class="meta-nav">Next Post</span>%title<br/>'.$next_date[0], 'ala'));
                   ?>
                </div><!-- .nav-links -->
            </nav><!-- .navigation -->
            <?php
        }

    There’s probably a more elegant way of testing for the existence of the posts but this works.

Viewing 1 replies (of 1 total)
  • The topic ‘Adding a custom field to post navigation’ is closed to new replies.