change read more text in archive page depending on post category

admin2025-05-31  0

I'm looking for a way to change the read more text on the archive page when posts are in a certain parent category, let's say 'Apple' or 'Strawberry'. Posts in other categories will use the standard text.

On the web i've found a lot of code snippets, but they didn't work out for me. Does anyone have a clue how to solve it?

I'm looking for a way to change the read more text on the archive page when posts are in a certain parent category, let's say 'Apple' or 'Strawberry'. Posts in other categories will use the standard text.

On the web i've found a lot of code snippets, but they didn't work out for me. Does anyone have a clue how to solve it?

Share Improve this question asked May 5 at 14:12 ErickErick 112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

With the online php generator I've created this code snippet.

$post_categories = get_the_category($post->ID);
$parent_category_name = '';

if (!empty($post_categories)) {
    $parent_category = $post_categories[0]->parent ? get_category($post_categories[0]->parent) : $post_categories[0];
    $parent_category_name = $parent_category->name;
}

if ($parent_category_name === 'Apple' || $parent_category_name === 'Strawberry') {

    echo "<span>Apples &amp; Strawberries</span>";
} else {
    echo "<span>Fruits</span>";
}

It really works fine for me.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748633355a313671.html

最新回复(0)