ACF – bbird.me http://bbird.me WordPress quick tips Wed, 08 Aug 2018 16:39:07 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.8 How to list taxonomy values in Advanced Custom Fields http://bbird.me/how-to-list-taxonomy-values-in-advanced-custom-fields/ http://bbird.me/how-to-list-taxonomy-values-in-advanced-custom-fields/#respond Thu, 18 Feb 2016 15:56:19 +0000 http://bbird.me/?p=350 Advanced Custom Fields plugin provides fields for dynamic data – one of them is Taxonomy Field Type. If you would

Post How to list taxonomy values in Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
Advanced Custom Fields plugin provides fields for dynamic data – one of them is Taxonomy Field Type.

taxonomies

If you would like to list the values the user selects, you could try doing it with this code.

<?php

$terms = get_field('custom_taxonomies');
if ($terms) {
    echo '<h1>Taxonomies:</h1>
    <p>';
    $term_names = array();
    foreach ($terms as $term) {
        $term_names[] = $term->name;
    }
    echo implode(' | ', $term_names),'</p>';
}

?>

 

Post How to list taxonomy values in Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
http://bbird.me/how-to-list-taxonomy-values-in-advanced-custom-fields/feed/ 0
Adding custom CSS class in body_class() using Advanced Custom Fields http://bbird.me/adding-custom-css-class-on-body_class-using-advanced-custom-fields/ http://bbird.me/adding-custom-css-class-on-body_class-using-advanced-custom-fields/#respond Wed, 17 Feb 2016 15:41:52 +0000 http://bbird.me/?p=342 The great thing about Advanced Custom Fields is that by using that plugin, you can replace number of other plugin reducing

Post Adding custom CSS class in body_class() using Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
The great thing about Advanced Custom Fields is that by using that plugin, you can replace number of other plugin reducing the page load and increasing the site sped, while achieving the same result only by using custom fields. WordPress function body_class() is one of the functions that is basically used by default by theme developers and could easily have the same importance as wp_head() or wp_footer(). Now let’s imagine this scenario: we create a custom field of Select type with several choices – could be colors – and we want to style posts or pages or whatever based on the color we choose.

custom colors

Having that said, assuming that CSS has already been prepared, all we need to do is to take the users’ choice and use it in the following way:

add_filter('body_class', 'add_acf_body_class');

function add_acf_body_class($class) {
    $field = get_field_object('custom_color', get_queried_object_id());
    $value = get_field('custom_color');
    $label = $field['choices'][$value];
    $class[] = $label;
    return $class;
}

You could add this into function.php or a custom plugin containing functions, whichever method you prefer. In order to get the current post ID, get_queried_object_id () is being used. We are also using get_field_object() to retrieve the color value.

Post Adding custom CSS class in body_class() using Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
http://bbird.me/adding-custom-css-class-on-body_class-using-advanced-custom-fields/feed/ 0
How to render shortcode using Advanced Custom Fields http://bbird.me/how-to-render-shortcode-using-advanced-custom-fields/ http://bbird.me/how-to-render-shortcode-using-advanced-custom-fields/#comments Tue, 16 Feb 2016 12:08:04 +0000 http://bbird.me/?p=337 As you know, WordPress function do_shortcode() will render whatever shortcode contains and is frequently used in custom templates. But if

Post How to render shortcode using Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
As you know, WordPress function do_shortcode() will render whatever shortcode contains and is frequently used in custom templates. But if you want to allow site owners to update shortcodes easily, without messing with template files (and possibly causing PHP errors and white screens of death), you will want to consider using Advanced Custom Fields. All you need to do this to create custom field of text Field Type and then paste your shortcode inside. It could look like this:

revolution slider

In order to render the shortcode, you could do something like this (this goes into template files):

<?php 
  $revslider_shortcode = get_field( 'revolution_slider_shortcode' ); 
  echo do_shortcode($revslider_shortcode);
?>

So the idea is to get the field value using get_field(), store it into variable and then render it using do_shortcode(). Also note the use of echo.

Post How to render shortcode using Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
http://bbird.me/how-to-render-shortcode-using-advanced-custom-fields/feed/ 1
How to set conditional logic in Advanced Custom Fields http://bbird.me/how-to-set-conditional-logic-in-advanced-custom-fields/ http://bbird.me/how-to-set-conditional-logic-in-advanced-custom-fields/#respond Mon, 15 Feb 2016 20:34:04 +0000 http://bbird.me/?p=321 If you tried to set conditional logic using Advanced Custom Fields plugin, but haven't done it before, there's a chance that you will stumble upon the following message "No toggle fields available" wondering what that means. Fortunately, it is very easy to set this up. In order to add toggle fields, you would only need to create a new custom field of "choice" Field Type and add few Choices.

Post How to set conditional logic in Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
If you tried to set conditional logic using Advanced Custom Fields plugin, but haven’t done it before, there’s a chance that you will stumble upon the following message “No toggle fields available” wondering what that means. Fortunately, it is very easy to set this up. In order to add toggle fields, you would only need to create a new custom field of “choice” Field Type and add few Choices.

custom fields1

For this particular example, I used “Select” choice, creating select custom field type. Into Choices, let’s add few options:

  • none
  • first choice
  • second choice

The third step is to create few custom fields and attach the conditional logic on them. It isn’t important at this point  what kind of fields do we have, as it could be  anything. You could set one custom field to show the post URL, and the other one to show custom URL.

custom fields 2

But how would you display custom fields depending on the choice being made? As you probably know, the_field() will display custom field value, but we can also find some use of get_field() here. This is how we could write the PHP function:

<?php
if ( 'none' == get_field('custom_url_toggle')){
    //do nothing
} elseif ( 'first choice' == get_field('custom_url_toggle') ) {
    the_field("custom_url");

} elseif ( 'second choice' == get_field('custom_url_toggle') ) {
   the_field("regular_url");
} 
?>

This function will display nothing if “none” is selected, it will display the content of custom_url field if “first choice”is selected and  regular_url value if the “second choice”is selected.

Post How to set conditional logic in Advanced Custom Fields je prvi puta viđen na bbird.me.

]]>
http://bbird.me/how-to-set-conditional-logic-in-advanced-custom-fields/feed/ 0