Site logo

[Resolved] Colour based on title

Home Forums Support [Resolved] Colour based on title

Home Forums Support Colour based on title

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1585458
    Matic

    Hi,
    I am creating a new layout for my website. This question is that GeneratePress related, but since I am using your elements to do this, I am hoping you can at least give me some guideline or a right direction as how to solve this problem.

    Example posts:
    Example #1
    Example #2

    I am using this with Elements > Header

    <div class="post-new">
      <h1 class="post-title-new">
        {{post_title}}
      </h1>
      <p class="post-date-new">
        {{post_date}}
      </p>
      <div class="post-thumbnail-new">
        {{custom_field._thumbnail_id}}
      </div>
    </div>

    At the moment, my class “post-new” has the following feature:
    background-image: linear-gradient(rgb(255, 255, 255) 0%, rgb(200, 180, 230));
    But let’s take a normal background to simplify this:
    background-color: #E6C8B4

    I was wondering how could I make it so that the background-color would be defined based on the URL? I want to create a background that would fit the image colour. I know I could use JS to do that, but that’s an even bigger mess.
    I don’t mind specifying a table in advance, that would look like this, for example:
    URL HEX
    URL1 HEX1
    URL2 HEX2
    etc

    I could then use some kind of PHP, such as (I am making it up because I don’t know PHP):

    
    URL = {{post-url}} 
    for URL$ = HEX$

    and the HTML (only the first div)
    <div style="HEX$"></div>

    Does any of this make sense? 😀

    Cheers,
    Matic

    #1585491
    Elvin
    Staff
    Customer Support

    Hi,

    Are the URLs static? or are they the actual URL or the current page?

    Or perhaps you want the hex color change depending on post type(page, single post, archive, etc)?

    Let us know.

    #1585517
    Matic

    Hey,

    the URLs change based on the post. I want this code for single posts only, and each has a different URL.
    Nope, I want the HEX based on image average, a thing can be done with JS, but I don’t mind providing HEX manually. I know I could do this by creating separate header for each post, but that would leave a mess after a while. Or do you think that this is a better option?

    Cheers

    #1585608
    David
    Staff
    Customer Support

    Hi there,

    heres a an example that uses custom fields to set the background color of the page-hero:

    https://generatepress.com/forums/topic/using-custom-field-page-values-in-element-header/#post-985694

    The same logic applies – you just need to load the $css variable with your color options – and target the correct selector in the echo statement.

    #1586038
    Matic

    To be fair, I cannot figure out what to do beside adding a PHP hook.

    I have this HTML now (in a header):

    <style>.post-new {background-color: {{custom_field.css}}}</style>
    <div class="post-new">
    *some code*
    </div>

    This PHP (in a hook):

    <?php
    $css = get_post_meta(get_the_ID(), 'css', true);
    if (!empty($css)) { ?>
        <style type="text/css">
            <?php echo ' .post-new {background-color: ' . $css .';}'; ?>
        </style>
        <?php }
    ?>

    I suppose I should add some CSS with post IDs now, but I don’t know where and how.
    This doesn’t work (obviously):
    #post-8321.page-hero {background-color: red;}

    Cheers

    #1586088
    David
    Staff
    Customer Support

    How many colors are we dealing with ? Will each post have a different color ?

    #1586091
    Matic

    That was the original idea, but if that’s too difficult (and would mean too much CSS), I could do with only 7-8 colours (red, orange, etc).

    #1586111
    David
    Staff
    Customer Support

    With this snippet hooked into the wp_head

    <?php
    $css = get_post_meta(get_the_ID(), 'css', true);
    if (!empty($css)) { ?>
        <style type="text/css">
            <?php echo ' .post-new {background-color: ' . $css .';}'; ?>
        </style>
        <?php }
    ?>

    You can just create a Custom Field in the Post editor – see here:

    https://wordpress.org/support/article/custom-fields/

    Give the Field a Name of css or whatever name you give it in this line of the code:

    $css = get_post_meta(get_the_ID(), ‘css’, true);

    And set its Value to the HEX/RGBA you require.

    This will print out that CSS for each post.

    #1586185
    Matic

    Ok, I finally understand! It works like a charm now + I can give each post its own background.

    Thank you very much 😀

    Cheers

    #1586397
    David
    Staff
    Customer Support

    Glad to be of help!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.