{"id":203,"date":"2019-01-24T12:28:24","date_gmt":"2019-01-24T04:28:24","guid":{"rendered":"https:\/\/www.intelliwolf.com\/?p=203"},"modified":"2019-01-24T12:29:05","modified_gmt":"2019-01-24T04:29:05","slug":"get-current-archive-category-type-in-wordpress","status":"publish","type":"post","link":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/get-current-archive-category-type-in-wordpress\/","title":{"rendered":"Get The Current Archive Category Type In WordPress"},"content":{"rendered":"\n
The template I was using ran several, different, custom categories through the archive.php<\/em> template. I needed to hook in some code to just one type of category, but not the others. I needed some way to test the current archive category type, but all the hooks were too generic.<\/p>\n\n\n\n After much messing about, I figured out how\u00a0to\u00a0get\u00a0the\u00a0current\u00a0archive\u00a0category\u00a0type\u00a0in\u00a0WordPress<\/strong>. Use the get_body_class()<\/em> WordPress function, then test for your category using in_array()<\/em>.<\/p>\n\n\n\n Here's how that looks:<\/p>\n\n\n\n When you view the source code of a WordPress page, it usually has a number of classes in the body tag. In my example, it looked like this:<\/p>\n\n\n\n The function get_body_class()<\/em> basically turns that into an array, which looks like this:<\/p>\n\n\n\n You can't always count on the classes being in that order, so don't use the array keys to test. It's much better to use in_array()<\/em>.<\/p>\n\n\n\n The code I wanted to run applied only to tax-job_listing_category<\/em>, rather than tax-job_listing_region<\/em>, which ran on the same template and used the same WordPress do_action<\/em> hooks.<\/p>\n\n\n\n You often can use is_category()<\/em> or is_tax()<\/em>, if you know which you want to connect with.<\/p>\n\n\n\n In fact, while researching for this tutorial, I realised is_tax()<\/em> was even better suited to what I was trying to do.<\/p>\n\n\n\n I've needed to connect in with the body classes before, so I'm leaving this tutorial with the example I started with.<\/p>\n\n\n\n The code l ended up rolling out was:<\/p>\n\n\n\n Notice that to run the query, you have to drop the \"tax-\"<\/em> from the start.<\/p>\n\n\n\n The same would apply if you were running it on a category archive. You would use is_category('jquery')<\/em> if it was on a page that had category-jquery<\/em> in the body class.<\/p>\n\n\n\n If you had to test for several categories, you would put them in array like this:<\/p>\n\n\n\n Using get_body_class()<\/em> is particularly useful when you need to combine different queries.<\/p>\n\n\n\n There are many ways to arrive at your desired result. Hopefully this helps you get there.<\/p>\n","protected":false},"excerpt":{"rendered":" The template I was using ran several, different, custom categories through the archive.php template. I needed to hook in some code to just one type of category, but not the others. I needed some way to test the current archive category type, but all the hooks were too generic. After much messing about, I figured<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"yoast_head":"\n$body_classes = get_body_class();\nif (in_array('tax-job_listing_category', $body_classes)) {\n \/\/ code to run\n}<\/code><\/pre>\n\n\n\n
How does it work?<\/h2>\n\n\n\n
<body class=\"archive tax-job_listing_category term-short-term-rentals term-18 logged-in admin-bar no-customize-support wp-custom-logo post-image-below-header post-image-aligned-center sticky-menu-fade right-sidebar nav-float-right fluid-header separate-containers active-footer-widgets-0 header-aligned-left dropdown-hover\"><\/code><\/pre>\n\n\n\n
Array\n(\n [0] => archive\n [1] => tax-job_listing_category\n [2] => term-short-term-rentals\n [3] => term-18\n [4] => logged-in\n [5] => admin-bar\n [6] => no-customize-support\n [7] => wp-custom-logo\n [8] => post-image-below-header\n [9] => post-image-aligned-center\n [10] => sticky-menu-fade\n [11] => right-sidebar\n [12] => nav-float-right\n [13] => fluid-header\n [14] => separate-containers\n [15] => active-footer-widgets-0\n [16] => header-aligned-left\n [17] => dropdown-hover\n)<\/code><\/pre>\n\n\n\n
Can you use is_category() or is_tax()?<\/h2>\n\n\n\n
if (is_tax('job_listing_category')) {\n \/\/ code to run\n}<\/code><\/pre>\n\n\n\n
if (is_category(array('jquery', 'php')) {\n \/\/ code to run\n}<\/code><\/pre>\n\n\n\n