1
I have a Wordpress page with some posts, and they present some categories, but we will define them as: cat1, cat2 and cat3.
Within the loop
of Wordpress are displayed three posts
, each with its own category and they have a different image for each one, an example of this is:
- In
cat1
, theposts
that have this category, will have an image in the corner written: 50% Off. - In
cat2
, theposts
that have this category, will have an image in the corner, written: 70% Off. - In
cat3
, follows the same criterion...
I already have a code, and this is where my big problem comes in, it’s too repetitive and it bothers me. I have a set of three pictures with different names and sizes.
They are being called within several if
, following example:
$category = get_cat_name();
if ( $category == "cat1" ) {
echo "<img src='...'> "
}
if ( $category == "cat2" ) {
echo "<img src='...'> "
}
There is possibility to create this same content without a huge repeat?
UPDATE:
I tried to create a foreach
, but I ended up crashing on how to pull the images according to the selected category, the code stayed that way:
$cat_name = get_cat_name();
$test = ["cat1", "cat2", "cat3"];
$images = ["img1.jpg", "img2.jpg", "img3.jpg"];
foreach ( $test as $t ) {
if ( $cat_name == $t ) {
$image = "<img src='./* Aqui que estou confuso */.'>";
}
}
You can use parameters within your parole.
– durtto
@durtto right now I tried an array of objects and then a foreach, but ended up impaling on how to call the images in an orderly way (according to foreach category).
– Lucas Henrique
Wouldn’t it be the case to simply rename the images to Slug (short name) of the categories? There is no foreach... This would be the case for another question, but can you put an extra field in the categories, on site.com/wp-admin/Edit-tags.php? taxonomy=Category
– brasofilo