{"id":3468,"date":"2017-04-07T14:03:16","date_gmt":"2017-04-07T12:03:16","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=3468"},"modified":"2020-02-09T10:59:54","modified_gmt":"2020-02-09T09:59:54","slug":"adding-post-template-names-to-post_class","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/adding-post-template-names-to-post_class-3468\/","title":{"rendered":"Adding post template names to post_class()"},"content":{"rendered":"\n<p>For my currently work-in-progress-theme, I want to display <a href=\"https:\/\/make.wordpress.org\/core\/2016\/11\/03\/post-type-templates-in-4-7\/\">posts with post templates<\/a> different in the blog view. For that, I add the name of the post template to the <code class=\"lang-php\">post_class()<\/code> output via the <code class=\"lang-php\">post_class<\/code> filter. This post shows you how to get this working.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Function for returning the post template slug<\/h2>\n\n\n\n<p>Because I need the name of the post templates in various places of the theme, we write a function for returning the post template name first:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-keyword\">if<\/span> ( ! function_exists( <span class=\"hljs-string\">'photographia_get_post_type_template'<\/span> ) ) {\n\t<span class=\"hljs-comment\">\/**\n\t * Returns the post type template slug \n\t * without templates\/ dir and .php ending.\n\t *\n\t * <span class=\"hljs-doctag\">@return<\/span> string\n\t *\/<\/span>\n\t<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">photographia_get_post_type_template<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n\t\t$template_slug = get_page_template_slug();\n\t\t<span class=\"hljs-keyword\">if<\/span> ( <span class=\"hljs-string\">''<\/span> !== $template_slug ) {\n\t\t\t<span class=\"hljs-comment\">\/**\n\t\t\t * Remove \u00bbtemplates\/\u00ab from slug.\n\t\t\t *\/<\/span>\n\t\t\t$template_slug = str_replace( <span class=\"hljs-string\">'templates\/'<\/span>, <span class=\"hljs-string\">''<\/span>, $template_slug );\n\n\t\t\t<span class=\"hljs-comment\">\/**\n\t\t\t * Remove .php file ending.\n\t\t\t *\/<\/span>\n\t\t\t$post_type_template = str_replace( <span class=\"hljs-string\">'.php'<\/span>, <span class=\"hljs-string\">''<\/span>, $template_slug );\n\n\t\t\t<span class=\"hljs-keyword\">return<\/span> $post_type_template;\n\t\t} <span class=\"hljs-keyword\">else<\/span> {\n\t\t\t<span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-string\">''<\/span>;\n\t\t}\n\t}\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>At the first line, we check if the function <code class=\"lang-php\">photographia_get_post_type_template()<\/code> was previously defined by a child theme (or plugin). Inside the function, we determine the current\u2019s post template slug with <code class=\"lang-php\">get_page_template_slug()<\/code>. If the post has no template, the function call returns an empty string. If the result is not empty, the theme\u2019s template slugs look like that: <code class=\"lang-markup\">templates\/name-of-template.php<\/code>. We do not need the <code class=\"lang-markup\">templates\/<\/code> part and the <code class=\"lang-markup\">.php<\/code> ending \u2014 let us remove them with two <code class=\"lang-php\">str_replace()<\/code> calls and return the <code class=\"lang-php\">$post_type<\/code>.<\/p>\n\n\n\n<p>If the post has no template, we return an empty string.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Function for adding the class<\/h2>\n\n\n\n<p>Now the function, which filters the <code class=\"lang-php\">post_class<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/**\n * Add classes to post_class()\n *\n * <span class=\"hljs-doctag\">@param<\/span> array $classes array with post classes.\n *\n * <span class=\"hljs-doctag\">@return<\/span> array\n *\/<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">photographia_filter_post_classes<\/span><span class=\"hljs-params\">( $classes )<\/span> <\/span>{\n\t<span class=\"hljs-comment\">\/**\n\t * Get the post type template name.\n\t * Empty string if no template is used.\n\t *\/<\/span>\n\t$post_type_template = photographia_get_post_type_template();\n\n\t<span class=\"hljs-comment\">\/**\n\t * Add post template class if post has a template\n\t *\/<\/span>\n\t<span class=\"hljs-keyword\">if<\/span> ( <span class=\"hljs-string\">''<\/span> !== $post_type_template ) {\n\t\t$classes&#91;] .= <span class=\"hljs-string\">\"-$post_type_template-template\"<\/span>;\n\t}\n\n\t<span class=\"hljs-keyword\">return<\/span> $classes;\n}\n\nadd_filter( <span class=\"hljs-string\">'post_class'<\/span>, <span class=\"hljs-string\">'photographia_filter_post_classes'<\/span> );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We get the string of the post template name \u2014 or an empty string if no template is used \u2014 by calling our <code class=\"lang-php\">photographia_get_post_type_template()<\/code> function. If the function does not return an empty string, we add a new array value to <code class=\"lang-php\">$classes<\/code> and return the classes array.<\/p>\n\n\n\n<p>Now if a post has a template, calling the <code class=\"lang-php\">post_class()<\/code> function in the loop returns a class with the pattern <code class=\"lang-markup\">-templatename-template<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For my currently work-in-progress-theme, I want to display posts with post templates different in the blog view. For that, I add the name of the post template to the post_class() output via the post_class filter. This post shows you how to get this working.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[115],"tags":[],"class_list":["post-3468","post","type-post","status-publish","format-standard","hentry","category-wordpress-snippets"],"wp-worthy-pixel":{"ignored":false,"public":"34903400663c4ea983a390c8c8a628e8","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/34903400663c4ea983a390c8c8a628e8"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3468","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=3468"}],"version-history":[{"count":2,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3468\/revisions"}],"predecessor-version":[{"id":5901,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3468\/revisions\/5901"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=3468"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=3468"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=3468"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}