php - Modify code to pass a post id as a parameter in order to create a breadcrumb via AJAX in Wordpress -



php - Modify code to pass a post id as a parameter in order to create a breadcrumb via AJAX in Wordpress -

i have function utilize create breadcrumb on wordpress site:

function the_breadcrumb() { $delimiter = '>'; $currentbefore = '<li><a>'; $currentafter = '</a></li>'; if ( !is_home() && !is_front_page() || is_paged() ) { echo '<nav class="breadcrumb"><ul>'; global $post; if ( is_page() && !$post->post_parent ) { echo $currentbefore; the_title(); echo $currentafter; } elseif ( is_page() && $post->post_parent ) { $parent_id = $post->post_parent; $breadcrumbs = array(); while ($parent_id) { $page = get_page($parent_id); $breadcrumbs[] = '<li><a href="' . get_permalink($page->id) . '">' . get_the_title($page->id) . '</a></li>'; $parent_id = $page->post_parent; } $breadcrumbs = array_reverse($breadcrumbs); foreach ($breadcrumbs $crumb) echo $crumb; echo $currentbefore; the_title(); echo $currentafter; } echo '</ul></nav>'; } }

but function take post_id (id of page) parameter in order utilize in ajax function create breadcrumb page.

function ajaxify() { $post_id = $_post['post_id']; $breadcrumb = the_breadcrumb($post_id); print_r($breadcrumb); die(); // remove trailing 0 }

how can accomplish that?

many help.

you need pass post_id argument , retrieve post info wp_query :

function the_breadcrumb($post_id){ $delimiter = '>'; $currentbefore = '<li><a>'; $currentafter = '</a></li>'; // here query $args = array( 'page_id' => $post_id, // id of page, post, or custom type 'post_type' => 'any'); // here informations $mypost = new wp_query($args); if ( !is_home() && !is_front_page() || is_paged() ) { echo '<nav class="breadcrumb"><ul>'; if ( is_page() && !$mypost->post_parent ) { echo $currentbefore; the_title(); echo $currentafter; } elseif ( is_page() && $mypost->post_parent ) { $parent_id = $mypost->post_parent; $breadcrumbs = array(); while ($parent_id) { $page = get_page($parent_id); $breadcrumbs[] = '<li><a href="' . get_permalink($page->id) . '">' . get_the_title($page->id) . '</a></li>'; $parent_id = $page->post_parent; } $breadcrumbs = array_reverse($breadcrumbs); foreach ($breadcrumbs $crumb) echo $crumb; echo $currentbefore; the_title(); echo $currentafter; } echo '</ul></nav>'; }

php ajax wordpress function

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -