wordpress breadcrumb - главная> блог> пост - PullRequest
0 голосов
/ 15 мая 2018

Я был бы очень благодарен за любые предложения о моей крошке. 2 дня головных болей, и я просто не могу понять это.

У меня есть статическая страница для записей в блоге, и я хотел бы включить URL в крошку.

mysite.com> блог> пост

Прямо сейчас у меня "mysite.com> post"

В худшем случае удалите домашний URL и измените его на URL блога, так что в этой крошке отображается хотя бы URL блога.

Блог> post

Следующий код прекрасно работает, но он не отображает «URL-адрес блога», так как он называет только «home», но я хотел бы добавить эхо или что-то для отображения статической страницы под названием blog после home (home-blog-post ).

Можно ли добавить ссылку на страницу, например, echo 'Blog '? Или даже удалите дом и поместите перед крошкой как Мой сайт >> Мой блог >>, а затем остальную часть крошки (сообщение / категория).

Моя цель ... mysite.com> блог> пост

Спасибо!

function my_breadcrumb() {
$sep = ' › ';
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>';
   }
* *} Тысяча двадцать-один

//

  • примечание. Мне любопытно, почему страница постов не отображается, поскольку код говорит, что она должна отображаться как Главная >> Блог. (Я создал страницу под названием блог, а затем настройки - чтение - страница постов и использование home.php в качестве шаблона). (На передней странице используется страница с именем welcome, затем настройки - чтение - первая страница, шаблон - front-page.php). ** Я делаю свою собственную тему, и я не хотел бы добавлять больше плагинов.

1 Ответ

0 голосов
/ 15 мая 2018

Отображение Breadcrumb для ожидаемого URL, например: mysite.com> blog> post

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>';
    }
}

Надеюсь, это работает для вас.

...