首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wordpress面包屑-主页>博客>帖子

wordpress面包屑-主页>博客>帖子
EN

Stack Overflow用户
提问于 2018-05-15 02:51:01
回答 1查看 2.8K关注 0票数 2

我会非常感谢任何关于我面包屑的建议。两天的头痛,我就是搞不明白。

我有一个静态页面的博客条目,我想包括url在面包屑。

mysite.com >博客>帖子

现在我有“mysite.com > post”

最坏的情况是删除home url并更改blog url,所以这个面包屑至少会显示blog url。

博客>帖子

下面的代码很好用,但它不显示"blog url“,因为它只称为" home ",但是我想包含一个echo或其他东西来显示名为blog的静态页面(home - blog - post )。

是否可以包括一个链接到一个页面,例如回显'Blog< /a>‘?甚至在面包屑之前将主页和位置移除为< href=/>我的站点< /a> >>我的博客< /a> >>,然后剩下的面包屑( post /类别)。

我的目标..。mysite.com >博客>帖子

谢谢!

代码语言:javascript
复制
function my_breadcrumb() {
$sep = ' &rsaquo; ';
if (!is_front_page()) {

// Start the breadcrumb with a link to your homepage
    echo '<div class="x"><nav class="x">';
    echo '<a href="';
    echo get_option('home');
    echo '">';
    bloginfo('name');
    echo '</a>' . $sep;

// Check if the current page is a category, an archive or a single page. If so show the category or archive name.
    if (is_category() || is_single() ){
        the_category('title_li=');
    } elseif (is_archive() || is_single()){
        if ( is_day() ) {
            printf( __( '%s', 'text_domain' ), get_the_date() );
        } elseif ( is_month() ) {
            printf( __( '%s', 'text_domain' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
        } elseif ( is_year() ) {
            printf( __( '%s', 'text_domain' ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
        } else {
            _e( 'Blog Archives', 'text_domain' );
        }
    }   
// If the current page is a single post, show its title with the separator
    if (is_single()) {
        echo $sep;
        the_title();
    }   
// If the current page is a static page, show its title.
    if (is_page()) {
        echo the_title();
    }
// if you have a static page assigned to be you posts list page. It will find    the title of the static page and display it. i.e Home >> Blog
    if (is_home()){
        global $post;
                    $page_for_posts_id = get_option('page_for_posts');
                    if ( $page_for_posts_id ) { 
                        $post = get_page($page_for_posts_id);
                        setup_postdata($post);
                        the_title();
                        rewind_posts();
                    }
                }
      echo '</nav></div>';
   }

}

//

  • 请注意。我很好奇为什么博文页面没有显示为代码,它应该作为家庭>>博客。(我创建了一个名为blog的页面,然后设置--阅读- posts页面并使用home.php作为模板)。(首页使用的是一个叫做欢迎的页面,然后是设置-阅读-首页,模板是Fron-page.php )。**我正在制作我自己的主题,我不想添加更多的插件。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-15 05:18:40

显示预期url的Breadcrumb,如: mysite.com > blog > post

代码语言:javascript
复制
function my_breadcrumb() {
    $sep = ' > ';
    if (!is_front_page()) {

    // Start the breadcrumb with a link to your homepage
        echo '<div class="breadcrumbs">';
        echo '<a href="';
        echo get_option('home');
        echo '">';
        bloginfo('name');
        echo '</a>' . $sep;

    // Check if the current page is a category, an archive or a single page. If so show the category or archive name.
        if (is_category() || is_single() ){
           echo '<a href="'.get_site_url().'/blog">Blog</a>';
        } elseif (is_archive() || is_single()){
            if ( is_day() ) {
                printf( __( '%s', 'text_domain' ), get_the_date() );
            } elseif ( is_month() ) {
                printf( __( '%s', 'text_domain' ), get_the_date( _x( 'F Y', 'monthly archives date format', 'text_domain' ) ) );
            } elseif ( is_year() ) {
                printf( __( '%s', 'text_domain' ), get_the_date( _x( 'Y', 'yearly archives date format', 'text_domain' ) ) );
            } else {
                _e( 'Blog Archives', 'text_domain' );
            }
        }

    // If the current page is a single post, show its title with the separator
        if (is_single()) {
            echo $sep;
            the_title();
        }

    // If the current page is a static page, show its title.
        if (is_page()) {
            echo the_title();
        }

    // if you have a static page assigned to be you posts list page. It will find the title of the static page and display it. i.e Home >> Blog
        if (is_home()){
            global $post;
            $page_for_posts_id = get_option('page_for_posts');
            if ( $page_for_posts_id ) { 
                $post = get_page($page_for_posts_id);
                setup_postdata($post);
                the_title();
                rewind_posts();
            }
        }
        echo '</div>';
    }
}

希望这对你有用。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50341648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档