Support

Account

Home Forums Backend Issues (wp-admin) Display repeater in a php as css

Solved

Display repeater in a php as css

  • I’m trying to calling a repearter field in a php file which is my template css.

    I may understand that we need to create a function to make understand ACF what we are taking about. So I made it but I suppose we need an “action” too. Something like:

    <?php
    function Myfunction(){
    if( have_rows('repeater', 'option') ):					
    	while( have_rows('repeater', 'option')): the_row(); 
    		$FieldColor = the_sub_field('color'); 			
    	endwhile; 
    endif; 
    
    }
    add_action('', 'Myfunction');
    
    ?>
     <!-- Begin CSS --!>
    h1 {color: <?php echo $FieldColor; ?>;}

    Which function and add_action may I use to display my field in my style page ?

  • I’m not sure I understand what you’re trying to do.

    What file are you trying to add this code to?

  • hi thank you for your answer 🙂

    Actually I’m making a generic personnal template with customization from the backend; something like in the premium themes.

    So I made a “theme apearance options page”. I added fields like “h1 title” as repeater and “colors, fonts…” as subfields.

    I made a specific css for my “customization theme” but to display the ACF fields I’m using a php file enqueued as stylesheet.
    When I’m enter the value like “$color =’#fff'” and echo it on the css tag it’s working.

    So I think my problem is ACF doesn’t knows which fields i’m calling from this file and I need create a function with an add_action. But may be i’m wrong …

    But I don’t know how resolve it

  • You can’t enqueue a php file normally as a stylesheet and have it work.

    For example, this will not work

    
    wp_enqueue_style('your-handle', 'your-file.php');
    

    the reason for this is not that WP will not enqueue it or add it to your page. This issue is that no WP or plugin function will be available in this file.

    Your 2 choices are to put the custom styles inline in the header of your page or to enqueue the style script using admin-ajax.php

  • Oh thanks,

    So the inline is not the solution in my opinion…

    I may add the script with admin-ajax

    it’s not really about ACF but have you an exemple for the ajax way ?

  • Basically you need to look at this https://codex.wordpress.org/AJAX_in_Plugins

    When you enqueue the script you do it something like this, please note that this is only an example of the values that will be used. You should really look at the document and read about getting the admim ajax url and all of that

    
    wp_enqueue_style('your-handle', '/wp-admin/admin-ajax.php?action=my_custom_css_action');
    

    Then you create an action functin that outputs your dynamic php stylesheet.

  • So I have some difficulties with ajax. With a developper friend, we did like this and it’s working.

    So if someone have the same problem, we did it like this:

    I made a php file “parametres-template-css.php” wich calling acf fields and use:

    <?php
    if( have_rows('repeater', 'option') ):					
    	while( have_rows('repeater', 'option')): the_row(); 
    		$FieldColor = get_sub_field('color'); 			
    	endwhile; 
    endif; 

    the first problem was I used the_sub_field and not get_subfield

    I made my css-php “styles-css.php” file including “parametres-template-css.php” like this:

    <?php require( 'parametres-template-css.php'); ?>
    
    <?php /* ---------------- TITLE ----------------  */ ?>
    
    	h1{ color:  <?php echo $h1titleColor ; ?>;}

    and I included this file in my headers

    <style><?php include("inc/template/css-template/styles-css.php"); ?></style>

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Display repeater in a php as css’ is closed to new replies.