Whoops—I forgot the middle step there. Get the attributes:
$block_color_attrs = get_block_color_attrs( $block );
Here’s how I’m handling block color settings:
This function parses the $block
attributes and settings:
/**
* Get an ACF block's color settings.
*
* @param array $block The block settings and attributes.
*/
function get_block_color_attrs( $block = null ) {
if ( ! $block ) {
return;
}
$block_class = null;
$block_style = null;
if ( $block['backgroundColor'] ) {
$block_class .= ' has-background has-' . $block['backgroundColor'] . '-background-color ';
}
if ( $block['textColor'] ) {
$block_class .= ' has-text-color has-' . $block['textColor'] . '-color ';
}
if ( $block['style']['color']['background'] ) {
$block_class .= ' has-background ';
$block_style .= 'background-color: ' . $block['style']['color']['background'] . ';';
}
if ( $block['style']['color']['text'] ) {
$block_class .= ' has-text-color ';
$block_style .= 'color: ' . $block['style']['color']['text'] . ';';
}
return array(
'class' => $block_class,
'style' => $block_style,
);
}
Then I print that information in the block wrapper element:
<div class="<?php echo esc_attr( $block_color_attrs['class'] ); ?>" style="<?php echo esc_attr( $block_color_attrs['style'] ); ?>">
[...]
</div>
Block color settings are available via the $block
array, but it’s trickier than it seems. First of all, if you’re referencing an array value by key name, the key must be in quotes. E.g.:
$block[textColor]; // <-- THIS *WON'T* WORK.
$block['textColor']; // <-- THIS *WILL* WORK.
The next thing to note is that different $block
settings will be returned depending on whether a predefined named color was picked from the palette or a custom color was picked.
If you pick one of the default palette colors (or if you have set your own palette colors with the add_theme_support('editor-color-palette', array( ... ));
, it will return the slug
of the color (e.g., blue
) instead of a hex-value, and in two separate array keys.
If you pick custom colors, they will be accessible via $block['style']['color']['background']
and $block['style']['color']['text']
.
If you pick two predefined colors:
$block Array => (
...
[backgroundColor] => bright-purple
[textColor] => white
)
If you pick two custom colors:
$block Array => (
...
[style] => Array (
[color] => Array (
[background] => #cccccc
[text] => #aa0000
)
)
)
And if you pick one palette color and one custom color:
$block Array => (
...
[textColor] => black
[style] => Array
(
[color] => Array
(
[background] => #cccccc
)
)
)
I’m not sure what you mean by dynamic footer—can you clarify?
@rashed I’m unclear exactly what the issue is; can you provide a more detailed explanation of what is going wrong? Maybe a screenshot with more context, or multiple screenshots?
@pipoulito You might try entering https://
as the default value and placeholder for a link in the field group editor, rather than this JS solution after the fact.
@rashed You can put this code in your functions.php file, which acts like a plugin that is automatically loaded for the current WordPress theme (more info).
If this problem is happening for you, though, I would definitely recommend starting a support ticket with ACF, since this particular forum post is almost five years old and might be a completely different issue. 🙂
Ah, I’m glad it’s working now—apparently this functionality (or this filter?) is limited to Pro.
I would start a new topic for your other request… I don’t know anything about Buddypress, but the user dropdown wouldn’t search that data unless it saved them as actual WordPress users. Best of luck! 🙂
I just tested this, and that code worked just fine for me on a fresh installation of WordPress and ACF Pro:
Here’s the code I used:
function change_user_acf_result( $result, $user, $field, $post_id ) {
// Empty the string so we can repopulate it.
$result = '';
if ( $user->first_name ) {
$result .= $user->first_name;
}
if ( $user->last_name ) {
$result .= ' ' . $user->last_name;
}
if ( $user->user_email ) {
$result .= ' (' . $user->user_email . ')';
}
return $result;
}
add_filter( 'acf/fields/user/result', 'change_user_acf_result', 10, 4 );
You can see the result in the attached screenshot.
Happy to help, glad you were able to get it working! Have a good one. 🙂
@juliusbangert Looks like you can ignore it with some arguments provided by that plugin: https://www.nsp-code.com/advanced-post-types-order-api/sample-usage. Give that a shot?
It looks like your posts are still being sorted DESC
, and that the menu_order
is also being sorted on first. Do you have any plugins running that might affect this query? Could you try using get_posts()
instead of new WP_Query()
?
@juliusbangert So your posts are still being shown in reverse chronological order even though you’re sorting them by date
ascending? Have you tried print_r()
-ing your $news_query
results?
@hube2 That plugin didn’t actually help; it doesn’t allow for the narrowing of results by taxonomy or custom post type, and it’s been abandoned by the creator. Do you know of any other resources for this?
@bherrero Any chance you could share the code you wrote for this? I’m trying to accomplish the same thing!
@elliot This latest change is working reliably for me now.
@elliot Wait, scratch that—RC2 is no longer fixing this issue. I’m seeing no custom fields when previewing posts.
RC2 is working for me.
I am seeing this issue as well, as are others: https://support.advancedcustomfields.com/forums/topic/preview-solution/page/2/
Great! Glad to be of service!
I’m not sure what you mean—the oEmbed field stores the URL but when you echo it, it returns the appropriate embed code. Do you want to show just the video URL?
This should do the trick:
<?php
$args = array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'banner',
'compare' => '=',
'value' => 1
)
),
'meta_key' => 'start_time'
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
?>
See the documentation here: http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/
You’ll want to query the database using the custom field’s name as a meta key and comparing the values against your current custom field’s value. Try this:
<?php
$current_event_date = get_field('date');
$next_event_query = new WP_Query(array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>',
'value' => $current_event_date,
'type' => 'DATE'
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
if ($next_event_query->have_posts()) while ($next_event_query->have_posts()): $next_event_query->the_post();
// Print information about next event.
endwhile;
?>
Can you provide a bit more code than what you’ve pasted here? It’s difficult to tell what some of those variables are supposed to be returning. For example, the field you mention, catalogo-pdf
, doesn’t appear anywhere in that code.
I expect something like this would do the trick:
<button class="load-video-button" data-video-embed="<?php echo htmlspecialchars(get_field('oembed_video_url')) ?>">Load Video</button>
<div class="video-container"></div>
<script>
$(function() {
$('.load-video-button').click(function() {
$('.video-container').html($(this).data('video-embed'));
});
});
</script>
(I assume you’re using jQuery here.)
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.