前面我們學了一行代碼搞定WordPress面包屑導航breadcrumb,現在wordpress文檔中有一個簡單實現的方法,適用於page頁面,有二級分類的情況(Simple breadcrumb trail for pages, two levels deep.),隨ytkah一起看看代碼
echo '<div class="breadcrumb">'; // If there is a parent, display the link. $parent_title = get_the_title( $post->post_parent ); if ( $parent_title != the_title( ' ', ' ', false ) ) { echo '<a href="' . esc_url( get_permalink( $post->post_parent ) ) . '" alt="' . esc_attr( $parent_title ) . '">' . $parent_title . '</a> » '; } // Then go on to the current page link. echo '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark" alt="' . esc_attr( the_title_attribute() ) . '">' . the_title() . '</a>'; echo '</div>';
參考文檔https://developer.wordpress.org/reference/functions/get_the_title/