如何在页面内容中显示此自定义的简码(Divi Builder)?

时间:2019-07-02 17:59:09

标签: php wordpress shortcode wordpress-shortcode

我正在使用Divi主题。

简码正在工作,但是它直接显示在标题下方,而不是我在Divi Builder中放置简码的位置。

我阅读了更多有关WP短代码的信息,看起来我们应该使用return而不是echo,但是当我将其更改为return时,它根本不会显示在页面上。

谢谢!

function breadcrumb() {
$delimiter = '»'; // delimiter between crumbs
$home = 'Home'; // text for the 'Home' link
$showCurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
$before = '<span class="current">'; // tag before the current crumb
$after = '</span>'; // tag after the current crumb
global $post;
$homeLink = get_bloginfo('url');
    echo '<div id="crumbs"><a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
        if (is_single() && !is_attachment()) {
        if (get_post_type() != 'post') {
            $post_type = get_post_type_object(get_post_type());
            $slug = $post_type->rewrite;
            return '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
            if ($showCurrent == 1) {
                return ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
            }
        } else {
            $cat = get_the_category();
            $cat = $cat[0];
            $cats = get_category_parents($cat, true, ' ' . $delimiter . ' ');
            if ($showCurrent == 0) {
                $cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
            }
            echo $cats;
            if ($showCurrent == 1) {
                echo $before . get_the_title() . $after;
            }
        }
    } elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
        $post_type = get_post_type_object(get_post_type());
        return $before . $post_type->labels->singular_name . $after;
    } elseif (is_attachment()) {
        $parent = get_post($post->post_parent);
        $cat = get_the_category($parent->ID);
        $cat = $cat[0];
        return get_category_parents($cat, true, ' ' . $delimiter . ' ');
        echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>';
        if ($showCurrent == 1) {
            echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
        }
    } elseif (is_page() && !$post->post_parent) {
        if ($showCurrent == 1) {
            return $before . get_the_title() . $after;
        }
    } elseif (is_page() && $post->post_parent) {
        $parent_id  = $post->post_parent;
        $breadcrumbs = array();
        while ($parent_id) {
            $page = get_page($parent_id);
            $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
            $parent_id  = $page->post_parent;
        }
        $breadcrumbs = array_reverse($breadcrumbs);
        for ($i = 0; $i < count($breadcrumbs); $i++) {
            return $breadcrumbs[$i];
            if ($i != count($breadcrumbs)-1) {
                return ' ' . $delimiter . ' ';
            }
        }
        if ($showCurrent == 1) {
            return ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
        }
    }
    return '</div>';

}

1 个答案:

答案 0 :(得分:0)

function breadcrumb() {
$delimiter = '&raquo;'; // delimiter between crumbs
$home = 'Home'; // text for the 'Home' link
$showCurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
$before = '<span class="current">'; // tag before the current crumb
$after = '</span>'; // tag after the current crumb
global $post;
$homeLink = get_bloginfo('url');
    $bredcrumb =  '<div id="crumbs"><a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
        if (is_single() && !is_attachment()) {
        if (get_post_type() != 'post') {
            $post_type = get_post_type_object(get_post_type());
            $slug = $post_type->rewrite;
            $bredcrumb .='<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
            if ($showCurrent == 1) {
                $bredcrumb .=' ' . $delimiter . ' ' . $before . get_the_title() . $after;
            }
        } else {
            $cat = get_the_category();
            $cat = $cat[0];
            $cats = get_category_parents($cat, true, ' ' . $delimiter . ' ');
            if ($showCurrent == 0) {
                $cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
            }
            $bredcrumb .= $cats;
            if ($showCurrent == 1) {
                $bredcrumb .= $before . get_the_title() . $after;
            }
        }
    } elseif (!is_single() && !is_page() && get_post_type() != 'post' && !is_404()) {
        $post_type = get_post_type_object(get_post_type());
        $bredcrumb .= $before . $post_type->labels->singular_name . $after;
    } elseif (is_attachment()) {
        $parent = get_post($post->post_parent);
        $cat = get_the_category($parent->ID);
        $cat = $cat[0];
        $bredcrumb .= get_category_parents($cat, true, ' ' . $delimiter . ' ');
        $bredcrumb .= '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>';
        if ($showCurrent == 1) {
            $bredcrumb .= ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
        }
    } elseif (is_page() && !$post->post_parent) {
        if ($showCurrent == 1) {
            $bredcrumb .= $before . get_the_title() . $after;
        }
    } elseif (is_page() && $post->post_parent) {
        $parent_id  = $post->post_parent;
        $breadcrumbs = array();
        while ($parent_id) {
            $page = get_page($parent_id);
            $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
            $parent_id  = $page->post_parent;
        }
        $breadcrumbs = array_reverse($breadcrumbs);
        for ($i = 0; $i < count($breadcrumbs); $i++) {
            $bredcrumb .= $breadcrumbs[$i];
            if ($i != count($breadcrumbs)-1) {
                $bredcrumb .= ' ' . $delimiter . ' ';
            }
        }
        if ($showCurrent == 1) {
            $bredcrumb .= ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
        }
    }
    $bredcrumb .= '</div>';
    echo $bredcrumb;
    return $bredcrumb;