How to get post titles by post ID and integrate with my code?

Question

I have function which gets menu next and previous item url. I want to get also title for them. How can I do this? This is what i have :

  <?php
$menu_name = 'main-menu';
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

$i=-1;
foreach ( $menuitems as $item ):

    $i++;

  $id = get_post_meta( $item->ID, '_menu_item_object_id', true );
  $page = get_page( $id );
  $link = get_page_link( $id );

    $linkarray.=$id.",";
    $urlarray.=$link.",";

  if ($id==$post->ID){
    $previd=$i-1;
    $nextid=$i+1;
  }
endforeach;

$linkarray=explode(',',$linkarray);
$urlarray=explode(',',$urlarray);

$nextid=$urlarray[$nextid];
if (empty($nextid)){
    $nextid=$urlarray[0];
}
$previd=$urlarray[$previd];
if (empty($previd)){
    $previd=$urlarray[$i];
}
?>

<a href="<?php echo $nextid; ?>">Next Item</a>
<a href="<?php echo $previd; ?>">Previous Item</a>

Destination point is to replace ‘next item’ and ‘previous item’ with wp_nav_menu’s next and prev item titles. Is that possible with this function? Any answer will be helpful!

0
, Sten Stmith 1 year 2020-07-16T22:10:35-05:00 0 Answers 46 views 0

Leave an answer

Browse
Browse