修改代码以传递post-id作为参数,以便在Wordpress中通过AJAX创建面包屑


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

我有一个函数用于在wordpress网站上创建面包屑:

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 as $crumb) echo $crumb;
            echo $currentBefore;
            the_title();
            echo $currentAfter;
        }
        echo '</ul></nav>';
    }
}

但我希望这个函数使用post_id(页面的id)作为参数,以便在为该页面创建面包屑的AJAX函数中使用它。

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

我怎样才能做到这一点?

非常感谢你的帮助。

您需要将post_id作为参数传递,并使用wp_query检索帖子信息,如下所示:

function the_breadcrumb($post_id){
$delimiter = '>';
$currentBefore = '<li><a>';
$currentAfter = '</a></li>';
//    here your query
$args = array(
    'page_id' => $post_id, // id of a page, post, or custom type
    'post_type' => 'any');
// here is the 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 as $crumb) echo $crumb;
        echo $currentBefore;
        the_title();
        echo $currentAfter;
    }
    echo '</ul></nav>';
}

此代码适用于帖子的异步内容:(在functions.php中复制)

function ariane() {
    $cat_id         = get_the_category()[0]->term_id;
    $breadcrumb     = '<li>' . get_the_title() . '</li>';
    $if_parent = TRUE;
    while($if_parent == TRUE) :
        $cat_object     = get_category($cat_id);
        $cat            = $cat_object->term_id;
        $categoryURL    = get_category_link($cat);
        $name           = $cat_object->name;
        $cat_id         = $cat_object->parent;              
        $add_link       = '<li><a href="' . $categoryURL . '">' . $name . '</a></li>';
        $breadcrumb     = substr_replace($breadcrumb, $add_link, 0, 0);
        if($cat_id  == 0) :
            $if_parent = FALSE;
        endif;
    endwhile;
    echo '<ul>' . $breadcrumb . '</ul>';    
}

在您的archive.php或循环中WP_QUERY

<div class="ariane"><?php ariane(); ?></div>

在css中:

.ariane ul{
    display: flex;
    justify-content: flex-start;
    align-items: center;
}  
.ariane li:not(:last-child):after {
    content: '>';
    margin: 0 5px;
}