错误的帖子视图格式在wordpress中计算

时间:2016-01-11 17:14:47

标签: php wordpress return echo

我打印一条由发布日期和观看次数组成的行(使用插件wp-PostViews)。但是,我得到26 [icon] 2016-01-09 [icon] views,而不是[icon] 2016-01-09 [icon] 26 views(错误的观看次数位置),如下所示:

enter image description here

以下是源代码:

$time_string_published = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
$time_string_published = sprintf( $time_string_published,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date( 'Y-m-d' ) )
);

printf( '<span class="posted-on">%1$s</span><span class="views-link">%2$s</span>',
                sprintf( '<a href="%1$s" rel="bookmark">%2$s</a>',
                        esc_url( get_permalink() ),
                        $time_string_published
                ),
                sprintf('%1$s views', the_views())
        );

相关的css设置为:

.entry-meta .views-link:before {
        display: inline-block;
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
        font: normal 22px/1 'Genericons';
        vertical-align: top;
}
.entry-meta .views-link:before { content: '\f403'; }

顺便说一句,以下代码正常工作,显示为[icon] 26(正确的位置)。

<span class="views-link">
      <?php if(function_exists('the_views')) { echo the_views(); } ?>
</span>

是否可以在排除上述代码后加载插件wp-PostViews(包含函数the_views())?

2 个答案:

答案 0 :(得分:1)

我不确定其他人 - 但这似乎有点像试图这样做有点过分。为什么不尝试:

$url = esc_url( get_permalink() );
$time = $time_string_published;
$views = the_views();

$out = <<<EOD
<span class="posted-on"><a href="$url"
rel="bookmark">$time</a></span><span class="views-link">$views</span>
EOD;

printf( "%s", $out );

以上做同样的事情,但更清楚一点。抱歉!以为我有它,但我最初弄错了。我现在已经纠正了生成的代码。所以我现在说 - 你检查了$ time_string_published给你的东西吗?

BTW:如果你对the_views()的回应给你“ico 26 views”,为什么不这样做呢:

$views = substr( the_views(), 4 );

获得“26个观点”部分。然后它会自动将“视图”粘在它的末尾。只是一个想法。

更新:晚上8:57

这是重新编写的代码。它对我有用。

<?php

$time_string_published = '<time class="entry-date published" datetime="%1$s">%2$s</time>';
$time_string_published = sprintf( $time_string_published,
        date( 'c', time() ),
        date( 'Y-m-d', time() )
);

printf( '<span class="posted-on">%1$s</span><span class="views-link">%2$s</span>',
                sprintf( '<a href="%1$s" rel="bookmark">%2$s</a>',
                        ( get_permalink() ),
                        $time_string_published
                ),
                sprintf('%1$s views', the_views())
        );

$date1 = date('c', time() );
$date2 = date('Y-m-d', time() );
$time_string_published = "<time class='entry-date published' datetime='$date1'>$date2</time>";

$url = get_permalink();
$view = the_views();

$s = "<a href='$url' rel='bookmark'>$time_string_published</a>";
echo "<p><span class='posted-on'>$s</span> <span class='views-link'>$view views</span>";

function get_permalink()
{
    return "http://www.google.com";
}
function the_views()
{
    return 26;
}

?>

输出结果为:

2016-01-1226 views

2016-01-12 26 views

页面源代码是:

<span class="posted-on"><a href="http://www.google.com" rel="bookmark"><time class="entry-date published" datetime="2016-01-12T02:55:41+00:00">2016-01-12</time></a></span><span class="views-link">26 views</span><p><span class='posted-on'><a href='http://www.google.com' rel='bookmark'><time class='entry-date published' datetime='2016-01-12T02:55:41+00:00'>2016-01-12</time></a></span> <span class='views-link'>26 views</span>

请注意,第二行在第一个SPAN和第二个SPAN之间有一个空格。如果你没有得到相同的输出,那么WordPress或整个程序的其他部分都会发生一些事情。关于我能提供的所有帮助。 : - )

答案 1 :(得分:0)

最后,我跟踪了它。原因是函数 [ on "input" targetValue (Signal.message address << SendRequest) ] 直接打印 ,而是返回一个变量。这是源代码:

request

要解决此问题,将参数the_views()传递给### Function: Display The Post Views function the_views($display = true, $prefix = '', $postfix = '', $always = false) { $post_views = intval( get_post_meta( get_the_ID(), 'views', true ) ); $views_options = get_option('views_options'); if ($always || should_views_be_displayed($views_options)) { $output = $prefix.str_replace( array( '%VIEW_COUNT%', '%VIEW_COUNT_ROUNDED%' ), array( number_format_i18n( $post_views ), postviews_round_number( $post_views) ), stripslashes( $views_options['template'] ) ).$postfix; if($display) { echo apply_filters('the_views', $output); } else { return apply_filters('the_views', $output); } } elseif (!$display) { return ''; } } 以作为变量返回,即false

the_views