Announcement

Collapse
No announcement yet.

ACF fields implemetation - reaching out for PHP pro´s

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • ACF fields implemetation - reaching out for PHP pro´s

    Searching help from PHP/ACF pro´s as recently I´m trying to get the grips of working with custom fields - but the problem is my lacking PHP knowledge, I admit....
    Get some fields in a hook is not the problem, but how do I check if they´re populated and - if not - do something else (or don´t display any HTML)?

    The goal: My customer loves blockquotes. She´d like to have different blockquotes for each page so I set up ACF fields to make it easy for her to add or change them. Got two fields: zitat and autor (blockquote and author).
    I´d like to check if those fields are populated or - if they´re not - display a default blockquote instead (to have a consistent look).

    Searched a lot, gathered some code snippets. Got some ingredients but no meal yet.

    Could somebody please tell me if this makes any sense yet or how to do it better?:

    PHP Code:
    add_action'genesis_after_header''add_blockquote' );
    function 
    add_blockquote() {
            
    // Return early if it´s not a page
            
    if ( !is_page() )
                    return;

            
    // Get the fields
            
    $zitate = array(
                    
    'zitat' => get_field'zitat' ),
                    
    'autor' => get_field'autor' ),
            );
     
            if ( 
    $zitate ) {
                    echo 
    '<div class="whatever">';
                    echo 
    '<p class="blockquote">' esc_attr$zitate['zitat'] ) . '</p><br><p class="author">' esc_attr$zitate['autor'] ) . '</p>';
                    echo 
    '</div>';
            }

                else
     
                    echo 
    '<div class="whatever">';
                    echo 
    '<p class="blockquote">Some default blockquote here</p><br><p class="author">An author</p>';
                    echo 
    '</div>';


    Last edited by SiGa; 03-15-2014, 11:46 AM.

  • #2
    Jay! Seems I finally got it working now.
    I wrapped the blockquotes into the featured top divs, that way I can use the Dynamik settings for most of the styling. Still got to change the cite tags for a more HTML5 approach but that won´t be the problem now that it´s working.

    Still, if you see a better/more effective way to do it, please tell me.

    PHP Code:
    add_action'genesis_after_header''add_blockquote'15 );  //priority only needed if another element already uses the same hook
    function add_blockquote() {
            
            
    // Return early if not a page - uncomment to use optional conditions
            // if ( !is_page() )
                   // return;

            // Store the data
            
    $zitate = array(
                    
    'zitat' => get_field'zitat' ),
                    
    'autor' => get_field'autor' ),
            );
     
       
    // If the fields are empty, display the default blockquote
           
    if(
        ( 
    false === get_field('zitat') ) &&
        ( 
    false === get_field('autor') )  

          )
     
                    echo 
    '<div id="ez-feature-top-container-wrap" class="clearfix"><div id="ez-feature-top-container" class="clearfix">
                    <div id="ez-feature-top-1" class="widget-area ez-widget-area"><blockquote>
                    The default text here
                    <cite>The author</cite></blockquote></div></div></div>'
    ;
          
          
    // Else get the data from the fields and display it
            
    else  
                    echo 
    '<div id="ez-feature-top-container-wrap" class="clearfix"><div id="ez-feature-top-container" class="clearfix">
                    <div id="ez-feature-top-1" class="widget-area ez-widget-area"><blockquote>'
                     
    esc_attr$zitate['zitat'] ) . '<cite>' esc_attr$zitate['autor'] ) . '
                     </cite></blockquote></div></div></div>'
    ;
            



    Here´s the thread that brought me on track: http://wordpress.stackexchange.com/q...ty-fields?rq=1
    Last edited by SiGa; 03-15-2014, 11:51 AM.

    Comment


    • #3
      Hi SiGa,

      I would like to talk to you about this. But the subject seems to be to complicated for me. I am afraid that I don't even know what is the way we can use ACF fields.




      Regards,
      Robert Gabriel


      Last edited by robert; 10-30-2016, 11:21 PM.

      Comment


      • #4
        I´m only starting out with custom fields, as you see - and still a long way to go, but very much worth it.
        The fields come in handy if you´d like to have different content for different posts/pages. Can be exhausting to create many different widget areas / labels / conditionals / hooks for that task if you got a large amount of different content. Fields make that easier and also you won´t have your customer deal with all those widget areas (where they need special user roles for, too), but you can provide some simple fill-it-out-fields they will see whenever they edit a page or post.

        So the basics for fields are
        - Install the plugin "Advanced Custom Fields" (to begin with - it´s free, but there are others, too), create a field group and a simple text field. (Good tutorials out there, but quite self-explaining)
        - In the settings below the field(s), choose where to display it/them (on posts, on pages, on a certain page....). Save.
        - You will now be able to see the field in Edit - Page (or posts), but not yet on frontend.
        - Go to Dynamik Custom - Hooks, choose a hook (you can add labels or conditionals here, too) and put in a simple call for the field, like:

        PHP Code:
        <p><?php the_field('field_name'); ?></p>
        and save. You will now see the content of the field displayed where you hooked it. You can also use the code for the fields in your custom templates and in your functions (as I´ve done above).
        Just try it - the documentation is here: http://www.advancedcustomfields.com/...code-examples/
        And there are many code snippets out there if you just start investigating for Wordpress or Genesis. It´s surely worth it!
        Last edited by SiGa; 03-15-2014, 03:29 PM.

        Comment


        • #5
          Thank you SiGa for a short explanation. I definitely have to invest more time to get more information about this.





          Regards,
          Robert Gabriel


          Last edited by robert; 10-30-2016, 11:21 PM.

          Comment

          Working...
          X