WordPress自定义主题面包屑异常求助:需添加Blog页面链接
修复WordPress单文章页面包屑显示Blog页面的问题
我帮你看了下你的自定义面包屑函数,问题出在单文章页的逻辑上——你现在调用的the_category会输出文章所属分类,但你需要的是指向你设置的静态Blog页面的链接,而不是分类。下面是修改后的完整代码,我会在后面说明关键修改点:
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; // 处理单文章页:添加Blog静态页面的链接 if (is_single()) { $page_for_posts_id = get_option('page_for_posts'); if ($page_for_posts_id) { $blog_page = get_page($page_for_posts_id); echo '<a href="' . get_permalink($blog_page->ID) . '">' . get_the_title($blog_page->ID) . '</a>' . $sep; } } // 处理分类/归档页(保留原有逻辑) elseif (is_category() || is_archive()){ 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' ); } } // 处理静态页面(保留原有逻辑) elseif (is_page()) { echo get_the_title(); } // 处理Blog静态列表页(保留原有逻辑) elseif (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(); } } // 单文章页添加文章标题 if (is_single()) { echo get_the_title(); } echo '</nav></div>'; } }
关键修改说明:
- 重构了单文章页的逻辑:把原来混合在分类/归档判断里的
is_single抽出来单独处理,先获取你设置的Blog静态页面ID(get_option('page_for_posts')),然后输出这个页面的链接和标题,再跟上文章标题。 - 调整了条件判断顺序:先处理
is_single,再处理分类/归档、静态页面等,避免逻辑冲突。 - 替换了
the_category:原来的the_category会输出分类链接,但你需要的是统一指向Blog页面,所以换成了Blog页面的固定链接。
现在你的单文章页面包屑会显示成:mysite.com > Blog > 文章标题,完全符合你的需求,而且不需要任何额外插件。
内容的提问来源于stack exchange,提问作者JPDesign
相关产品推荐
相关产品推荐

