Wordpress Code & Research


All things about wordpress

Get post meta

Post meta is a good feature of wordpress, when you use post you can attach post meta for each post.

You can get post meta value by many ways:
The code below will help you get value of this variable

<?php $meta_values = get_post_meta($post_id, $key, $single); ?>
<?php $key_1_values = get_post_meta(76, ‘key_1’); ?>
<?php $key_1_value = get_post_meta(ID, ‘key_1’, true); ?>


Filed under: BASIC CODE

Get the category in wordpress

I like wordpress very much, so I worked with it for many times.
Sometimes I cannot find my wordpress code which I stored them in my computer.
So I created the blog and saved my wordpress research and code here.

In the first post, I will post basic code when you work with category .

1- get_the_category:

You want to get the current category name, you can use it:

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

You want to get the current category id, the same is below:

$category = get_the_category();
echo $category[0]->cat_ID;

2- Get category by slug
Example we have slug with a variable: $pageslug

$pageslug= ‘news’;
$catslug = get_category_by_slug($pageslug); // get category of the same slug
$catid = $catslug->term_id; // get id of this category

3- Get the slug of current category with query var

<?php if(is_category())
$cat= get_query_var(‘cat’);
$yourcat= get_category ($cat);
echo ‘the slug is ‘.$yourcat—>slug;


Filed under: BASIC CODE