braddalton Posted January 15, 2022 Share Posted January 15, 2022 (edited) I've tried everything but cannot get this to work : $cats = str_replace( '<a', '<a style="color: ' . $cat_color . ' "', get_the_category_list( ', ' ), $cats ); This doesn't work either : $cats = str_replace( '<a', '<a style="color:{$cat_color}"', get_the_category_list( ', ' ), $cats ); The result i get is this : element.style { color: ($color); } Edited January 15, 2022 by braddalton Quote Link to comment https://forums.phpfreaks.com/topic/314411-use-php-variable-as-value-for-inline-style-property/ Share on other sites More sharing options...
Barand Posted January 15, 2022 Share Posted January 15, 2022 Use outer double quotes to insert a variable into a string. $cat_list = "<a href='blah'>cat 1</a>, <a href='yadayada'>cat 2</a>"; $cat_color = '#8F1FCF'; $cat_list = str_replace('<a', "<a style='color:$cat_color'", $cat_list); Quote Link to comment https://forums.phpfreaks.com/topic/314411-use-php-variable-as-value-for-inline-style-property/#findComment-1593337 Share on other sites More sharing options...
braddalton Posted January 15, 2022 Author Share Posted January 15, 2022 (edited) Works great when posts only contain 1 category link. Thanks. The only problem i have now is the value for the first category is used to style every category link when the post contains more than 1 category. $category_id = get_the_category( get_the_ID()); $cat_id = get_category($category_id[0]->term_id); $color = get_field( 'cat_color', $cat_id ); $cats = str_replace( '<a', "<a style='color:$color'", $list ); Should i start a new thread for this 2nd problem Edited January 15, 2022 by braddalton Quote Link to comment https://forums.phpfreaks.com/topic/314411-use-php-variable-as-value-for-inline-style-property/#findComment-1593342 Share on other sites More sharing options...
Barand Posted January 15, 2022 Share Posted January 15, 2022 If you do, tell us what your real problem is. At present you are telling us a solution that isn't working - because it's the wrong solution, perhaps? Quote Link to comment https://forums.phpfreaks.com/topic/314411-use-php-variable-as-value-for-inline-style-property/#findComment-1593343 Share on other sites More sharing options...
braddalton Posted January 16, 2022 Author Share Posted January 16, 2022 Yes, my problem is i can't work out how to use the value foreach custom field to color each category link when the post is assigned to multiple categories. I think i need to use a foreach loop with an indexed Array with the category id foreach value returned as a string. Or an associative Array so i can assign a category id foreach custom field value and return an array of colors which returns this array on the first post in the loop array (size=4) 'red' => int 0 'green' => int 0 'blue' => int 0 'alpha' => float 1 And this on the 2nd array () 'red' => int 221 'green' => int 153 'blue' => int 51 'alpha' => float 1 $list = get_the_category_list(); $category_id = get_the_category( get_the_ID()); $id = get_category($category_id[0]->term_id); $color = get_field( 'cat_color', $id ); foreach( $id as $color ) { $cats = str_replace( '<a', "<a style='color:$color'", $list ); } Quote Link to comment https://forums.phpfreaks.com/topic/314411-use-php-variable-as-value-for-inline-style-property/#findComment-1593355 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.