最新公告
  • 欢迎您光临知事网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入我们
  • WordPress函数get_cat_ID()用法 通过分类名获取分类ID

    正文概述 知事网   2020-09-11   32

    描述:

    通过分类名获取分类ID

    用法:

    <?php get_cat_ID( $cat_name ) ?>

    参数:

    $cat_name
    
    (string) (可选) 默认值为“General”,可以是任何类别名称。
    
    默认值: 'General'

    示例:

    <?php
    $category_id = get_cat_ID('Category Name');
    <!-- Start the Loop. -->
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <!-- Test if the current post is in category "Category Name". -->
    <!-- If it is, the div box is given the CSS class "post-cat-special". -->
    <!-- Otherwise, the div box is given the CSS class "post". -->
    
    <?php if ( in_category($category_id) ) { ?>
    <div class="post-cat-special">
    <?php } else { ?>
    <div class="post">
    <?php } ?>
    </div>
    <!-- Stop The Loop (but note the "else:" - see next line). -->
    
    <?php endwhile; else: ?>
    
    <!-- The very first "if" tested to see if there were any Posts to -->
    <!-- display. This "else" part tells what do if there weren't any. -->
    <p>Sorry, no posts matched your criteria.</p>
    
    <!-- REALLY stop The Loop. -->
    <?php endif; ?>

    源文件:

    /**
    * Retrieve the ID of a category from its name.
    *
    * @since 1.0.0
    *
    * @param string $cat_name Category name.
    * @return int 0, if failure and ID of category on success.
    */
    function get_cat_ID( $cat_name ) {
    $cat = get_term_by( 'name', $cat_name, 'category' );
    if ( $cat )
    return $cat->term_id;
    return 0;
    }
    找互联网优质资源,上知事网!
    知事网 » WordPress函数get_cat_ID()用法 通过分类名获取分类ID

    发表评论

    发表评论

    • 313会员总数(位)
    • 389资源总数(个)
    • 20本周发布(个)
    • 0 今日发布(个)
    • 3390稳定运行(天)

    提供最优质的资源集合

    Mac软件 Dedecms源码
    升级SVIP尊享更多特权立即升级