Adding Category Images to Category Top Pages — Implementation Step 3

English

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. This is part of our series on making categories independent as an alternative to multisite.

Adding Category Feature Images to Category Top Pages — Implementation Steps 1 & 2

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 …

This time, we're going to display the category name overlaid directly on top of the category image.
I've gotten used to this by now, so the research phase is practically gone.

Researching HTML Structure

Here is the site I used for reference this time. Instead of using stylesheets, alignment is specified inline.
https://gazocustomize.com/top-text-image/

Since the text wouldn't center properly using the method on the site above, I did some extra googling for the specific property details.
https://sororikaku.com/center-text/

Ultimately, I want to shape the HTML like this.

<div style="position: relative; display:inline-block;">
<img src="./img/カテゴリーの画像.jpg" alt="topimg">
<div style="position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)">
<p>カテゴリー名</p>
</div></div>

Modifying the PHP Code

Following on from last time,
we'll modify the following Lightning theme file responsible for global page header rendering:
/var/www/html/wp-content/themes/lightning/_g3/template-parts/page-header.php
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.

<!-- //カテゴリー独立プロジェクトのための修正、プラグインのCategory Imagesを利用している
<!-- //categoryだったらの条件はz_taxonomy_image()に入っていたのでそこもいじる -->
<?php
if (function_exists('z_taxonomy_image')){
  echo '<div style="position: relative; display:inline-block;">';
  $taximage = z_taxonomy_image(); //これで画像[show]のecho、成功時にもhtmlコードを返すようにした
  if(!empty($taximage)){           //画像があったらカテゴリー名を[show]
    echo '<div style="position: absolute; top: 50%; left: 50%; max-width: 100%; max-height: 100%; color:white;transform:translate(-50%,-50%)">';
    echo '<p>';
    echo wp_kses( apply_filters( 'lightning_page_header_title_html', $page_header_title_html ), $allowed_html ); //カテゴリー名
    echo '</p>';
    echo '</div>';
  }
  echo '</div>';
 }
?>

And that's it. Once you reach this point, you can pretty much do anything.
Wouldn't it be nice to fetch color settings straight from the Lightning configurations? Well, let's put a pin in that for now.

The Result

It worked. And it looks pretty sleek, too.

Next Steps

I've accomplished most of what I wanted to do.
・I want to serve smaller images for mobile devices.
Though I wonder how things look on 4K smartphones nowadays?

Uploading a single image automatically generates and uploads these filename and size variations simultaneously:
category-kayak-1024x576.jpg
category-kayak-150x150.jpg
category-kayak-1536x863.jpg
category-kayak-300x169.jpg
category-kayak-320x180.jpg
category-kayak-768x432.jpg
category-kayak.jpg

Leave a Reply