【问题标题】:Get custom category name or id on archive.php page Wordpress在 archive.php 页面 Wordpress 上获取自定义类别名称或 ID
【发布时间】:2013-08-28 16:19:00
【问题描述】:

如何在archive.php 页面上“获取”自定义类别名称或ID。那么当我在那个页面模板上时,我怎么知道显示了哪些自定义类别的帖子?

【问题讨论】:

  • 对于“自定义类别名称”,您是指添加到自定义分类的术语还是添加到标准类别的术语?
  • 我的意思是自定义分类术语

标签: wordpress categories archive


【解决方案1】:

检查此代码是否有帮助??

if ( is_single() ) {
$cats =  get_the_category();
$cat = $cats[0]; // let's just assume the post has one category
}
else { // category archives
$cat = get_category( get_query_var( 'cat' ) );
}
$cat_id = $cat->cat_ID;
$cat_name = $cat->name;
$cat_slug = $cat->slug;

【讨论】:

    【解决方案2】:

    使用get_queried_object(); 检索当前查询的对象。

    在分类术语的情况下:

    //Custom taxonomy is project_type, custom term is web-design
    $obj = get_queried_object();
    
    echo '<pre>';
    print_r( $obj );
    echo '</pre>';
    

    显示以下内容:

    stdClass Object
    (
        [term_id] => 56
        [name] => Web Design
        [slug] => web-design
        [term_group] => 0
        [term_taxonomy_id] => 56
        [taxonomy] => project_type
        [description] => Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        [parent] => 0
        [count] => 0
    )
    

    希望对你有帮助!

    【讨论】:

      【解决方案3】:

      试试这段代码获取类别名称!! $cat_name = single_cat_title('',false);

      【讨论】:

        【解决方案4】:

        如果您尝试在您的archive.php 页面上显示当前类别,例如,如果您的网址是:

        www.sitename.com/category/design
        

        然后,要回显“设计”,您将使用以下内容:

        single_cat_title();
        

        在我的存档页面上,我有以下内容:

        <h1 class="page-heading"><?php single_cat_title(); ?></h1>
        

        在 h1 标记中显示类别“设计”。我希望这对某人有所帮助。

        【讨论】:

          【解决方案5】:

          简单的就是最好的

          $term_id       =    get_queried_object_id();
          $prod_term        =    get_term($term_id,'product_cat');
          $current_cat_slug =    $prod_term->slug;    
          

          【讨论】:

            猜你喜欢
            • 2020-11-22
            • 1970-01-01
            • 1970-01-01
            • 2015-11-09
            • 1970-01-01
            • 2018-06-16
            • 2013-04-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多