使用 Wordpress 自定义字段为背景帖子着色

Using Wordpress Custom Fields to Color background posts

我正在尝试使用 wordpress 中的自定义字段为每个 post 制作不同的背景颜色,就像这样 website here

截至目前,在我的本地主机上,作为测试,我将自定义字段名称设置为 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>

它把所有 post 作为从最新 post 添加的最新颜色来处理,想知道如何解决这个问题

它还在我的本地主机网络服务器上显示颜色,但不在线

我删除了行

$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) ?>

像这样

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

效果很好。

以防有人需要知道如何解决这样的问题