[WordPress]先祖postへのリンクを出力する

function get_parents($post,$parents = array())
{
    if (!empty($post->post_parent)){
        $parent = get_post($post->post_parent);
        array_unshift( $parents, $parent);
        return get_parents( $parent, $parents );
    }
    return $parents;
}
function the_parents($post,$format)
{
    $parents = get_parents($post);
    if(!empty($parents)){
        foreach($parents as $i => $parent){
            $html = $format;
            $html = preg_replace('/%permalink%/',get_permalink($parent->ID),$html);
            $html = preg_replace('/%title%/',esc_html($parent->post_title),$html);
            echo $html;
        }
    }
}