Answer the question
In order to leave comments, you need to log in
Images for categories are all the same, why?
Hello! Faced such a problem. It is necessary to display categories of articles in the form of pictures on the main page. Found the solution from this link . One problem, all categories display only one picture, not different ones. Please tell me what is the problem?
Here is the category output code
<section class="container pt-3">
<div class="row mt-5 mb-5 text-light">
<?php
$categories = get_categories(array(
orderby' => 'name',
'order' => 'ASC',
'type' => 'post'
);
foreach( $categories as $category ){
?>
<div class="work col-3 mt-1 pr-1 item">
<div class="copywriter">
?php
echo '<a class="rounded border border-light p-2" href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
$term_id = get_queried_object_id();
// получим ID картинки из метаполя термина
$image_id = get_term_meta( $term_id, '_thumbnail_id', 1 );
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
// выводим картинку на экран
echo '<img src="'. $image_url .'" alt="" />';
?>
</div>
</div>
<?php
}
?>
</div>
</section>
Answer the question
In order to leave comments, you need to log in
There are at least a lot of syntax errors in your code.
Corrected version without synth. errors:
<section class="container pt-3">
<div class="row mt-5 mb-5 text-light">
<?php
$categories = get_categories(array(
'orderby' => 'name',
'order' => 'ASC',
'type' => 'post'
));
?>
<?php foreach( $categories as $category ){ ?>
<div class="work col-3 mt-1 pr-1 item">
<div class="copywriter">
<?php echo '<a class="rounded border border-light p-2" href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
$term_id = get_queried_object_id();
// получим ID картинки из метаполя термина
$image_id = get_term_meta( $term_id, '_thumbnail_id', 1 );
$image_url = wp_get_attachment_image_url( $image_id, 'full' );
// выводим картинку на экран
echo '<img src="'. $image_url .'" alt="" />';
?>
</div>
</div>
<?php } ?>
</div>
</section>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question