{"id":6696,"date":"2022-07-19T21:00:01","date_gmt":"2022-07-19T12:00:01","guid":{"rendered":"https:\/\/the-world-star.com\/?p=6696"},"modified":"2026-11-20T08:08:12","modified_gmt":"2026-11-19T23:08:12","slug":"adding-category-images-to-category-top-pages-implementation-step-3","status":"publish","type":"post","link":"https:\/\/the-world-star.com\/en\/server\/adding-category-images-to-category-top-pages-implementation-step-3-6696\/","title":{"rendered":"Adding Category Images to Category Top Pages \u2014 Implementation Step 3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Continuing from the last post.<br>I want to display a category image at the very top of both the category archive page and individual posts within that category. This helps leave a lasting impression on visitors and makes it instantly clear when they've wandered into a completely different topic area, as if written by someone else. This is part of our series on making categories independent as an alternative to multisite.<\/p>\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\t\t\t<div class=\"blog-card\">\n\t\t\t\t<div class=\"blog-card-body-outer\">\n\t\t\t\t\t<div class=\"blog-card-body\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t<h5 class=\"blog-card-title\">\n\t\t\t\t\t\t\t\t<a href=\"https:\/\/the-world-star.com\/en\/server\/adding-category-feature-images-to-category-top-pages-implementation-steps-1-2-6699\/\">Adding Category Feature Images to Category Top Pages \u2014 Implementation Steps 1 &#038; 2<\/a>\n\t\t\t\t\t\t\t<\/h5>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<p class=\"blog-card-text\">\n\t\t\t\t\t\t\t\tI want to display a category feature image at the top of both category archive pages and individual blog posts. Doing this helps leave a lasting impression on \u2026\t\t\t\t\t\t\t<\/p>\n\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"blog-card-site-title\">\n\t\t\t\t\t\t\t<a href=\"http:\/\/the-world-star.com\/en\/\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tThe\u2606World Star\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<div class=\"blog-card-image-outer\">\n\t\t\t\t\t\t<a href=\"https:\/\/the-world-star.com\/en\/server\/adding-category-feature-images-to-category-top-pages-implementation-steps-1-2-6699\/\" class=\"blog-card-image-frame\">\n\t\t\t\t\t\t\t<img decoding=\"async\" class=\"blog-card-image-src\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/31115522\/server_eyecatch.jpg\" alt=\"\">\n\t\t\t\t\t\t<\/a>\n\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\n\t\t\t<\/div><\/figure>\n\n\n<p class=\"wp-block-paragraph\">This time, we're going to display the category name overlaid directly on top of the category image.<br>I've gotten used to this by now, so the research phase is practically gone.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Researching HTML Structure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the site I used for reference this time. Instead of using stylesheets, alignment is specified inline.<br><a href=\"https:\/\/gazocustomize.com\/top-text-image\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/gazocustomize.com\/top-text-image\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since the text wouldn't center properly using the method on the site above, I did some extra googling for the specific property details.<br><a rel=\"noreferrer noopener\" href=\"https:\/\/sororikaku.com\/center-text\/\" data-type=\"URL\" data-id=\"https:\/\/sororikaku.com\/center-text\/\" target=\"_blank\">https:\/\/sororikaku.com\/center-text\/<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ultimately, I want to shape the HTML like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div style=\"position: relative; display:inline-block;\"&gt;\n&lt;img src=\".\/img\/\u30ab\u30c6\u30b4\u30ea\u30fc\u306e\u753b\u50cf.jpg\" alt=\"topimg\"&gt;\n&lt;div style=\"position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)\"&gt;\n&lt;p&gt;\u30ab\u30c6\u30b4\u30ea\u30fc\u540d&lt;\/p&gt;\n&lt;\/div&gt;&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Modifying the PHP Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Following on from last time,<br>we'll modify the following Lightning theme file responsible for global page header rendering:<br>\/var\/www\/html\/wp-content\/themes\/lightning\/_g3\/template-parts\/page-header.php<br>We'll edit the exact same spot we modified last time. Around the code outputting the category image and the category name, we'll echo a bunch of HTML to construct the layout we looked up.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!-- \/\/\u30ab\u30c6\u30b4\u30ea\u30fc\u72ec\u7acb\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u305f\u3081\u306e\u4fee\u6b63\u3001\u30d7\u30e9\u30b0\u30a4\u30f3\u306eCategory Images\u3092\u5229\u7528\u3057\u3066\u3044\u308b\n&lt;!-- \/\/category\u3060\u3063\u305f\u3089\u306e\u6761\u4ef6\u306fz_taxonomy_image()\u306b\u5165\u3063\u3066\u3044\u305f\u306e\u3067\u305d\u3053\u3082\u3044\u3058\u308b --&gt;\n&lt;?php\nif (function_exists('z_taxonomy_image')){\n<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">  echo '&lt;div style=\"position: relative; display:inline-block;\"&gt;';<\/mark>\n  $taximage = z_taxonomy_image(); \/\/\u3053\u308c\u3067\u753b\u50cf\u8868\u793a\u306eecho\u3001\u6210\u529f\u6642\u306b\u3082html\u30b3\u30fc\u30c9\u3092\u8fd4\u3059\u3088\u3046\u306b\u3057\u305f\n  if(!empty($taximage)){           \/\/\u753b\u50cf\u304c\u3042\u3063\u305f\u3089\u30ab\u30c6\u30b4\u30ea\u30fc\u540d\u3092\u8868\u793a\n<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">    echo '&lt;div style=\"position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)\"&gt;';\n    echo '&lt;p&gt;';<\/mark>\n    echo wp_kses( apply_filters( 'lightning_page_header_title_html', $page_header_title_html ), $allowed_html ); \/\/\u30ab\u30c6\u30b4\u30ea\u30fc\u540d\n<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">    echo '&lt;\/p&gt;';\n    echo '&lt;\/div&gt;';<\/mark>\n  }\n<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">  echo '&lt;\/div&gt;';<\/mark>\n }\n?&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And that's it. Once you reach this point, you can pretty much do anything.<br>Wouldn't it be nice to fetch color settings straight from the Lightning configurations? Well, let's put a pin in that for now.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Result<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It worked. And it looks pretty sleek, too.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"901\" height=\"717\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20132109\/Screenshot-from-2022-07-20-13-20-54.png\" alt=\"\" class=\"wp-image-2066\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20132109\/Screenshot-from-2022-07-20-13-20-54.png 901w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20132109\/Screenshot-from-2022-07-20-13-20-54-300x239.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20132109\/Screenshot-from-2022-07-20-13-20-54-768x611.png 768w\" sizes=\"auto, (max-width: 901px) 100vw, 901px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Next Steps<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I've accomplished most of what I wanted to do.<br>\u30fbI want to serve smaller images for mobile devices.<br>Though I wonder how things look on 4K smartphones nowadays?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Uploading a single image automatically generates and uploads these filename and size variations simultaneously:<br>category-kayak-1024x576.jpg<br>category-kayak-150x150.jpg<br>category-kayak-1536x863.jpg<br>category-kayak-300x169.jpg<br>category-kayak-320x180.jpg<br>category-kayak-768x432.jpg<br>category-kayak.jpg<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Continuing from the last post.I want to display a category image at the very top of both the category archive page and individual posts within that category. This helps leave a lasting impression on visitors and makes it instantly clear when they've wandered into a completely different topic area, as if written by someone else. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2606,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_locale":"en_US","_original_post":"https:\/\/175.41.248.22\/?p=2058","_lightning_design_setting":{"layout":"default"},"footnotes":""},"categories":[3],"tags":[68,81,1241,1255,1256,1257,67],"class_list":["post-6696","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-wordpress","tag-lightning","tag-category-top","tag-category","tag-category-image","tag-independence","tag-server","en-US"],"_links":{"self":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6696","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/comments?post=6696"}],"version-history":[{"count":2,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6696\/revisions"}],"predecessor-version":[{"id":6698,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6696\/revisions\/6698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media\/2606"}],"wp:attachment":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/media?parent=6696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/categories?post=6696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/tags?post=6696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}