【问题标题】:Exclude category from get_category_parents (wordpress)从 get_category_parents (wordpress) 中排除类别
【发布时间】:2011-04-27 17:03:45
【问题描述】:

我正在寻找一种方法来从我的面包屑主题的 get_category_parents 中排除 ID 为 3 和 4 的类别。这是代码,有问题的行是11:

function the_breadcrumb() {
global $post;
if (!is_home()) {
    echo '<a href="'.get_option('home').'">'.home.'</a>';
    if (is_category()) {
        echo " / ";
        echo single_cat_title();
    } elseif(is_single() && !is_attachment()) {
        $cat = get_the_category(); $cat = $cat[0];
        echo " / ";
        echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
        echo " / ";
        echo thman_get_limited_string($post->post_title,30);
    }       
    elseif (is_search()) {
        echo " / " . cerca;
    }       
    elseif (is_page() && $post->post_parent) {
        echo ' / <a href="'.get_permalink($post->post_parent).'">';
        echo get_the_title($post->post_parent);
        echo "</a> / ";
        echo thman_get_limited_string($post->post_title,30);        
    }
    elseif (is_page() OR is_attachment()) {
        echo " / "; 
        echo thman_get_limited_string($post->post_title,30);
    }
    elseif (is_author()) {
        echo wp_title(' / Profilo');
        echo "";
    }
    elseif (is_404()) {
        echo " / "; 
        echo errore_404;
    }       
    elseif (is_archive()) {
        echo wp_title(' / ');       
    }
}
    }

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:
    $cat = get_the_category();
    $cat = $cat[0]->term_id;
    // next will return an array of all category ancestors, with toplevel cat being [0]
    $ancestors = array_reverse(get_ancestors($cat, 'category'));
    if($ancestors) {
      // set up output
      $output = '';
      foreach($ancestors as $cat) {
        // skips cats 3 and 4
        if($cat == '3' || $cat == '4') continue;
        $catlink = get_category_link($cat);
        $catname = get_cat_name($cat);
        $output .= '<a href="' . $catlink . '">' . $catname . '</a>' . "\n";
      }
    }
    
    echo $output;
    

    这不是我的想法,但我相信它是正确的。

    【讨论】:

    • +1...很好的答案雪莉!这绝对可以完成工作!
    • 感谢 shelly 的回答,但我不是 php 专家,所以我的完整代码应该如下所示?
    • 好的 - 请原谅我 - 我在这些论坛上有点像 n00b :) 我在下面找不到对您的问题发表评论的“评论”链接!但是,是的,我给你的代码应该替换“elseif(is_single() && !is_attachment()) {”部分中的内容——我相信这就是你下面的内容。同样,该代码不在我的脑海中,因此您可能需要稍微尝试一下-但我认为它是正确的。希望它有所帮助:)
    • 感谢 shelly,但给了我错误:解析错误:语法错误,意外 ';'到这个位置 $ancestors = array_reverse(get_ancestors($cat, 'category');
    • ok 错过了一个括号 $ancestors = array_reverse(get_ancestors($cat, 'category'));但现在错误如下致命错误:调用未定义函数获取祖先()
    【解决方案2】:

    感谢shelly的回答,但我不是php专家,所以我的完整代码应该是这样的?

    function the_breadcrumb() {
    global $post;
    if (!is_home()) {
        echo '<a href="'.get_option('home').'">'.home.'</a>';
        if (is_category()) {
            echo " / ";
            echo single_cat_title();
        } elseif(is_single() && !is_attachment()) {
          $cat = get_the_category(); $cat = $cat[0]->term_id;
      // next will return an array of all category ancestors, with toplevel cat being [0]
      $ancestors = array_reverse(get_ancestors($cat, 'category');
      if($ancestors) {
      // set up output
      $output = '';
      foreach($ancestor as $cat) {
      // skips cats 3 and 4
      if($cat == '3' || $cat == '4') continue;
      $catlink = get_category_link($cat);
      $catname = get_cat_name($cat);
      $output .= '<a href="' . $catlink . '">' . $catname . '</a>' . "\n";
       }
      }
      echo $output;
            echo " / ";
            echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
            echo " / ";
            echo the_title_shorten(45,'...');
        }       
        elseif (is_search()) {
            echo " / " . cerca;
        }       
        elseif (is_page() && $post->post_parent) {
            echo ' / <a href="'.get_permalink($post->post_parent).'">';
            echo get_the_title($post->post_parent);
            echo "</a> / ";
            echo the_title_shorten(45,'...');       
        }
        elseif (is_page() OR is_attachment()) {
            echo " / "; 
            echo the_title_shorten(45,'...');
        }
        elseif (is_author()) {
            echo wp_title(' / Profilo');
            echo "";
        }
        elseif (is_404()) {
            echo " / "; 
            echo errore_404;
        }       
        elseif (is_archive()) {
            echo wp_title(' / ');       
        }
    }
        }
    

    【讨论】:

      【解决方案3】:

      谢谢大家, 我终于让代码工作了:

      $ancestors = array_reverse(get_ancestors(get_cat_ID(single_cat_title("", false)), 'category'));
      $cat_parent_and_cat = '';
      if($ancestors) {
          foreach($ancestors as $cat) {               
               //if($cat == '3' || $cat == '4') continue; // skips cats 3 and 4
               $catlink = get_category_link($cat);
               $catname = get_cat_name($cat);
               $cat_parent_and_cat .= '<a href="' . $catlink . '">' . $catname . '</a>' . ' &rsaquo; ' ;
          }
      }
      echo $cat_parent_and_cat;
      

      希望对大家有所帮助

      【讨论】:

        猜你喜欢
        • 2013-10-29
        • 2014-12-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-14
        • 1970-01-01
        相关资源
        最近更新 更多