June 23, 2015

How to apply a custom header image for each page in WordPress?

How to apply a custom header image for each page in WordPress with respect to the navigation menu bar?


Here is the function:
function get_parent_menu_id($id, $arr){
if($arr[$id]==0)
return $id;
else
return get_parent_menu_id($arr[$id], $arr);
}
$arr = array();
$arr_obj = array();
$page_id = get_the_ID();
$menu_items = wp_get_nav_menu_items('Menu Main');
if(!empty($menu_items)){
foreach($menu_items as $items){
$parent = $items->menu_item_parent;
$arr[$items->ID] = $parent;
$arr_obj[$items->object_id] = $items->ID;
}
}
$parent_id = get_parent_menu_id($arr_obj[$page_id], $arr);
$arr_obj = array_flip($arr_obj);
$parent_id = $arr_obj[$parent_id];


<div class="container_alpha slider">
<div style="clear:both">
<div><img src="<?php echo get_stylesheet_directory_uri().'/images/header_images/'.$parent_id.'.jpg'; ?>" /></div>
</div>
</div>

Last updated: June 23, 2015