在 WordPress 中检查 if_parent 和 if_child

发布于 2024-08-13 15:21:37 字数 69 浏览 2 评论 0原文

我需要为 WP 创建一个函数,用于检查当前类别是否既是 X 类别的子类别又是 Z 类别的父类别。

有想法吗?

I need to create a function for WP that will check if the current category is both child of X category and parent to Z category.

Ideas?

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

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

发布评论

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

评论(3

慈悲佛祖 2024-08-20 15:21:37

未经测试的 MySQL 查询可帮助您入门:

SELECT x.cat_ID, y.cat_ID, z.cat_ID
FROM   categories y,
    LEFT JOIN categories x ON y.parent = x.cat_ID,
    LEFT JOIN categories z ON z.parent = y.cat_ID,
WHERE  y.cat_name = ? AND
       x.cat_name = ? AND
       z.cat_name = ?

Untested MySQL query to get you started:

SELECT x.cat_ID, y.cat_ID, z.cat_ID
FROM   categories y,
    LEFT JOIN categories x ON y.parent = x.cat_ID,
    LEFT JOIN categories z ON z.parent = y.cat_ID,
WHERE  y.cat_name = ? AND
       x.cat_name = ? AND
       z.cat_name = ?
人海汹涌 2024-08-20 15:21:37

试试这个:

<?php
$catid = get_query_var('cat');
if (cat_is_ancestor_of($catid,$test_child_cat) && cat_is_ancestor_of($test_parent_cat,$catid)) {
echo "Current Category is child of X and Parent of Y";
}
?>

Try this:

<?php
$catid = get_query_var('cat');
if (cat_is_ancestor_of($catid,$test_child_cat) && cat_is_ancestor_of($test_parent_cat,$catid)) {
echo "Current Category is child of X and Parent of Y";
}
?>
半窗疏影 2024-08-20 15:21:37

我终于编写了这个简单的解决方案,让父级和子级形成 WordPress 中当前查看的类别:

  $children = $wp_query->query_vars[category__in];
  $count = 0;

  echo 'Parent: ' . $wp_query->queried_object->parent;
        echo ' | ';
  echo 'Children: ';

  foreach ($children as $child) { 
   if (($wp_query->query_vars[category__in][$count]) != ($wp_query->query_vars[cat])) {
     echo $wp_query->query_vars[category__in][$count];
     echo ' ';
    }
   $count++;
  }

I finally got to write this simple solution for getting both Parent and Children form the current viewed category in Wordpress:

  $children = $wp_query->query_vars[category__in];
  $count = 0;

  echo 'Parent: ' . $wp_query->queried_object->parent;
        echo ' | ';
  echo 'Children: ';

  foreach ($children as $child) { 
   if (($wp_query->query_vars[category__in][$count]) != ($wp_query->query_vars[cat])) {
     echo $wp_query->query_vars[category__in][$count];
     echo ' ';
    }
   $count++;
  }

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