I followed this tutorial to add colors to categories. However I can't make it work, when I try to use it in my loop nothing happens, I tried multiple versions but none was right. This is what I tried:
<?php
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
?>
<div class="catalogue_item">
<h2 style="color:<?php
//This is where i want the category color name
;
?>"><?php the_title(); ?></h2>
<a href="<?php echo get_permalink(); ?>">lien vers la fiche</a>
<p>
<?php foreach ( ( get_the_category() ) as $category ) {
echo $category->cat_name . ' ';
}?>
</p>
<?php the_excerpt(); ?>
<?php the_content(); ?>
<p> prix: <?php echo (types_render_field( 'price', array() ));?> </p>
<p> prix2: <?php echo (types_render_field( 'pricet', array() ));?> </p>
</div>
<?php
}
}
?>
And I cannot find something that works, I'm sorry if this is a dumb one but I am stuck.
I followed this tutorial to add colors to categories. However I can't make it work, when I try to use it in my loop nothing happens, I tried multiple versions but none was right. This is what I tried:
<?php
$args = array(
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
?>
<div class="catalogue_item">
<h2 style="color:<?php
//This is where i want the category color name
;
?>"><?php the_title(); ?></h2>
<a href="<?php echo get_permalink(); ?>">lien vers la fiche</a>
<p>
<?php foreach ( ( get_the_category() ) as $category ) {
echo $category->cat_name . ' ';
}?>
</p>
<?php the_excerpt(); ?>
<?php the_content(); ?>
<p> prix: <?php echo (types_render_field( 'price', array() ));?> </p>
<p> prix2: <?php echo (types_render_field( 'pricet', array() ));?> </p>
</div>
<?php
}
}
?>
And I cannot find something that works, I'm sorry if this is a dumb one but I am stuck.
Share Improve this question asked Jan 9, 2021 at 19:56 DendenzillaDendenzilla 1212 bronze badges1 Answer
Reset to default 0From your question, it seems that you want to fetch the meta for the post. In which case you would want get_post_meta
which would look something like this:
echo get_post_meta( get_the_ID(), 'color', true );
Change the second value ('color') to whatever you have used for the meta key - I took a wild guess.