- This topic has 9 replies, 3 voices, and was last updated 5 years, 4 months ago by
David.
-
AuthorPosts
-
December 18, 2020 at 1:52 am #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 #2I 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: #E6C8B4I 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
etcI 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,
MaticDecember 18, 2020 at 2:16 am #1585491Elvin
StaffCustomer SupportHi,
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.
December 18, 2020 at 2:25 am #1585517Matic
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
December 18, 2020 at 3:44 am #1585608David
StaffCustomer SupportHi 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
$cssvariable with your color options – and target the correct selector in the echo statement.December 18, 2020 at 7:19 am #1586038Matic
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
December 18, 2020 at 7:49 am #1586088David
StaffCustomer SupportHow many colors are we dealing with ? Will each post have a different color ?
December 18, 2020 at 7:52 am #1586091Matic
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).
December 18, 2020 at 8:11 am #1586111David
StaffCustomer SupportWith 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
cssor 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.
December 18, 2020 at 9:05 am #1586185Matic
Ok, I finally understand! It works like a charm now + I can give each post its own background.
Thank you very much 😀
Cheers
December 18, 2020 at 12:18 pm #1586397David
StaffCustomer SupportGlad to be of help!
-
AuthorPosts
- You must be logged in to reply to this topic.