• I want to change the background-proberty value of an element (div id = Box2)
    what sits in a container div with red background.

    Box2 should has a background attribute of the specified post-metakey/custom field value.

    When I insert the normal php set of the customfield to show off to the footer.php it shows all the different used values of the specied article.

    The stylesheet is a style.css.php file. I tried 4 Versions and all do not work.

    What do i do wrong?

    /* –V1 (style.css.php)-(this is working) — – – – – – – – – — – */

    <?php
    header(‘Content-type: text/css’);
    $newBG = “darkviolet”;
    ?>

    #Box2 {
    background: <?php echo $newBG ?> ;
    }

    #Box2 {
    background:red
    }

    /* –V2 (style.css.php)- — – – – – – – – – — – */

    <?php
    header(‘Content-type: text/css’);
    $newBG = get_post_meta($post->ID, ‘custombg’, true);
    ?>

    #Box2 {
    background: <?php echo $newBG ?> ;
    }

    #Box2 {
    background:red
    }

    /* –V3 (style.css.php)- — – – – – – – – – — – */

    <?php
    header(‘Content-type: text/css’);
    ?>

    #Box2 {
    background: <?php the_field(‘custombg’) ?> ;
    }

    #Box2 {
    background:red
    }

    /* –V4 (style.css.php)- — – – – – – – – – — – */

    <?php
    header(‘Content-type: text/css’);
    ?>

    #Box2 {
    background: <?php echo the_field(‘custombg’) ?> ;
    }

    #Box2 {
    background:red
    }

    /* –WP Check (all works fine)- — – – – – – – – – — – */

    <?php the_meta();?> /* gives out every post-meta-key + values */
    <br><br>
    <?php echo get_post_meta($post->ID, ‘custombg’, true); ?> /* gives out specified post-meta-key + values */
    <br><br>
    <?php the_field(‘custombg’); ?> /* gives out specified post-meta-key value */

    /* –PHP/HTML values to inject- — – – – – – – – – — – */

    <div id=”Box1″ style=”background:red”>
    <div id=”Box2″>
    </div>
    </div>

    • This topic was modified 7 years, 1 month ago by ptm666.
    • This topic was modified 7 years, 1 month ago by ptm666.
Viewing 3 replies - 1 through 3 (of 3 total)
  • I don’t understand what the problem exactly is and next time please use the code styling so it’s easier for us to read the code.

    Either way in all of your styles you say :

    #Box2 {
    background: <?php echo the_field(‘custombg’) ?> ;
    }
    
    #Box2 {
    background:red
    }

    First of all the background:red is missing a ; and since you have it last it will always overwrite the meta value, so even if you pass a background from your PHP it will always be red as that’s the last CSS style mentioned.

    Thread Starter ptm666

    (@ptm666)

    Its the last, so i dont need that “;” in this case, It′s just a test for me, so I know when another function is calling that background, because I set the first one on !important.

    The problem i have, is, i guess so, that i want to have informations outside the loop right?

    There are the normal php files of wordpress and plugins and so on. I changed style.css to style.css.php so that i can use dynamic code in css. It works somehow I just cant talk to the loop or the element. Its not working. When i bring simple wp calls like “the_meta();” it is not reading it. How can I solve this?

    Thx for your help!

    Thread Starter ptm666

    (@ptm666)

    This works

    <?php
    header(‘Content-type: text/css’);
    $newBG = “darkviolet”;
    ?>
    
    #Box2 {
    background: <?php echo $newBG ?> !important;
    }
    
    #Box2 {
    background:red
    }

    This is not working:

    <?php
    header(‘Content-type: text/css’);
    $newBG = get_post_meta($post->ID, ‘custombg’, true);
    ?>
    
    #Box2 {
    background: <?php echo $newBG ?> !important;
    }
    
    #Box2 {
    background:red
    }

    Is there any chance to bring this dss into the loop that it knows the code? Or can I set this proberty somewhere else:
    $newBG = get_post_meta($post->ID, ‘custombg’, true);
    (customfield-key: custombg / value: yellow)

    • This reply was modified 7 years, 1 month ago by ptm666.
    • This reply was modified 7 years, 1 month ago by ptm666.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘change Element with custom field – not working’ is closed to new replies.