从 get_category_parents 中排除类别(wordpress)

发布于 2024-11-03 09:28:11 字数 1355 浏览 2 评论 0原文

我正在寻找一种方法,从面包屑主题的 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(' / ');       
    }
}
    }

I'm looking for a way to exclude categories with ID 3 and 4 from get_category_parents of my breadcrumb theme. This is the code, The line in question is the 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(' / ');       
    }
}
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南烟 2024-11-10 09:28:11
$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;

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

$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;

That's off the top of my head, but I believe it's correct.

冷情妓 2024-11-10 09:28:11

谢谢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(' / ');       
    }
}
    }

thanks shelly the answer, but I'm not an expert in php, so my full code should look like this?

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(' / ');       
    }
}
    }
无妨# 2024-11-10 09:28:11

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

$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>' . ' › ' ;
    }
}
echo $cat_parent_and_cat;

希望它能帮助别人

Thanks for all of you,
I finally get the code working:

$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>' . ' › ' ;
    }
}
echo $cat_parent_and_cat;

Hope it will help someone

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文