dongweihuan8610 2011-02-07 06:43
浏览 70
已采纳

问题在Wordpress中返回短代码内的函数

Hey guys, I'm having 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 '';
    }
}
  • 写回答

3条回答 默认 最新

  • dri8163 2011-02-07 06:50
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源