【问题标题】:Wordpress breadcrumbs issueWordpress 面包屑问题
【发布时间】:2014-03-21 17:16:20
【问题描述】:

我的网站中有一个无法正常运行的面包屑导航。 当我去像

这样的产品时

Equipment» Products» Railing» Heavy Railing» Waterproof» Superrail 2000

它正确地向我显示了这个面包屑。但是,当我通过“非防水”而不是“防水”获得相同的产品时,它会显示相同的面包屑(所以基本上,它只遵循 1 个类别)。

我想在我的面包屑插件的设置中检查这一点,但它显示消息“您的设置已过期。立即迁移。”,我的主管建议我不要“迁移”它。

有没有人使用插件或自定义代码来处理多个类别的解决方案?

编辑:

我正在使用 WP 3.6,我不允许对其进行更新。 URL 看起来像这样http://www.website.com/product/asl6109t32/,这意味着我不能使用 URL 来生成面包屑。

【问题讨论】:

标签: php wordpress breadcrumbs


【解决方案1】:

如果可以的话,这里有一些漂亮的代码 sn-p 用于创建面包屑导航,将其放在主题的functions.php 文件中:

/*********************************************************** 
* wsf_breadcrumbs() - Shows breadcrumbs in template 
***********************************************************/  
function wsf_breadcrumbs( $sep = '/', $label = 'Browsing' ) {  

    global $post, $front_page;  

   // Do not show breadcrumbs on home or front pages.  
   // So we will just return quickly  
   if((is_home() || is_front_page()) && (!$front_page))  
      return;  

    // Create a constant for the separator, with space padding.  
    $SEP = ' ' . $sep . ' ';  

  echo '<div class="breadcrumbs">';  
  echo $label;  
  echo wsf_make_link( home_url(), 'Home', get_bloginfo('name'), true ) . $SEP;  

  if(is_single()) {  
    the_category(', '); echo $SEP;   
  } 
    elseif (is_search()) {
        echo 'Search results - ';
    } 
    elseif (is_404()) { //Wordpress bug
        echo 'Error 404 (Page not found) '; 
    }
    elseif (is_author()) {
        echo 'Articles posted by author ';
    }
    elseif (is_tag()) { 
        echo 'Posts tagged ';
    }
    elseif (is_year()) {
        echo get_the_time('Y');
    }
  elseif (is_month()) {
        echo get_the_time('F').' ('.get_the_time('Y').')';
    }

    elseif (strstr($_SERVER['REQUEST_URI'],"wp-login.php")) {
        echo 'Administration panel';
    }


    elseif(is_page()) {  
        $parent_id = $post->post_parent;  
        $parents = array();  
        while($parent_id) {  
                $page = get_page($parent_id);  
        $parents[]  = wsf_make_link( get_permalink($page->ID), get_the_title($page->ID) ) . $SEP;  
                $parent_id  = $page->post_parent;  
        }  
        $parents = array_reverse($parents);  
    foreach($parents as $parent) {  
       echo $parent;  
    }  
  }  
   // Wordpess function that echoes your post title.  
  the_title();  
  echo '</div>';  
}  
/*------------------------------------------------------*/
/* Create breadcrumb navigation
/*------------------------------------------------------*/
/*********************************************************** 
* Helper Functions for template coding 
***********************************************************/  
function wsf_make_link ( $url, $anchortext, $title=NULL, $nofollow=false ) {  
   if ( $title == null ) $title=$anchortext;  
   $rel = ($nofollow == true) ? ' rel="nofollow"' : $rel = '';   

   $link = sprintf( '<a href="%s" title="%s" %s>%s</a>', $url, $title, $rel, $anchortext );  
   return $link;   
}  

/*------------------------------------------------------*/
/*------------------------------------------------------*/

然后,在您的 footer.php 文件中,像这样调用函数:

<div id="breadcrumb">
 <?php wsf_breadcrumbs('','') ?>  
</div>

【讨论】:

  • 如果 url 不包含类别,这会显示类别吗?
  • 我已经实现了这个,它显示给我的只是Home ASL6109T/32
  • 必须正确设置页面/帖子,即具有正确的parent 以便面包屑导航起作用。
  • 它如何知道我是通过哪个父页面访问的?我需要设置多个类别。
  • foreach($parents as $parent) { echo $parent; },它通过所有的父母,看代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多