Notes:
<当前,您正在回送
wp_get_archives(). 为了返回,我们必须设置
echo 将参数输入到
false.
您假设wp_get_archives() 是一个数组,但它是一个字符串。
Workaround:
这里有一种方法,通过计算
<li> 实例,具有
html 格式:
$args = [
  \'parent\'     => 0,
  \'hide_empty\' => 0,
  \'echo\'       => 0,
  \'format\'     => \'html\',
];
$archive_count = substr_count( wp_get_archives( $args ), \'<li>\' );
 这里我们假设
<li> 未被修改
get_archives_link 滤器
Update:
下面是使用
get_archives_link 过滤以勾选计数器。这应该能够处理所有类型的
wp_get_archvies().
让我们创建一个wp_get_archives(), 同时获取输出和计数。
首先,我们创建MyArchive:
$myarchive = new MyArchive;
 然后我们用相关参数生成它:
$myarchive->generate( $args );
 要获取我们使用的计数:
$archive_count = $myarchive->getCount();
 我们通过以下方式获得输出:
$archive_html = $myarchive->getHtml();
 这里我们定义
MyArchive 包装器组件(演示):
class MyArchive
{
    private $count;
    private $html;
    public function getCount()
    {
        return (int) $this->count;
    }
    public function getHtml()
    {
        return $this->html;
    }
    public function generate( array $args )
    {
        $this->count = 0;
        // Make sure we return the output
        $args[\'echo\'] = false;  
        // Do the counting via filter
        add_filter( \'get_archives_link\',    [ $this, \'getArchivesLink\' ] );
        // Generate the archives and store it
        $this->html = wp_get_archives( $args );
        // Remove filter
        remove_filter( \'get_archives_link\', [ $this, \'getArchivesLink\' ] );
        return $this;
    }       
    public function getArchivesLink( $link )
    {
        $this->count++;
        return $link;
    }
}