在 WordPress 中返回短代码内的函数时出现问题

发布于 2024-10-16 07:34:31 字数 2094 浏览 2 评论 0原文

我在编写主题简码时遇到一些麻烦。我希望代码显示一个带有视图计数器功能的 div,然后显示一个以 shotcodes 内容作为 url 的链接。

view_count();在主题文件内部调用时,函数工作正常,我实际上设法让它显示,但随后它显示在 the_content(); 之前当我想要将其包含在元素下的内容中时,该帖子(灰色条)的位置。

(1)这是我得到的:

function video_block( $atts, $content = null ) { 
    return '<div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="' . $content . '">Link</a></div>';
}

(2)这是在页面顶部显示的代码:

function video_block( $atts, $content = null ) { ?>
    <div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>
<?php }

(3)此代码显示内容上方的视图,以及正确位置的链接:

function video_block( $atts, $content = null ) {
    $views = the_views();
    return '<div class="video-block"><span class="ViewCount"><?php $views; ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>';
}

我在 WordPress 论坛的某处读到你应该在函数中返回(而不是回显)值,但这会破坏它,显示视图计数,跳过 html 并吐出 $content。

以下是相关页面的链接:http://nvrt.me/4Qf1(当前使用来自第 2 块)

我的夜灯快用完了。如果有人能帮助我,我将非常感激。


编辑:

这是 the_views() 的代码;功能。我可以看到它是回显的,但是当更改为返回时,它根本不显示。

### Function: Display The Post Views
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
    $post_views = intval(post_custom('views'));
    $views_options = get_option('views_options');
    if ($always || should_views_be_displayed($views_options)) {
        $output = $prefix.str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']).$postfix;
        if($display) {
            echo apply_filters('the_views', $output);
        } else {
            return apply_filters('the_views', $output);
        }
    }
    elseif (!$display) {
        return '';
    }
}

I have some trouble coding a themes shortcode. I want the code to display a div with a view counter function, and then a link with the shotcodes content as the url.

The view_count(); function works fine when called inside theme files, and I actually managed to make it show, but then it displayed before the_content(); of the post (the gray bar), when I wanted it within content under an element.

(1) Here's what I've got:

function video_block( $atts, $content = null ) { 
    return '<div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="' . $content . '">Link</a></div>';
}

(2) Here's the code that displays both on top of page:

function video_block( $atts, $content = null ) { ?>
    <div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>
<?php }

(3) This code displays the views above content, and the link in the correct place:

function video_block( $atts, $content = null ) {
    $views = the_views();
    return '<div class="video-block"><span class="ViewCount"><?php $views; ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>';
}

I read somewhere in the Wordpress forums that you should return (instead of echo) values within functions, but that breaks it, displaying the view count, skips the html and spits out the $content.

Here is a link to the page in question: http://nvrt.me/4Qf1 (currently using code from block #2)

I'm running out of midnight oil. I would really appreciate it if someone could help me out.


EDIT:

Here is the code for the_views(); function. I can see that it's echoed, but when changed to return, it doesn't display it at all.

### Function: Display The Post Views
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
    $post_views = intval(post_custom('views'));
    $views_options = get_option('views_options');
    if ($always || should_views_be_displayed($views_options)) {
        $output = $prefix.str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']).$postfix;
        if($display) {
            echo apply_filters('the_views', $output);
        } else {
            return apply_filters('the_views', $output);
        }
    }
    elseif (!$display) {
        return '';
    }
}

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

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

发布评论

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

评论(3

柒七 2024-10-23 07:34:31

尽管在 WordPress 中建议使用返回值的函数,但并不总是可行,特别是当您调用另一个将其输出直接写入到溪流。

当组件将其输出直接写入流时,您需要编码以适应该行为,除非您想重写 who 组件(我不会这样做;-))。

在这种情况下, the_views() 函数实际上为您提供了这两个选项。如果您查看 $display 参数并遵循代码,则该函数可以以两种方式运行。如果 $display 设置为 True(默认值),它将回显该函数的结果。如果 $display 设置为 False 它将返回输出。

因此,您有两个选项,这两个选项都应该有效:

选项 1,返回一个值

请注意,当我调用 the_views() 时,我向它传递了一个错误参数,如下所示: the_views (false)

<?php

function video_block( $atts, $content = null ) { 
  return "<div class=\"video-block\"><span class=\"ViewCount\">" . the_views(false) .  "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}

?>

*选项 2:回显您的输出 *

请注意,当我调用 the_views() 时,没有传递给它的参数。

<?php

function video_block( $atts, $content = null ) { 
  echo "<div class=\"video-block\"><span class=\"ViewCount\">";
  the_views();
  echo "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}

?>

哦,另外,当您返回字符串时,不要忘记转义引号。

Even though it is a recommended practice in Wordpress to have functions that return values, it is not always possible, especially when you are calling another function that writes it's output directly to the stream.

When the component writes it's output directly to the stream you need to code to accommodate that behavior unless you want to rewrite the who component (I wouldn't do that ;-) ).

In this instance the the_views() function actually offers you both options. If you look at the $display parameter and follow the code this function can behave both ways. If $display is set to True (it's default) it will echo the results of the function. If $display is set to False it will return the output.

So you have two options, both of which should work:

Option 1, Return a value

Note that when I call the_views() I pass it a false parameter like this: the_views(false)

<?php

function video_block( $atts, $content = null ) { 
  return "<div class=\"video-block\"><span class=\"ViewCount\">" . the_views(false) .  "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}

?>

*Option 2: Echo your output *

Note that when I call the the_views() there are no parameters passed to it.

<?php

function video_block( $atts, $content = null ) { 
  echo "<div class=\"video-block\"><span class=\"ViewCount\">";
  the_views();
  echo "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}

?>

Oh, also, don't forget to escape your quotes when you are returning a string.

枯叶蝶 2024-10-23 07:34:31

无法添加代码块作为对 @BLewis 响应的评论,但根据他所说的内容,我做了这样的事情:

function my_shortcode() {
  ob_start();
  a_function_to_display();
  $data = ob_get_clean();
  return $data;
}
add_shortcode( 'shortcode_name', 'my_shortcode' );

Couldn't add the code block as a comment on @BLewis response but working off what he said, I did something like this:

function my_shortcode() {
  ob_start();
  a_function_to_display();
  $data = ob_get_clean();
  return $data;
}
add_shortcode( 'shortcode_name', 'my_shortcode' );
得不到的就毁灭 2024-10-23 07:34:31

我想在这里贡献一个新的答案。

我正在努力扩展一个插件,发现作者就是这样做的,对我来说这更有意义。

如果您要输出大量 HTML,则不应使用 echo,这是不好的做法,难以阅读,而且使用 echo 保持输出格式良好也很痛苦。即使您使用它的对应部分,使用 echo_rprintvar_dump 仍然不如使用纯 HTML 干净。

由于 PHP 仅在包装标签 ?> 内运行,因此您应该充分利用这一点。

但是,由于我们不能只在这里执行此操作,因为您可能会丢失内容顺序,因此您可以使用一些对象存储。基本上在开始 HTML 块之前(可能在短代码函数的开头会很好),只需添加 ob_start(); 来存储您的代码。然后,在结束函数之前,您可以使用 $data = ob_get_clean();return $data; 返回数据。

祝您在更多 WordPress 工作中好运。

I'd like to contribute a new answer here.

I was working on extending a plugin and found this was how the author did it and to me it makes much more sense.

If you're outputting a lot of HTML you should not use echo, it's bad practise, hard to read and a pain to keep the formatting of the output nice using echo. Even if you use it's counter parts, it's still less clean to have echo_r or print or var_dump than to use pure HTML.

Since PHP only runs inside the wrapper tags <?php and ?>, you should use this to your advantage.

But since we can't just do this here because you may lose your content order, you can use some object storing. Basically before you start your HTML block (probably at the beginning of your shortcode function would be good), just add ob_start(); to store your code. Then just before ending your function, you can return your data using $data = ob_get_clean(); and return $data; .

Good luck with more WordPress work.

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