取同層級頁面id
/**
取同層級頁面id
bool $with_parent = true ; 包含父層id
return array
https://v123.tw
*/
function v123tw_get_same_level_post_id_of_current_post($with_parent=false){
if(!$id = get_the_ID())return array();
if($pid = wp_get_post_parent_id( $id ))$id = $pid;
if(!$id)return array();
$args = array(
'post_parent' => $id,
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'page',
'post_status' => 'publish',
);
$children_array = get_children($args);
$rekeyed_array = array_keys($children_array);
if(!count($rekeyed_array))return array();
if($with_parent)array_unshift($rekeyed_array,$id);
return $rekeyed_array;
}做成簡碼,方便再主題或文章內使用
/**
取當前頁面之同層級頁面項目
https://v123.tw
*/
add_shortcode('v123tw-submenu',function(){
$submenu = v123tw_get_same_level_post_id_of_current_post();
if($submenu){
$output = '';
$output .= '<div class="v123tw_submenu_div">';
$output .= '<ul>';
foreach($submenu as $id){
$output .= '<li><a href="'.get_post_permalink($id).'"><span>'.get_the_title($id).'</span></a></li>'."\n";
};
$output .= '</ul>';
$output .= '</div>';
echo $output;
}
});