我在编写主题短代码时遇到了一些麻烦。我希望代码显示一个带有视图计数器功能的 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 '';
}
}