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

For my currently work-in-progress-theme, I want to display posts with post templates<\/a> different in the blog view. For that, I add the name of the post template to the post_class()<\/code> output via the post_class<\/code> filter. This post shows you how to get this working.<\/p>\n\n\n\n\n\n\n\n

Function for returning the post template slug<\/h2>\n\n\n\n

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

if<\/span> ( ! function_exists( 'photographia_get_post_type_template'<\/span> ) ) {\n\t\/**\n\t * Returns the post type template slug \n\t * without templates\/ dir and .php ending.\n\t *\n\t * @return<\/span> string\n\t *\/<\/span>\n\tfunction<\/span> photographia_get_post_type_template<\/span>()<\/span> <\/span>{\n\t\t$template_slug = get_page_template_slug();\n\t\tif<\/span> ( ''<\/span> !== $template_slug ) {\n\t\t\t\/**\n\t\t\t * Remove \u00bbtemplates\/\u00ab from slug.\n\t\t\t *\/<\/span>\n\t\t\t$template_slug = str_replace( 'templates\/'<\/span>, ''<\/span>, $template_slug );\n\n\t\t\t\/**\n\t\t\t * Remove .php file ending.\n\t\t\t *\/<\/span>\n\t\t\t$post_type_template = str_replace( '.php'<\/span>, ''<\/span>, $template_slug );\n\n\t\t\treturn<\/span> $post_type_template;\n\t\t} else<\/span> {\n\t\t\treturn<\/span> ''<\/span>;\n\t\t}\n\t}\n}<\/code><\/div>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

At the first line, we check if the function 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 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: templates\/name-of-template.php<\/code>. We do not need the templates\/<\/code> part and the .php<\/code> ending \u2014 let us remove them with two str_replace()<\/code> calls and return the $post_type<\/code>.<\/p>\n\n\n\n

If the post has no template, we return an empty string.<\/p>\n\n\n\n

Function for adding the class<\/h2>\n\n\n\n

Now the function, which filters the post_class<\/code>:<\/p>\n\n\n

\/**\n * Add classes to post_class()\n *\n * @param<\/span> array $classes array with post classes.\n *\n * @return<\/span> array\n *\/<\/span>\nfunction<\/span> photographia_filter_post_classes<\/span>( $classes )<\/span> <\/span>{\n\t\/**\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\/**\n\t * Add post template class if post has a template\n\t *\/<\/span>\n\tif<\/span> ( ''<\/span> !== $post_type_template ) {\n\t\t$classes[] .= \"-$post_type_template-template\"<\/span>;\n\t}\n\n\treturn<\/span> $classes;\n}\n\nadd_filter( 'post_class'<\/span>, 'photographia_filter_post_classes'<\/span> );<\/code><\/div>Code language:<\/span> PHP<\/span> (<\/span>php<\/span>)<\/span><\/small><\/pre>\n\n\n

We get the string of the post template name \u2014 or an empty string if no template is used \u2014 by calling our photographia_get_post_type_template()<\/code> function. If the function does not return an empty string, we add a new array value to $classes<\/code> and return the classes array.<\/p>\n\n\n\n

Now if a post has a template, calling the post_class()<\/code> function in the loop returns a class with the pattern -templatename-template<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"

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":{"footnotes":"","wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","lazy_load_responsive_images_disabled":false},"categories":[115],"tags":[],"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"}],"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}]}}