{"id":6699,"date":"2022-07-19T20:00:01","date_gmt":"2022-07-19T11:00:01","guid":{"rendered":"https:\/\/the-world-star.com\/?p=6699"},"modified":"2026-11-20T14:18:22","modified_gmt":"2026-11-20T05:18:22","slug":"adding-category-feature-images-to-category-top-pages-implementation-steps-1-2","status":"publish","type":"post","link":"https:\/\/the-world-star.com\/en\/server\/adding-category-feature-images-to-category-top-pages-implementation-steps-1-2-6699\/","title":{"rendered":"Adding Category Feature Images to Category Top Pages \u2014 Implementation Steps 1 &#038; 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I 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 visitors and clearly signals that they are reading content from a completely different category. This is a continuation of my \"category independence strategy,\" which I started after deciding to ditch WordPress multisite.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">STEP 1: Display the Category Image If Available, Otherwise Fall Back to Default<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I used the \"Category Images\" plugin to enable uploading images for each category. By modifying the plugin's functions and the display logic in the Lightning theme, I set it up so that if a category image exists, it displays; otherwise, it falls back to the standard text-with-colored-background display. Here is how I modified it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The \"Category Images\" Plugin<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can set category images like this. To display them, you need to modify the PHP code of your theme for each environment and call the plugin's functions.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"923\" height=\"991\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095937\/Screenshot-from-2022-07-20-09-59-23.png\" alt=\"\" class=\"wp-image-2036\" style=\"width:540px;height:580px\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095937\/Screenshot-from-2022-07-20-09-59-23.png 923w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095937\/Screenshot-from-2022-07-20-09-59-23-279x300.png 279w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095937\/Screenshot-from-2022-07-20-09-59-23-768x825.png 768w\" sizes=\"auto, (max-width: 923px) 100vw, 923px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Code Modification 1<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\/var\/www\/html\/wp-content\/themes\/lightning\/_g3\/template-parts\/page-header.php<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At the very bottom of the file.<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-background-color has-background\"><code>&lt;!-- \/\/category\u3060\u3063\u305f\u3089\u306e\u6761\u4ef6\u306fz_taxonomy_image()\u306b\u5165\u3063\u3066\u3044\u308b --&gt;\n&lt;?php\nif (function_exists('z_taxonomy_image')){\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\n    echo wp_kses( apply_filters( 'lightning_page_header_title_html', $page_header_title_html ), $allowed_html ); \/\/\u30ab\u30c6\u30b4\u30ea\u30fc\u540d\n }\n?&gt;\n\n&lt;!-- \/\/edit \u9752\u3044\u67a0 --&gt;\n&lt;?php\nif (empty($taximage)){ \/\/\u753b\u50cf\u304c\u306a\u304b\u3063\u305f\u3089\u3053\u308c\u307e\u3067\u901a\u308a\u306e\u9752\u3044\u67a0+\u6587\u5b57\n  echo '<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">&lt;div class=\"page-header\"&gt;&lt;div class=\"page-header-inner container\"&gt;<\/mark>';\n  echo <mark style=\"background-color:#8ed1fc\" class=\"has-inline-color has-black-color\">wp_kses( apply_filters( 'lightning_page_header_title_html', $page_header_title_html ), $allowed_html );<\/mark>\n  echo ('<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">&lt;\/div&gt;&lt;\/div&gt;<\/mark>');\n }\n?&gt;\n\n<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">&lt;!-- &#91; \/.page-header ] --&gt;<\/mark><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since there are a lot of changes, I've only highlighted the parts that resemble the original code.<br>There are two ways to use echo: echo(); and echo 'xxxx'. If you enclose strings in single quotes (') without parentheses, you don't need to escape double quotes (\") with a backslash (). You can use double quotes instead, but if you do, any original double quotes inside need to be escaped as \".<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Addendum (2022\/08\/03)<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Based on the original implementation, if the image width was narrower than the full content width, it aligned to the left. I've updated it to center the image instead. I added some HTML tag outputs, so if you're going to copy and paste, I recommend grabbing the updated version here.<\/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\/centering-category-feature-images-in-wordpress-6671\/\">Centering Category Feature Images in WordPress<\/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 recently set up category-specific feature images to appear on category top pages and post pages. However, smaller images were aligning to the left, especiall\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\/centering-category-feature-images-in-wordpress-6671\/\" 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\/20173139\/20220720_172737.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<h3 class=\"wp-block-heading\">Code Modification 2<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I wanted to check whether an image existed using the plugin's z_taxonomy_image() function. However, it had this weird quirk where it only returned a value when it wasn't echoing, so I tweaked it to return the value even when echoing.<br>\/var\/www\/html\/wp-content\/plugins\/categories-images\/categories-images.php<br>The function is zTaxonomyImage()<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-background-color has-background\"><code>\n        if ($echo)    echo $taxonomy_image;\n<mark style=\"background-color:#f0c5d0\" class=\"has-inline-color\">        \/\/else \/\/edit \u753b\u50cf\u304c\u3042\u3063\u305f\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3057\u305f\u3044<\/mark>\n\treturn $taxonomy_image;\n    }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Specifically, comment out the else statement and unconditionally return $xxx below it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Results of the Modification<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here is how it looks in the test environment.<br>For categories where I configured an image using the Category Images plugin, the category page now displays that image alongside the category name.<br>I hid the original blue banner and category name, displaying the category name separately for the next step.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"772\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095306\/Screenshot-from-2022-07-20-09-52-41.png\" alt=\"\" class=\"wp-image-2033\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095306\/Screenshot-from-2022-07-20-09-52-41.png 831w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095306\/Screenshot-from-2022-07-20-09-52-41-300x279.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095306\/Screenshot-from-2022-07-20-09-52-41-768x713.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Categories without an image still display the centered text over a blue background, just like before.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"831\" height=\"772\" src=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095332\/Screenshot-from-2022-07-20-09-52-51.png\" alt=\"\" class=\"wp-image-2034\" srcset=\"https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095332\/Screenshot-from-2022-07-20-09-52-51.png 831w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095332\/Screenshot-from-2022-07-20-09-52-51-300x279.png 300w, https:\/\/static.the-world-star.com\/wp-content\/uploads\/2022\/07\/20095332\/Screenshot-from-2022-07-20-09-52-51-768x713.png 768w\" sizes=\"auto, (max-width: 831px) 100vw, 831px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">By the way, I initially thought Lightning couldn't do this because it felt a bit limited, but research showed that the paid version supports this natively. So, throwing money at the problem is always an option, or switching to a different theme entirely. But out of sheer curiosity, I'll push forward with this approach for now.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">STEP 2: Applying It to Single Post Pages<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Category images are also important on single post pages for visual impact. While the Lightning theme handles it the same way, the plugin restricts functionality based on the page type. So, I need to modify the same file in the plugin.<br>\/var\/www\/html\/wp-content\/plugins\/categories-images\/categories-images.php<br>The function is zTaxonomyImage()<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   function zTaxonomyImage($term_id = NULL, $size = 'full', $attr = NULL, $echo = TRUE) {\n        if (!$term_id) {\n            if (is_category())\n\t      $term_id = get_query_var('cat');\n            elseif (is_tag())\n\t      $term_id = get_query_var('tag_id');\n            elseif (is_tax()) {\n                $current_term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));\n                $term_id = $current_term-&gt;term_id;\n            }\n<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\t    elseif(is_single()){ \/\/edit \u8ffd\u52a0 \u6295\u7a3f\u8a18\u4e8b\u306e\u3068\u304d\u306f\u4e0a\u8a18\u3069\u308c\u3067\u3082\u306a\u3044\u3002\u901a\u5e38\u8a18\u4e8b\u3067\u3042\u308b\u5224\u5b9a\u306f\uff1fis_single()\n\t      $cats   = get_the_category(); \/\/category\u306f\u914d\u5217\u3067\u5165\u308b\n\t      $term_id = $cats&#91;0]-&gt;term_id;  \/\/\u4e00\u3064\u76ee\u306ecategory ID\u3092\u5165\u308c\u308b\n\t    }                    \/\/---edit\u3053\u3053\u307e\u3067<\/mark>\n\t    \/\/echo (\"DEBUG:term_id:\"); echo ($term_id);\/\/edit is_cat\u306e\u3068\u304d\u306f21\u3067\u3001\u901a\u5e38\u8a18\u4e8b\u306e\u3068\u304d\u306f\u4f55\u3082\u5165\u3063\u3066\u306a\u3044\n        }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the highlighted blue sections.<br>I learned that is_single() is the conditional function used to check for single post pages.<br>The subsequent processing utilizes the knowledge I gained previously when I retrieved category IDs to display recent posts from the same category on single posts.<\/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\/how-to-show-only-same-category-posts-in-recent-posts-on-wordpress-6702\/\">How to Show Only Same-Category Posts in &quot;Recent Posts&quot; on WordPress<\/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\tContinuing from my last post, I&#039;ve been exploring how to prevent the &quot;Recent Posts&quot; widget from displaying unrelated content that readers aren&amp;#\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\/how-to-show-only-same-category-posts-in-recent-posts-on-wordpress-6702\/\" 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\/17161431\/Screenshot-from-2022-07-17-16-13-40-1024x499.png\" 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\">By the way, there is another function called zTaxonomyImage_url() higher up that contains similar logic, but since I'm not using it this time, I'm going to ignore it\u2014mainly because the fix from Step 1 wouldn't work on it directly. Having duplicated logic like this is considered \"code cloning\" and is generally bad practice, which annoys me. 100% of the two places I had to tweak in this plugin involved less-than-ideal code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What I Want to Do Next<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">I want to overlay text on top of the image, similar to the Lightning top page layout. I don't need a multi-image slider. It seems the standard way is to specify this via CSS, though there also appears to be a way without using stylesheets. I'll write about that in the next post.<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I 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 visitors and clearly signals that they are reading content from a completely different category. This is a continuation of my \"category independence strategy,\" which I started after [&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=2028","_lightning_design_setting":{"layout":"default"},"footnotes":""},"categories":[3],"tags":[67,68,81,87,1240,1255],"class_list":["post-6699","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-server","tag-wordpress","tag-lightning","tag-top","tag-image","tag-category","en-US"],"_links":{"self":[{"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6699","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=6699"}],"version-history":[{"count":2,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6699\/revisions"}],"predecessor-version":[{"id":6701,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/posts\/6699\/revisions\/6701"}],"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=6699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/categories?post=6699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/the-world-star.com\/wp-json\/wp\/v2\/tags?post=6699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}