显示wordpress帖子的数量

时间:2014-11-27 22:11:03

标签: php wordpress

我正在尝试以类似自动收报机的方式显示wordpress帖子的数量,但这些数字并未显示。我的代码是对的吗? 在functions.php中,我写道:

<?php
$totalposts = wp_count_posts();
$postsepnumbers = array_pad(str_split($totalposts), -3, "");

因为我想要显示三位数字。我没有结束?&gt;消除空白问题。

然后我想将它们显示为这里的代码:this site 所以在我的front-page.php中我添加了

<div class=counterwrapper"><div class="countbox"><center>We currently have <span><?php echo $postsepnumbers[0]; ?></span><span><?php echo $postsepnumbers[1]; ?></span><span><?php echo $postsepnumbers[2]; ?></span> things to do listed here.  Add your ideas!!</center></div></div>

而css是

.countbox {
color: #CCCCCC;
background: linear-gradient(to bottom, #3d3d3d 0%,#4c4c4c 25%,#1c1c1c 52%,#161616 52%,#2b2b2b 76%,#131313 100%);
border-radius: 4px;
font-weight: bold;
font-family: "proxima-nova", sans-serif;
display: inline-block;
border: 1px solid #181818;
height: 60px;
}
.countbox span {
display: inline-block;
border-right: 2px solid #111;
padding: 4px 2px;
font-size: 1.8em;
width: 30px;
text-align: center;
line-height: 1em;
text-shadow: 0 -1px 1px #000;
color: #fff;
}

我没有为“反包装”添加任何CSS,因为我想让它先工作。但是如果你看到这里:site没有数字显示。

我不明白为什么我不能让它显示数字。

1 个答案:

答案 0 :(得分:1)

函数wp_count_post()返回一个objet,而不是一个数组。这是你的代码不工作的唯一原因。您可以在此处找到如何使用它的示例:http://codex.wordpress.org/Function_Reference/wp_count_posts

可能你可以尝试:

<?php
$totalposts = wp_count_posts();
$postsepnumbers = array_pad(str_split($totalposts->publish), -3, "");

那应该显示已发布帖子的数量。