最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - ACF background-color per post in a WordPress loop

matteradmin9PV0评论

I have the following loop in WordPress:

    <div class="container">
    <?php

    $news = array(
        'post_type' => 'post',
        'category_name' => 'angebote',
        'order' => 'ASC',
        'posts_per_page' => -1
    );


    $wp_query = new WP_Query($news);
    if (have_posts()) : while (have_posts()) :
        the_post(); ?>

        <div class="main-columns">
            <div class="card-content">
                <div class="main-content liste"
                     style="background-image: url(<?php the_field('background-image'); ?>
                             )">
                    <h2><?php the_title(); ?></h2>
                    <div class="row"><?php the_content(); ?></div>
                    <div class="row content-button">
                        <a href="<?php the_permalink(); ?>">
                            <p>MEHR</p>
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <style>
            .card-container .main-content:hover {
                background: <?php echo get_field('background-farbe' ); ?>;
                background-blend-mode: multiply;
            }
        </style>

    <?php var_dump($postid); ?>
    <?php endwhile; ?>

    <?php endif; ?>
    <?php wp_reset_query();
    ?>


</div>

As can be seen, I have the following CSS, what it does is add a color assigned to each post with ACF.

     <style>
        .card-container .main-content:hover {
            background: <?php the_field('background-farbe' ); ?>;
            background-blend-mode: multiply;
        }
    </style>

The problem is that I only get the color of the first post on all. Could someone help me solve the problem?, why do not I see it

I have the following loop in WordPress:

    <div class="container">
    <?php

    $news = array(
        'post_type' => 'post',
        'category_name' => 'angebote',
        'order' => 'ASC',
        'posts_per_page' => -1
    );


    $wp_query = new WP_Query($news);
    if (have_posts()) : while (have_posts()) :
        the_post(); ?>

        <div class="main-columns">
            <div class="card-content">
                <div class="main-content liste"
                     style="background-image: url(<?php the_field('background-image'); ?>
                             )">
                    <h2><?php the_title(); ?></h2>
                    <div class="row"><?php the_content(); ?></div>
                    <div class="row content-button">
                        <a href="<?php the_permalink(); ?>">
                            <p>MEHR</p>
                        </a>
                    </div>
                </div>
            </div>
        </div>
        <style>
            .card-container .main-content:hover {
                background: <?php echo get_field('background-farbe' ); ?>;
                background-blend-mode: multiply;
            }
        </style>

    <?php var_dump($postid); ?>
    <?php endwhile; ?>

    <?php endif; ?>
    <?php wp_reset_query();
    ?>


</div>

As can be seen, I have the following CSS, what it does is add a color assigned to each post with ACF.

     <style>
        .card-container .main-content:hover {
            background: <?php the_field('background-farbe' ); ?>;
            background-blend-mode: multiply;
        }
    </style>

The problem is that I only get the color of the first post on all. Could someone help me solve the problem?, why do not I see it

Share Improve this question edited Oct 31, 2018 at 10:16 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Oct 31, 2018 at 9:15 Dario B.Dario B. 15510 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Well, your problem lies in your CSS code.

    .card-container .main-content:hover {
        background: <?php the_field('background-farbe' ); ?>;
        background-blend-mode: multiply;
    }

It uses class only, so it will be applied to every element with that class. Multiplying the same code with different colors won't change anything - only one such rule will be active.

So how to solve this?

You have to assign unique identifiers to your posts:

<div class="container">
    <?php
        $news = array(
            'post_type' => 'post',
            'category_name' => 'angebote',
            'order' => 'ASC',
            'posts_per_page' => -1
        );

        $wp_query = new WP_Query($news);
        while (have_posts()) :
            the_post();
    ?>
    <div class="main-columns">
        <div class="card-content" id="card-<?php echo esc_attr( get_the_ID() ); ?>">
            <div class="main-content liste"
                 style="background-image: url(<?php the_field('background-image'); ?>
                         )">
                <h2><?php the_title(); ?></h2>
                <div class="row"><?php the_content(); ?></div>
                <div class="row content-button">
                    <a href="<?php the_permalink(); ?>">
                        <p>MEHR</p>
                    </a>
                </div>
            </div>
        </div>
    </div>
    <style>
        #card-<?php echo esc_attr( get_the_ID() ); ?> .main-content:hover {
            background: <?php echo get_field('background-farbe' ); ?>;
            background-blend-mode: multiply;
        }
    </style>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
</div>

PS. You don't need any if statements in your loop, because you don't do anything outside while loop ;)

Post a comment

comment list (0)

  1. No comments so far