使用WordPress自定义字段,以彩色背景帖子

问题描述:

我试图使用WordPress中的自定义字段,以使不同的背景颜色为每个帖子有点像这个website here使用WordPress自定义字段,以彩色背景帖子

截至目前在我的本地,作为测试我具有自定义字段名称为ABC123,然后该值作为黄色

这里是代码:

<section> 
    <div class="container"> 
     <div class="row"> 
      <div class="col-lg-12 text-center"> 
       <h2 class="section-heading">Check out our blog</h2> 
       <hr class="light"> 
       </div> 
       <div class="col-lg-8 col-lg-offset-2 text-center" > 
        <?php 
// Get the last 3 posts. 
global $post; 
$args = array('posts_per_page' => 3); 
$myposts = get_posts($args); 
$bgcolor1 = get_post_meta($post->ID, "abc123", true); 
foreach($myposts as $post) : setup_postdata($post); ?> 
        <div style="background-color:<?php echo $bgcolor1; ?>"> 
         <h2 class="section-heading"> 
          <a class="link" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to 
           <?php the_title_attribute(); ?>"> 
           <?php the_title(); ?> 
          </a> 
         </h2> 
         <p> 
          <?php the_excerpt();?> 
         </p> 
         <a class="link" href=" 
          <?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">Read More!</a> 
         <hr class="blight"> 
         </div> 
         <?php endforeach; ?> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
</section> 

它做所有的帖子从最新的帖子中添加最新的颜色,不知道如何解决

也是它的显示在我的本地Web服务器的颜色,但不能上网

我已删除的行

$bgcolor1 = get_post_meta($post->ID, "abc123", true); 

,也为$ bgcolor1

<div style="background-color:<?php echo $bgcolor1; ?>"> 

回声,取而代之的是

<?php echo get_post_meta($post->ID,'custom-field-name',true) ?> 

like so

<div style="background-color:<?php echo get_post_meta($post->ID,'custom-field-name',true) ?>;"> 

它很好用。

因为任何人需要知道如何解决这样的问题