2021年2月20日

How to get id, name and slug on wordpress

on page

page.php

Get category slug by ID
$category = get_category( $category_ID );
echo $category->slug;
Get category ID by slug
$idObj = get_category_by_slug( $cateslug );
$categoryID = $idObj->term_id;
Get tag name by ID
echo get_term_by('id' ,ここにタグID,  'post_tag')->name;
Get tag name by slug
$tag = get_term_by('slug', ここにタグスラッグ, 'post_tag');
echo $tag->name;

on category

category.php

Get category ID
$getcategory = get_category( get_query_var( 'cat' ) );
$cateid = $getcategory->cat_ID;
Get parent category ID
$cateid = get_query_var( 'cat' );//現在のカテゴリID
$child = get_category($cateid);
$parentID = $child->parent;//親カテゴリのID
Get category name
echo get_queried_object()->name;
single_cat_title('' , true );

on tag

tag.php

Get tag id
$tag_id = get_query_var('tag_id');

Anywhere

Get post id by url
$postid = url_to_postid( $url );
Wish I could be Comic Sans ついにマイコンピューターでインターネットを始めたの
わたしはヨガマットデザイナー

fzfeed