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!
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!
Share Improve this question edited Jul 17, 2020 at 2:37 mozboz 2,6281 gold badge12 silver badges23 bronze badges asked Jul 16, 2020 at 13:01 Sten StmithSten Stmith 32 bronze badges2 Answers
Reset to default 0To get a post title by ID you need: get_the_title( $id );
.
You can integrate this into your code using a similar method for how this has been done for the URL's with $link, but that's basically a PHP coding question, so it's best to get as far as you can with the code and then post again if you get stuck.
<?php
$menu_name = 'primary';
$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 );
$title = get_the_title( $id );
$linkarray.=$id.",";
$urlarray.=$link.",";
$titlearray.=$title.",";
if ($id==$post->ID){
$previd=$i-1;
$nextid=$i+1;
$titleid=$i+1;
}
endforeach;
$linkarray=explode(',',$linkarray);
$urlarray=explode(',',$urlarray);
$titlearray=explode(',',$titlearray);
$titleid=$titlearray[$titleid];
$nextid=$urlarray[$nextid];
if (empty($nextid)){
$nextid=$urlarray[0];
}
$previd=$urlarray[$previd];
if (empty($previd)){
$previd=$urlarray[$i];
}
?>
<?php echo $titleid; ?>
This is the edited working version, in case if somebody will need.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742255993a4410029.html
评论列表(0条)