获取指定或当前分类目录的文章数量总数这个功能,在开发 WordPress主题中经常会用到。
这个常用的功能也是非常容易实现的,下面分享几种实现方法
首获取当前文章数量
<?php
global $wp_query;
$cat_ID = get_query_var(\'cat\');
$category = get_category($cat_ID);
echo $category->count;
?>
使用方法
在需要获取当前文章数量的页面使用上面代码
//以上代码中,最后的count,也可以换成slug,就是输出分类别名。
echo $category->slug;
解析:
get_cat_ID() 根据分类名称获取分类ID
get_cat_name() 根据分类ID获取分类名称
get_category_link() 根据分类ID获取分类的链接URL
获取某个分类的文章数量
1、使用 WordPress的内置函数 get_posts()
<?php
$posts = get_posts( \'numberposts=-1&category=3\' );
echo count($posts);
?>
2、使用WordPress的内置函数 get_category_by_slug()
<?php
// 将下面\'category-name\'改成你的分类别名即可
echo get_category_by_slug(\'category-name\')->count;
?>
3、 使用 WordPress的内置函数 get_category()
<?php
// 将以下\'cat_ID\'改成你的分类ID即可
echo get_category(cat_ID)->count;
?>
4、 使用 wpdb 直接查询数据库
function wt_get_category_count($input = \'\') {
global $wpdb;
if($input == \'\') {
$category = get_the_category();
return $category[0]->category_count;
}
elseif(is_numeric($input)) {
$SQL = \"SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$input\";
return $wpdb->get_var($SQL);
}
else {
$SQL = \"SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug=\'$input\'\";
return $wpdb->get_var($SQL);
}
}
第4种方法的使用方法: 将上面代码添加在主题目录下的functions.php中, 然后在需要的地方调用下面三种函数:
// 1.在主循环中调出该函数,不提供参数,返回第一个分类的文章数
<?php echo wt_get_category_count(); ?>
// 2.提供数字参数,该数字为分类的 ID 号,则返回对应 ID 的分类的文章数:
// \'1\'为你的分类ID
<?php echo wt_get_category_count(1); ?>
// 3.提供分类别名做为参数,返回对应别名的分类文章数:
// \'hello-world\'为你的分类别名
<?php echo wt_get_category_count(\'hello-world\'); ?>
总结
个人建议使用WordPress内置函数get_category()获取某个分类的文章数量,也就是第3种方案。
1、本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
6、联系微信:A2022717!
袁博客 » WordPress获取指定/当前分类目录的文章数量
2、分享目的仅供大家学习和交流,请不要用于商业用途!
3、本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
4、如有链接无法下载、失效或广告,请联系管理员处理!
5、本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
6、联系微信:A2022717!
袁博客 » WordPress获取指定/当前分类目录的文章数量
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?
buy viagra generic order viagra for sale order generic cialis
sildenafil price lamotrigine 200mg pills order lamictal 200mg generic
pay for assignments cheap dissertation help ivermectin 6 mg
buy desyrel 50mg generic buy desyrel 100mg sale aurogra pills
us blackjack online free spins cialis 10mg cheap
buy cefuroxime 500mg without prescription buy ceftin 500mg online cheap buy robaxin online cheap
order viagra 50mg online cheap usa cialis overnight buy tadalafil 10mg pills
keflex 125mg drug buy erythromycin 500mg online buy generic erythromycin 500mg
fluconazole for sale online canadian viagra oral viagra
flagyl 400mg uk order bactrim 480mg without prescription bactrim 480mg pills
purchase zocor for sale order valtrex 500mg online buy finasteride 5mg for sale
buy cialis 10mg online cheap order ciprofloxacin 1000mg online cheap ciprofloxacin for sale online
order flomax without prescription spironolactone 25mg brand spironolactone 25mg cost
casino online games golden nugget online casino casinos online
zantac 300mg generic buy generic ranitidine 300mg celebrex 100mg canada
colchicine pill gloperba canada cash poker online
order sumatriptan 50mg sale buy generic levaquin 250mg avodart uk
baclofen 10mg usa purchase tizanidine for sale toradol 10mg sale
order losartan 25mg online cheap topiramate 200mg brand topiramate 100mg drug
motilium 10mg tablet cheap sumycin 500mg cyclobenzaprine uk
methotrexate online order methotrexate 5mg over the counter reglan 10mg cost
allopurinol generic zetia price buy ezetimibe online
order cialis 40mg generic clopidogrel pill order clopidogrel 150mg generic
buy generic orlistat 120mg diltiazem 180mg ca order acyclovir without prescription
order cialis 20mg sale sildenafil 100mg pills sildenafil australia
order dapoxetine 90mg online cheap synthroid pill order generic synthroid 75mcg
cialis online canada sildenafil cost viagra ca
order clomiphene 50mg for sale brand clomid 100mg online casinos
academic writing article academic writing support sildenafil 150mg price
buy custom research papers free slot machine games slots games free
prilosec 20mg for sale buy generic prilosec 20mg play online casino real money
buy zyvox generic online blackjack live dealer live blackjack
sildenafil next day delivery order viagra 50mg pills order lisinopril 10mg pills
clozapine usa order clozaril 100mg order dexamethasone for sale
order zyprexa 10mg generic order valsartan 80mg pill buy valsartan 160mg generic
order glucophage online buy glycomet sale cheap tadalafil without prescription
itraconazole us sporanox without prescription tinidazole tablet
brand aralen olumiant 2mg canada buy baricitinib 2mg
buy furosemide 40mg sale order plaquenil hydroxychloroquine 200mg generic
cialis overnight delivery brand viagra pills viagra brand
zithromax 500mg price azithromycin cheap order gabapentin 100mg pills
order generic nootropil 800 mg piracetam 800mg usa cheap sildenafil
buy generic isotretinoin 40mg buy amoxicillin 500mg pill deltasone tablet
order fluvoxamine generic fluvoxamine online buy glucotrol 5mg oral
modafinil without prescription order stromectol 12mg online cheap stromectol 6 mg dosage
order medroxyprogesterone 5mg sale purchase medroxyprogesterone without prescription cheap cyproheptadine
buy dapsone adalat medication buy perindopril generic
tadalafil 10mg price cialis 20mg canada symmetrel pills
cialis 20mg oral Buy cialis in us real viagra pills
imuran over the counter purchase imuran generic sildenafil overnight shipping
order sertraline pill lexapro 20mg without prescription viagra professional
buy imdur 20mg pills order azathioprine 25mg sale buy telmisartan 80mg pills
prograf 5mg ca ropinirole 1mg without prescription order ursodiol 150mg sale
olmesartan 10mg pill order diamox sale acetazolamide 250mg us
alendronate tablet order pepcid 40mg buy famotidine pill
purchase terazosin online buy hytrin sale buy sulfasalazine online
purchase buspirone buy buspirone pill oral oxybutynin 2.5mg
order catapres generic oral tiotropium bromide 9mcg spiriva 9mcg sale
ramipril 5mg for sale order astelin online cheap azelastine 10ml tablet
buy modafinil 200mg generic diamox 250mg oral acetazolamide 250 mg without prescription
order generic accutane 20mg cheap isotretinoin 20mg ivermectin online pharmacy
tadalafil 20mg ohne rezept cialis 20mg kaufen ohne rezept original viagra 50mg rezeptfrei sicher kaufen
prednisone 10mg oral sildenafil australia usa viagra overnight
cialis 10mg sans ordonnance en france pharmacie en ligne cialis pharmacie en ligne sildenafil 100mg
buy anastrozole 1mg online buy generic biaxin 500mg buy viagra 50mg sale
order cialis cialis cheap price sildenafil oral
indomethacin oral terbinafine 250mg cheap purchase trimox online cheap
accutane pills brand azithromycin 500mg buy azithromycin for sale
order tadalafil 10mg pill diclofenac 100mg for sale diclofenac 50mg canada
lamictal cost lamictal 200mg pill retin gel over the counter
buy suhagra for sale viagra sildenafil 50mg estrace oral
buy fildena 100mg for sale oral nolvadex 20mg methocarbamol usa
buy acillin without prescription order ampicillin 250mg erythromycin cheap
order spironolactone 25mg generic buy valtrex 1000mg pills fluconazole 100mg price
avodart drug ondansetron sale buy zofran sale
levofloxacin 250mg tablet levofloxacin 500mg drug