在短代码内返回 foreach 内

发布于 2024-11-15 01:52:36 字数 1975 浏览 3 评论 0原文

我使用下面的 PHP 代码来显示基于应用于我的 WordPress 帖子的自定义分类法的 CSS 类。我的分类法称为 CC,它的三个选项是 x、y 和 z。对于包含所有三个的帖子:

 $cc_terms = get_the_terms($post->ID,'cc');
 foreach ($cc_terms as $term) { 
     echo ' '.$term->slug.'-active'; 
 }

输出: x-active y-active z-active

我正在尝试将其转换为 WordPress 短代码函数(据我所知,其中 echo 不起作用)基本上我需要弄清楚如何将 cc_class() 捕获为一个变量(而不是数组),以便它给我与上面相同的输出。

function cc_meta($atts, $content = null) {
    global $post;
    extract(shortcode_atts(array('class' => 'default'), $atts));
    function cc_class() {
        $cc_terms = get_the_terms($post->ID, 'cc');
        foreach($cc_terms as $term) {
            return ' '.$term->slug.'-active';
        }
    }
    return '<div class="' . esc_attr($class) . cc_class() . '">...</div>';
}

输出:

...

它只采用第一项而不是全部三项,这让我想知道如何返回foreach 循环中工作。知道如何让它发挥作用吗?

所需的输出:

...

更新 1:我尝试过按照建议的 .= ,我收到一个空白错误:

function cc_meta( $atts, $content = null ) {
    global $post;
    extract(shortcode_atts(array('class' => 'default'), $atts));

    $cc_terms = get_the_terms($post->ID, 'cc');
    foreach ($cc_terms as $term) {
        $return .= ' '.$term->slug.'-active';
    }
    return '<div class="' . esc_attr($class) . $return . '">...</div>';
}

更新 2:我删除了 global $post 行,它可以工作 - 太好了!谢谢大家=)

function cc_meta($atts, $content = null) {
    extract(shortcode_atts(array('class' => 'default'), $atts));
    $cc_terms = get_the_terms($post->ID, 'cc');
    foreach($cc_terms as $term) { 
        $return .= ' '.$term->slug.'-active';
    }
    return '<div class="' . esc_attr($class) . $return . '">...</div>';
}

I'm using the PHP code below to display CSS classes based on the custom taxonomies applied to my WordPress posts. My taxonomy is called CC and its three options are x, y, and z. For a post that has all three:

 $cc_terms = get_the_terms($post->ID,'cc');
 foreach ($cc_terms as $term) { 
     echo ' '.$term->slug.'-active'; 
 }

outputs: x-active y-active z-active

I'm trying to convert it into WordPress shortcode function (where echo doesn't work as far as I know) and basically I need to figure out how to capture cc_class() as one variable (not an array) so that it give me the same output as above.

function cc_meta($atts, $content = null) {
    global $post;
    extract(shortcode_atts(array('class' => 'default'), $atts));
    function cc_class() {
        $cc_terms = get_the_terms($post->ID, 'cc');
        foreach($cc_terms as $term) {
            return ' '.$term->slug.'-active';
        }
    }
    return '<div class="' . esc_attr($class) . cc_class() . '">...</div>';
}

outputs: <div class="default x-active">...</div>

It's only taking the first term rather than all three, which makes me wonder how return works in foreach loops. Any idea how I can get this to work?

desired output: <div class="default x-active y-active z-active">...</div>

Update 1: I tried with the .= as suggested and I'm getting a blank error:

function cc_meta( $atts, $content = null ) {
    global $post;
    extract(shortcode_atts(array('class' => 'default'), $atts));

    $cc_terms = get_the_terms($post->ID, 'cc');
    foreach ($cc_terms as $term) {
        $return .= ' '.$term->slug.'-active';
    }
    return '<div class="' . esc_attr($class) . $return . '">...</div>';
}

Update 2: I removed the global $post line and it works—sweet! Thanks everyone =)

function cc_meta($atts, $content = null) {
    extract(shortcode_atts(array('class' => 'default'), $atts));
    $cc_terms = get_the_terms($post->ID, 'cc');
    foreach($cc_terms as $term) { 
        $return .= ' '.$term->slug.'-active';
    }
    return '<div class="' . esc_attr($class) . $return . '">...</div>';
}

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

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

发布评论

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

评论(1

夏の忆 2024-11-22 01:52:36

不要在函数中间返回,仅在最后使用返回。

使用以下代码代替 return

$return .= 

并在结束 } 之前执行 return $return;

Don't return in the middle of your function, use return only at the end.

Instead of return use this:

$return .= 

and before the closing } do a return $return;

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