E
E
Ekaterina Ivasenko2020-05-09 00:48:26
WordPress
Ekaterina Ivasenko, 2020-05-09 00:48:26

How to write if the category is x, then the picture is x?

There is a page where different products from different categories are collected
. I have the following code to display posts (product cards):

<?php
                $temp = $wp_query; $wp_query= null;
                $wp_query = new WP_Query(); $wp_query->query('category_name=for_heating&showposts=-1' . '&paged='.$paged);
                 while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
               <div class="col-lg-4 <?php $posttags = get_the_tags();
                    if( $posttags ){
                    foreach( $posttags as $tag ){
                    echo $tag->slug. ' ';
                    }
                } ?>">
    <div class="card_item" id="spoiler1">
      <div class="content-item">
        <div class="title_svg">
          <h4 class="h4_cards">
            <?php the_title() ?>
          </h4>
               <div class="svg_card">
                 <?php the_post_thumbnail( 'full', $svg_attr ); ?> 
                </div>
              </div>
                          </div>
        </div>
    </div>
<?php endwhile; ?>


<?php the_post_thumbnail( 'full', $svg_attr ); ?> - through such a thing now a picture is displayed (from the admin panel) The output is svg
, but in the img tag, and through it you cannot change the fill

fill: #newcolor , if the category is y, then y and so on

You will help with any idea or at least a sketch!
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2020-05-09
@katushell

If on the knee, then like this:

function wpp_change_svg_img_color( $url, $color ) {
  $content                 = file_get_contents( $url );
  $doc                     = new DOMDocument();
  $doc->preserveWhiteSpace = false;
  $doc->loadXML( $content ) or die( 'Failed to load SVG  ' . $url );
  $paths = $doc->getElementsByTagName( "path" );
  foreach ( $paths as $path ) {
    $path->setAttribute( 'fill', $color );
    $path->setAttribute( 'style', 'fill:' . $color );
    $content = $doc->saveXML( $doc );
  }

  return $content;
}

$url = the_post_thumbnail_url( 'full' ); // url миниатюры
$color = '#000000'; #нужный вам цвет
printf( '<img src="%s" alt="">', wpp_change_svg_img_color( $url, $color) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question