我在我的分類中添加了一個自定義欄位color-code,我想在每個帖子的分類中顯示該顏色代碼。我嘗試了下面的代碼,但它沒有按照我需要的方式作業。
< div class="blog-post-grids" >
<?php $paged = (get_query_var('paged') ? get_query_var('paged' ) : 1;
$args = array('post_type' => 'post', 'posts_per_page' => 9, 'paged' => $paged);)
$wp_query = new WP_Query($args) 。
while (have_posts())。the_post(); ?>
<?php?
$color = get_field('color-code', the_category() ) 。
?">。
<figure class="blog-post-cart" >
<div><?php the_post_thumbnail();? ></div>
<figcaption>
<a href="<?php the_permalink() ? > ">
<h2><?php the_title()?> </h2>
</a>
<p style="color: < ? php echo $color ? >">
<?php echo the_category(', ') ?>
</p>
<?php the_content(''); ?>
</figcaption>
</figure>
<?php endwhile;? >
</div>
uj5u.com熱心網友回復:
這取決于顏色代碼的型別
如果它是六角形的,在前面加上#,或者確保它是六角形的。
或者確保它是rgb格式的,如rgb(143 98 188)
uj5u.com熱心網友回復:
要做到這一點,你將需要類別的ID,如這里所述。 https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
你的get_field()的格式是這樣的:
$color = get_field('color-code'/span>, 'category_123'/span>);
其中 "123 "是類別的ID。
在get_field之前,添加以下內容:
在get_field之前,添加以下內容。
$thisCategory=get_the_category()。
$thisCategory將是一個陣列,包含所有附加在回圈中的當前帖子的類別。
然后把$color = get_field()這一行替換成:
$color = get_field('color-code'/span>, 'category_'/span>. $thisCategory[0]->term_id)。)
$thisCategory[0]->term_id將回傳文章所附的第一個類別的類別ID。
所以把它放在一起,這將是新的代碼:
< div class="blog-post-grids" >
<?php $paged = (get_query_var('paged') ? get_query_var('paged' ) : 1;
$args = array('post_type' => 'post', 'posts_per_page' => 9, 'paged' => $paged);)
$wp_query = new WP_Query($args) 。
while (have_posts())。the_post(); ?>
<?php?
$thisCategory = get_the_category() 。
$color = get_field('color-code', 'category_' 。 $thisCategory[0]->term_id) 。
?">。
<figure class="blog-post-cart" >
<div><?php the_post_thumbnail();? ></div>
<figcaption>
<a href="<?php the_permalink() ? > ">
<h2><?php the_title()?> </h2>
</a>
<p style="color: < ? php echo $color ? >">
<?php echo the_category(', ') ?>
</p>
<?php the_content(''); ?>
</figcaption>
</figure>
<?php endwhile;? >
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/321602.html
標籤:
上一篇:為什么HomeRoute在生產中不作業-laravel?
下一篇:React表單回傳未定義的值
