列出类别时如何将类别的帖子计数包含到链接项目?

发布于 2024-08-24 15:23:19 字数 333 浏览 3 评论 0原文

列出类别时,如何将类别下的帖子数包含到 a 标签中。这曾多次困扰我,但现在我决定找出答案。

<li><a href="#" title="asd">php</a> (1)</li>

<li><a href="#" title="asd">php (1)</a></li>

是否正确地假设,我正在使用 wp_list_categories 来制作此列表。

有什么解决办法吗?

马蒂·莱恩

How could I include the count of posts under a category into the a-tag when listing categories. This has been a problem for me many times but now I decided to find out.

<li><a href="#" title="asd">php</a> (1)</li>

to

<li><a href="#" title="asd">php (1)</a></li>

Is you propably assumed, I'm using wp_list_categories to make this list.

Any solutions?

Martti Laine

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄司 2024-08-31 15:23:19

我解决了。在这里发布以便有人可以使用它:)

<?php
$data = wp_list_categories('show_count=1&echo=0');
$data = preg_replace('/\<\/a\> \((.*)\)/',' ($1)</a>',$data);
echo $data;
?>

I solved it. Posting here so someone can use it :)

<?php
$data = wp_list_categories('show_count=1&echo=0');
$data = preg_replace('/\<\/a\> \((.*)\)/',' ($1)</a>',$data);
echo $data;
?>
悲念泪 2024-08-31 15:23:19

我最近也遇到了这个问题。我读过的其他论坛也建议使用正则表达式,就我个人而言,该选项似乎容易出现缺陷。

我的建议是:

$cat_args = array(
    'orderby' => 'count',
    'order' => 'DESC'
);

$categories = get_categories( $cat_args );

if ( count($categories) ) {

    echo '<ul>';

    foreach ( $categories as $category ) {
        echo '<li><a href="'.get_category_link( $category->term_id ).'">'.$category->name.' ('.$category->count.')</a></li>';
    }

    echo '</ul>';

}

如果您愿意,它还可以让您选择以括号以外的格式设置数字格式。

I recently had this problem as well. Other forums I read suggested regex too and personally that option seemed prone to flaw.

My recommendation is this:

$cat_args = array(
    'orderby' => 'count',
    'order' => 'DESC'
);

$categories = get_categories( $cat_args );

if ( count($categories) ) {

    echo '<ul>';

    foreach ( $categories as $category ) {
        echo '<li><a href="'.get_category_link( $category->term_id ).'">'.$category->name.' ('.$category->count.')</a></li>';
    }

    echo '</ul>';

}

It also gives you the option to format the number in something other than parens if you wish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文