在新标签页中打开

获取当前分类名称或别名和ID

获取当前分类名称、别名、id

其实在设计主题时经常会需要调用当前分类或当前文章所属分类名称的需求,一般大家都是使用:

<?php the_category(); ?>

来调用分类名称与连接的,但是这样并不能单独的调用出分类的名称,那么怎样单独的调用出wordpress的分类名称呢?方法如下:

分类页面调用当前分类名称

single_cat_title可获取当前分类的标题,可自定义输出样式。

<?php single_cat_title(); ?>

文章页面调取当前分类名称

如果是在在文章页想调用出当前文章所属分类的名称,可以使用get_the_category函数。

代码1

<?php
foreach((get_the_category()) as $category)
{
echo $category->cat_name;
}
?>

代码2

<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>

代码3

<?php $thiscat = get_category($cat); echo $thiscat ->name;?>

获取当前分类id

get_the_category用于获取当前分类信息,包含分类ID、分类名称、分类别名、分类描述、父分类ID、分类下文章数量等。

<?php
	// 取出当前分类 id: $categories[0]->term_id
	$categories = get_the_category();
	$term_id = $categories[0]->term_id;
	$cat_name = $categories[0]->name;
?>

当有子栏目,栏目页使用(上面未测试当有子栏目,内容页情况)

<?php
global $wp_query;
$cat_ID = get_query_var('cat');
?>

获取当前自定义分类id

代码1

自定义分类不能使用get_the_category,要使用get_the_terms,代码如下:

<?php
	// 取出当前分类 id: $categories[0]->term_id
	$categories = get_the_terms( $post->ID, 'products' );
	$term_id = $categories[0]->term_id;
	$cat_name = $categories[0]->name;
?>

代码2

输出自定义分类名称(带连接),也可以使用the_terms

<?php
the_terms( $post->ID, 'products' );
?>

获取当前页面的父类ID或者是页面最顶部根目录的ID

最顶级分类页面

  • 一级页面
    • 二级页面-获取父类页面 (我们需要获取的父类ID)
      • 当前页面

1、获取父类ID
在function.php中加入以下代码

function is_subpage() {
    global $post;
    if ( is_page() && $post->post_parent ) {
        return $post->post_parent;
    } else {
        return false;
    }
}

调用方法:$parent_id=is_subpage();  获取父类ID

2、获取当前页面最顶级分类ID
在想要获取的页面内加入以下代码:

$post_id = $post->ID; //获取当前页面ID
$ancestors = get_post_ancestors($post_id);
$root_id = (!emptyempty($ancestors) ? array_pop($ancestors): $post_id);

$root_id 就是当前页面最顶部分类的ID

WordPress 获取当前页面 ID 的几大方法

方法一:

//文章或页面的 ID 值,如果未在循环中输出值可能不准确
$postid = get_the_ID();
echo $postid;

方法二:

//检索当前查询对象的 ID
$current_id = get_queried_object_id();
echo $current_id;

方法三:

// 检索当前查询的对象,从对象中获取 ID
$object = get_queried_object();
$id = $object -> ID;
echo $id;

方法四:

// 通过$post 全局变量获取文章或页面 ID
global $post;
$id = $post -> ID;
echo $id;

补充内容:

// 第一种获取父级页面的 ID
global $post;
$id = $post -> ID;
$parent = get_post_ancestors($post -> ID);
print_r($parent);//打印出 Array ( [0] => 101 )
// 第二种获取父级页面的 ID
global $post;
$parent_id = $post -> post_parent;
echo $parent_id;//打印出父级页面的 ID

其实究竟要怎么去获取还是需要根据实际的开发情况而确定,文章或页面或者循环中可以使用 get_the_ID 函数直接获取,如果需要某些特殊或者 get_the_ID 获取不正确的时候,使用 get_queried_object_id 函数,get_queried_object_id可以对自定义分类生效。

$term_id = get_queried_object_id( 'product' )

注释:

get_queried_object() : 当前页面对象下所有的属性
 {
["ID"]=> int(339)
["post_author"]=> string(1) "1"
["post_date"]=> string(19) "2017-08-04 15:13:09"
["post_date_gmt"]=> string(19) "2017-08-04 07:13:09"
["post_content"]=> string(3040) "三一重工股份有限公司由三一集团投资创建于1994年”。"
["post_title"]=> string(12) "三一介绍"
["post_excerpt"]=> string(0) ""
["post_status"]=> string(7) "publish"
["comment_status"]=> string(6) "closed"
["ping_status"]=> string(6) "closed"
["post_password"]=> string(0) ""
["post_name"]=> string(36) "三一介绍"
["to_ping"]=> string(0) ""
["pinged"]=> string(0) ""
["post_modified"]=> string(19) "2017-08-04 16:42:44"
["post_modified_gmt"]=> string(19) "2017-08-04 08:42:44"
["post_content_filtered"]=> string(0) ""
["post_parent"]=> int(0) //获取父级的ID -> 很重要,经常用
["guid"]=> string(39) "http://localhost/wordpress/?page_id=339"
["menu_order"]=> int(0) ["post_type"]=> string(4) "page"
["post_mime_type"]=> string(0) ""
["comment_count"]=> string(1) "0"
["filter"]=> string(3) "raw"
 }

获取当前文章别名、分类别名的函数

<?php
/*
* 获取当前文章或页面别名的函数
*/
function the_slug() {
    $post_data = get_post($post->ID, ARRAY_A);
    $slug = $post_data['post_name'];
    return $slug;
}
/*
* 获取当前文章所属第一个分类别名的函数
*/
function the_category_slug(){
 $category = get_the_category();
 return ($category ? $category[0]->slug : "");
}
?>

使用

<?php
// 输出文章别名
echo the_slug();
// 输出文章分类的别名
echo the_category_slug();
?>

 

作者: 丝湃秀科技