最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Retrieving category's color with get_term_meta

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

From 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.

发布评论

评论列表(0)

  1. 暂无评论