Find the Category ID using the Category Slug in WordPress

default_thumb This is a quick little piece of code that I have been using a lot lately. I needed to get the category ID but all I had to work with was the category slug. Luckily WordPress has a neat little function called get_term_by() which makes it super easy. The function can be used to get a lot more information but for this example I am just going to extract the ID.
The function works like this:

<?php get_term_by( $field, $value, $taxonomy, $output, $filter ) ?>


$field: (string) (required) Either ‘slug’, ‘name’, or ‘id’. Default: None


$value: (string|integer) (required) Search for this term value. Default: None


$taxonomy: (string) (required) Taxonomy Name. Default: None


$output: (string) (optional) Constant OBJECT, ARRAY_A, or ARRAY_N. Default: OBJECT


$filter: (string) (optional) default is raw or no WordPress defined filter will applied. Default: ‘raw’



We are going to use it like this to get the ID with the category slug ‘tutorials’:



<?php 
$theCatId = get_term_by( 'slug', 'tutorials', 'category' );
$theCatId = $theCatId->term_id;
?>


If you insert a print_r() function you can echo out all the information that the get_term_by() function contains. Use something like this and see if there is other information that you might want to extract:



<?php 
$theCatId = get_term_by( 'slug', 'tutorials', 'category' );
print_r($theCatId);
?>


Then all you would have to do is use whichever term your see between those square brackets like so:



<?php 
$theCatId = get_term_by( 'slug', 'tutorials', 'category' );
$theCatId = $theCatId->description;
?>

Get category link

<?php
// Get the ID of a given category
$category_id = get_cat_ID( 'Category Name' );

// Get the URL of this category
$category_link = get_category_link( $category_id );
?>

<!-- Print a link to this category -->
<a href="<?php echo $category_link; ?>" title="Category Name">Category Name</a>

Get category ID using category name

default_thumb When coding a WordPress site, you often need to get a category ID. If you ever wanted to be able to get a category ID from the category name, then just read this recipe, I’m pretty sure you will like it.

As usual, let's start by pasting the function in your functions.php file:

function get_category_id($cat_name){
$term = get_term_by('name', $cat_name, 'category');
return $term->term_id;
}


Once you saved the file, just call the function with your category name as a parameter. Example:



$category_ID = get_category_id('WordPress Tutorials');


You could also use



$category_ID = get_cat_id(‘WordPress Tutorials’);


Works the same and doesn’t require a custom function in functions.php. Here’s the function reference : http://codex.wordpress.org/Function_Reference/get_cat_ID

Get current category name and / or ID

<?php 
//AKTUELLE Post-KATEGORIE-ID ermitteln
foreach((get_the_category()) as $category)
{
$postcat= $category->cat_ID;
$catname =$category->cat_name;
//echo $postcat;
}
?>

Build Select Boxes from arrays

The buildSelect() function takes two arguments (one for the list, and one for options) and builds a selectbox for your form. The first array is simply the list you want to display. There are several examples within the script, so syntax shouldn't be a hassle. The second array is simply an options list. The first two elements are for adding an optional 'Title' to the beginning of the list, and whether is's selected by default. The third, and any subsequent elements of the options array, for that matter, choose which elements of the list are selected ( more than one can be applied to multiple selectboxes) on display

<html>
<head>
<title>PHP select box constructor demo</title>
</head>
<? // build arrays for select boxes
$one = array(
"1" => "Choice one",
"2" => "Choice two",
"3" => "Choice three"
);

$two = array(
"one" => "#1",
"two" => "#2",
"three" => "#3"
);

$three = array(
"T" => "Top",
"M" => "Middle",
"B" => "Bottom"
);

$four = array (
"grla" => "Gorilla",
"ostr" => "Ostritch",
"pthn" => "Python",
"shrk" => "Shark",
"spdr" => "Spider"
);

function buildSelect($selArr, $selOpt){
$selectText = $selOpt[0];
if ($selectText != "") {
$selDefault = "selected";
if ($selOpt[1] == "true") {
$selDefault = "";
}
echo "<option $selDefault>$selectText</option>n";
}
foreach($selArr as $key => $value){
$sel = "";
for ($loop = 2; $loop < count($selOpt); $loop++) {
if ($selOpt[$loop] == $key) {
$sel = "selected";
}
}
echo "<option $sel value='$key'>$value</option>n";
}
return;
}
?>
<body>
<form method="POST" action="">
<table bordercolor="#000000" cellspacing="1" cellpadding="2" border="1">
<tr>
<td class="left">Select Box #1:</td>
<td class="right">
<select name="one">
<? buildSelect($one, array("Select an option", "true", "")) ?>
</select>
</td>
</tr>
<tr>
<td class="left">Select Box #2:</td>
<td class="right">
<select name="two">
<? buildSelect($two, array("Pick one", "false", "")) ?>
</select>
</td>
</tr>
<tr>
<td class="left">Select Box #3:</td>
<td class="right">
<select name="three">
<? buildSelect($three, array("", "", "M")) ?>
</select>
</td>
</tr>
<tr>
<td class="left">Select Box #4:</td>
<td class="right">
<select name="four" multiple size="<?echo count($four) - 2 ?>">
<?
$options = array( "", "", "grla", "pthn");
buildSelect($four, $options)
?>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
Labels:
2010 WEBSITE20. All rights reserved.