{"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

$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

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

<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

The function get_body_class()<\/em> basically turns that into an array, which looks like this:<\/p>\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

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

Can you use is_category() or is_tax()?<\/h2>\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

if (is_tax('job_listing_category')) {\n  \/\/ code to run\n}<\/code><\/pre>\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

if (is_category(array('jquery', 'php')) {\n  \/\/ code to run\n}<\/code><\/pre>\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":"\nGet The Current Archive Category Type In WordPress<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get The Current Archive Category Type In WordPress\" \/>\n<meta property=\"og:description\" content=\"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\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/\" \/>\n<meta property=\"og:site_name\" content=\"Intelliwolf\" \/>\n<meta property=\"article:published_time\" content=\"2019-01-24T04:28:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-01-24T04:29:05+00:00\" \/>\n<meta name=\"author\" content=\"Mike Haydon\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mike Haydon\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/\"},\"author\":{\"name\":\"Mike Haydon\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343\"},\"headline\":\"Get The Current Archive Category Type In WordPress\",\"datePublished\":\"2019-01-24T04:28:24+00:00\",\"dateModified\":\"2019-01-24T04:29:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/\"},\"wordCount\":368,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\"},\"articleSection\":[\"General\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/\",\"url\":\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/\",\"name\":\"Get The Current Archive Category Type In WordPress\",\"isPartOf\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#website\"},\"datePublished\":\"2019-01-24T04:28:24+00:00\",\"dateModified\":\"2019-01-24T04:29:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.intelliwolf.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"General\",\"item\":\"https:\/\/www.intelliwolf.com\/category\/general\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Get The Current Archive Category Type In WordPress\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.intelliwolf.com\/#website\",\"url\":\"https:\/\/www.intelliwolf.com\/\",\"name\":\"Intelliwolf\",\"description\":\"WordPress, Web Design and Coding Tutorials\",\"publisher\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.intelliwolf.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.intelliwolf.com\/#organization\",\"name\":\"Intelliwolf\",\"url\":\"https:\/\/www.intelliwolf.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png\",\"contentUrl\":\"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png\",\"width\":300,\"height\":100,\"caption\":\"Intelliwolf\"},\"image\":{\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343\",\"name\":\"Mike Haydon\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.intelliwolf.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g\",\"caption\":\"Mike Haydon\"},\"sameAs\":[\"https:\/\/intelliwolf.com\/about-mike-haydon\/\"]}]}<\/script>\n","yoast_head_json":{"title":"Get The Current Archive Category Type In WordPress","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/","og_locale":"en_US","og_type":"article","og_title":"Get The Current Archive Category Type In WordPress","og_description":"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","og_url":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/","og_site_name":"Intelliwolf","article_published_time":"2019-01-24T04:28:24+00:00","article_modified_time":"2019-01-24T04:29:05+00:00","author":"Mike Haydon","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Mike Haydon","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/#article","isPartOf":{"@id":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/"},"author":{"name":"Mike Haydon","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343"},"headline":"Get The Current Archive Category Type In WordPress","datePublished":"2019-01-24T04:28:24+00:00","dateModified":"2019-01-24T04:29:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/"},"wordCount":368,"commentCount":0,"publisher":{"@id":"https:\/\/www.intelliwolf.com\/#organization"},"articleSection":["General"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/","url":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/","name":"Get The Current Archive Category Type In WordPress","isPartOf":{"@id":"https:\/\/www.intelliwolf.com\/#website"},"datePublished":"2019-01-24T04:28:24+00:00","dateModified":"2019-01-24T04:29:05+00:00","breadcrumb":{"@id":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.intelliwolf.com\/get-current-archive-category-type-in-wordpress\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.intelliwolf.com\/"},{"@type":"ListItem","position":2,"name":"General","item":"https:\/\/www.intelliwolf.com\/category\/general\/"},{"@type":"ListItem","position":3,"name":"Get The Current Archive Category Type In WordPress"}]},{"@type":"WebSite","@id":"https:\/\/www.intelliwolf.com\/#website","url":"https:\/\/www.intelliwolf.com\/","name":"Intelliwolf","description":"WordPress, Web Design and Coding Tutorials","publisher":{"@id":"https:\/\/www.intelliwolf.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.intelliwolf.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.intelliwolf.com\/#organization","name":"Intelliwolf","url":"https:\/\/www.intelliwolf.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/","url":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png","contentUrl":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-content\/uploads\/intelliwolf-logo-300t.png","width":300,"height":100,"caption":"Intelliwolf"},"image":{"@id":"https:\/\/www.intelliwolf.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/7209e3ff14822e4d70d5f194a310f343","name":"Mike Haydon","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.intelliwolf.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d5f4754fae310a04dede91d15e57c8a0?s=96&d=mm&r=g","caption":"Mike Haydon"},"sameAs":["https:\/\/intelliwolf.com\/about-mike-haydon\/"]}]}},"_links":{"self":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts\/203"}],"collection":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":0,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress-757293-2559390.cloudwaysapps.com\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}