Hi, I try to create list of child category with images thumbs.
I add new field to taxonomy “image_category” and now I try to add this image to the list:
<?php
$args = array('parent' => 3);
$categories = get_categories( $args );
$term = get_queried_object();
$image_category = get_field('image_category', $term);
foreach($categories as $category) {
echo '<div class="col-md-3 col-sm-12 col-xs-12 lista-kraje text-center pb-3"><img src="' . $image_category . '"><h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
echo '<p> <strong>Number post:</strong> '. $category->count . '</p></div>';
}
?>
I see category list, name, number post. Only image dont work.
Image link dont show. I have on consol only “img src=(unknown)”. What am I doing wrong?
Is it possible to have some specific repeater fields to display on frontend form. for example look at the “fields” parameter in below example.
$options = array(
'field_groups' => ['group_602a592cf1805'],
'fields' => [
'my_name', //normal ACF field, (this works!)
'test_repeater_field' => ['hello'], //allowing only specific field with in a repeater field, (Not working)
],
'form_attributes' => array(
'method' => 'POST',
'action' => admin_url("admin-post.php"),
),
'html_before_fields' => sprintf(
'<input type="hidden" name="action" value="save_user_fields_form">
<input type="hidden" name="user_id" value="user_%s">',
$user->ID
),
'post_id' => "user_{$user->ID}",
'form' => true,
'html_submit_button' => '<button type="submit" class="button margin-top-20 margin-bottom-20" value="Update Profile">' . __('Submit', 'text-domain') . '</button>',
);
acf_form($options);
The documentation for the Google Map field demonstrates a really nice way to render addresses using the address entered into the field.
<?php
$location = get_field('location');
if( $location ) {
// Loop over segments and construct HTML.
$address = '';
foreach( array('street_number', 'street_name', 'city', 'state', 'post_code', 'country') as $i => $k ) {
if( isset( $location[ $k ] ) ) {
$address .= sprintf( '<span class="segment-%s">%s</span>, ', $k, $location[ $k ] );
}
}
// Trim trailing comma.
$address = trim( $address, ', ' );
// Display HTML.
echo '<p>' . $address . '.</p>';
}
For my project I am working with small towns in the UK so city, state and country are not relevant. Instead, I would like to use postal_town and administrative_area_level_2 in place of these which should render the town and county respectively. So for the foreach I have
foreach( array( 'street_number', 'street_name', 'postal_town', 'administrative_area_level_2', 'post_code' ) as $i => $k ) { ... }
I had assumed that this would return: number, street, town, county, post-code. But the postal_town and administrative_area_level_2 both return nothing.
Printing the array keys using <?php print_r(array_keys($location)); ?>
appears to show only a limited set of options.
I could as a compromise just use <?php echo $location['address']; ?>
but this gives a slightly different version of the address (Business name, street, town, country), but it’s not quite what I’m after.
Does anyone know how would I go about getting my chosen values to render to the front end without having to resort to a separate address input field?
Hello.
I have registered a post type and i want to have posts on it with an acf form.
The field group contains only one field. The field is file upload field.
The connection with post type and field works ok because i tried make post from dashboard and the field is on the editor of post type.
When i click the submit button the form works ok but there is not a post in my custom post type.
What’s the problem?
I send you the custom code if you want to help.
Thank you.
<?php acf_form_head();
get_header();
?>
<div class="container">
<?php
$settings = array(
'id' => 'photos_form',
'field_groups' => array("group_6025117548b3f"),
'fields' => false,
'submit_value' => __("Submit", 'acf'),
'label_placement' => 'top',
'html_submit_button' => '<input type="submit" id="submitbtn" class="acf-button button primary_btn" value="%s" />',
'html_submit_spinner' => '<span class="acf-spinner"></span>',
'updated_message' => '',
'html_after_fields' => '',
);
acf_form($settings);?>
</div>
<?php get_footer(); ?>
Hi, i have form to add new taxonomy and i also need template with form for editing this taxonomy so have for editing taxonomy next form but problem is that i get all value for custom field but for title of taxonomy i didnt get.
<?php acf_form(array(
'post_id' => $_GET["post"],
'post_title' => true,
'field_groups' => array(
'group_5f475552ca375'
),
'submit_value' => 'Shrani',
'html_submit_button' => '<input type="submit" class="acf-button btn btn-primary" value="%s" />',
'uploader' => 'wp',
));
?>
$_GET[“post”] i get from url parameter and the post title input is vissible but it is empty
Hi i want to save a new taxonomy, but i cant get the form to frontend.
Custom field are sucess visible in backend but on frontend i cant see, i think becuse i dont have right code. My custom taxonomy name is “narocilnice” and code is:
<?php acf_form(array(
'id' => 'acf-form',
'post_id' => true,
'post_title' => true,
'new_post' => array(
'tax_name' => 'narocilnice',
),
'submit_value' => 'Shrani',
'html_submit_button' => '<input type="submit" class="acf-button btn btn-primary" value="%s" />',
'uploader' => 'wp'
));
?>
On this picture you can see that i get on backend custom field but on frontend i can’t. So i need add new taxonomy to custom taxonomy name “narocilnice” not new post to custom post_type.
Can someone tell me what is wrong?
I am using the action restrict_manage_posts
to create a custom filter for my posts:
add_action( 'restrict_manage_posts', '_s_custom_filter' );
function kiewit_locations_custom_filter($post_type){
if ($post_type === 'custom_post_type'){
$args = array(
'post_type' => 'custom_post_type',
'parent' => 0
);
$items = get_pages( $args );
?>
<select id="filter_by_custom_post_type" name="filter_by_custom_post_type">
<option value=""><?php _e('All Custom Posts', '_s'); ?></option>
<?php
$current_v = isset($_GET['filter_by_custom_post_type']) ? $_GET['filter_by_custom_post_type'] : '';
foreach( $items as $item ) {
printf
(
'<option value="%s"%s>%s</option>',
$item->post_name,
$item->post_name == $current_v? ' selected="selected"':'',
$item->post_title
);
}
?>
</select>
<?php
}
}
I also have custom columns set up on my custom_post_type
to display selected meta data from a select
field and try to display that data using get_field_object('custom_select_information');
, however, that always returns false, even if I use the field key.
Any ideas how to get that custom_select_information
to display when filtering my posts?
Hi
I have a query I would like to resolve.
The scenario is as follows:
I have a custom post type called Auction.
Auctions have an acf in relation to a custom post type product.
Those products can be assigned taxonomies.
My question is how to sort those products by taxonomy when I’m on the single-auction.php page.
Following the documentation I can show all the products of an auction, but I would like to sort those products by their taxonomy.
An image to clarify what I want to get :
A brief explanation:
Taxonomy Term A
Product 1
Product 2
Taxonomy Term B
Product 3
Product 4
My code :
$featured_posts = get_field(‘products_in_this_auction’);
(Note: get_field(‘products_in_this_auction’) is a Post Object)
This is a post object and contains the ids of the products that are linked to the auction.
But my problem is how to get those products sorted by taxonomy.
I guess I have to use tax_query for my
query.
I have an old code that shows me all the custom post type products ordered by their taxonomy, as seen in the image, but what I need is to show only the custom post type products that depend on the current auction.
I know those custom post type are in $featured_posts = get_field(‘products_in_this_auction’); but I don’t know how to get them.
My code, something old-fashioned, is the following.
$categories = get_terms('type_produc', 'orderby=count&order=DESC&hide_empty=1');
foreach( $categories as $category ):
?>
<h3 class="title_pru"><?php echo $category->name; // Prints the cat/taxonomy group title ?></h3>
<?php
$posts = get_posts(array(
'post_type' => 'myproduct',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true,
));
foreach($posts as $post):
setup_postdata($post);
?>
<div id="post-<?php the_ID(); ?>">
<?php the_title( sprintf( '<p class="title_pru"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></p>' ); ?>
</div><!-- #post-## -->
<?php endforeach; ?>
<?php endforeach; ?>
This code shows the result of the image, but it doesn’t work for me.
I hope someone can help.
Thank you very much.
Hi,
I’m trying to make hero section where one column has slick slider (repeater field with images) and second one with innerBlocks.
Problem:
When I edit slider in any way (change order of sub_fields/ add new/ remove one) block preview slick slider can’t re-initialize.
If I comment out ‘jsx’ or change to ‘false’ – slider is working as expect.
In demo version i just simplify everything and copy-paste from code examples – same effect:
functions.php
add_action('acf/init', 'my_acf_init_blocks');
function my_acf_init_blocks() {
// Check function exists.
if( function_exists('acf_register_block_type') ) {
// Register a restricted block.
acf_register_block_type(array(
'name' => 'test',
'title' => 'Test',
'description' => 'A test content block.',
'category' => 'formatting',
'mode' => 'preview',
'supports' => array(
'align' => true,
'mode' => false,
'jsx' => true
),
'enqueue_assets' => function(){
$theme = wp_get_theme( );
$ver = $theme->version;
wp_enqueue_style( 'slick', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.css', array(), $ver );
wp_enqueue_style( 'slick-theme', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick-theme.css', array(), $ver);
wp_enqueue_script( 'slick', 'https://cdn.jsdelivr.net/npm/[email protected]/slick/slick.min.js', array('jquery'), $ver, true );
wp_enqueue_style( 'test-css', get_template_directory_uri() . '/test.css', array(''), $ver, 'all' );
wp_enqueue_script( 'test-js', get_template_directory_uri() . '/test.js', array('jquery'), $ver, true );
},
'render_template' => 'test.php',
));
}
}
test.php
$classes = '';
if( !empty($block['className']) ) {
$classes .= sprintf( ' %s', $block['className'] );
}
if( !empty($block['align']) ) {
$classes .= sprintf( ' align%s', $block['align'] );
} ?>
<div class="test <?php echo esc_attr($classes); ?>">
<div class="test-col">
<InnerBlocks />
</div>
<div class="test-col slider">
<?php if( have_rows('slides') ): ?>
<div class="slides">
<?php while( have_rows('slides') ): the_row();
$image = get_sub_field('image');
?>
<div>
<?php echo wp_get_attachment_image( $image['id'], 'full' ); ?>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<p>Please add some slides.</p>
<?php endif; ?>
</div>
</div>
test.js
(function($){
var initializeBlock = function( $block ) {
$block.find('.slides').slick({
dots: true,
infinite: true,
speed: 300,
slidesToShow: 1,
centerMode: true,
variableWidth: true,
adaptiveHeight: true,
focusOnSelect: true
});
}
// Initialize each block on page load (front end).
$(document).ready(function(){
$('.slider').each(function(){
initializeBlock( $(this) );
});
});
// Initialize dynamic block preview (editor).
if( window.acf ) {
window.acf.addAction( 'render_block_preview/type=test', initializeBlock );
}
})(jQuery);
As alternative option from formums: test.js
(function($){
var initializeBlock = function( $block ) {
function getSliderSettings() {
dots: true,
infinite: true,
speed: 300,
slidesToShow: 1,
centerMode: true,
variableWidth: true,
adaptiveHeight: true,
focusOnSelect: true
}
$block.find('.slides').slick( getSliderSettings() );
}
// Initialize each block on page load (front end).
$(document).ready(function(){
$('.slider').each(function(){
initializeBlock( $(this) );
});
});
// Initialize dynamic block preview (editor).
if( window.acf ) {
window.acf.addAction( 'render_block_preview/type=test', initializeBlock );
}
})(jQuery);
I believe there’s a solution to reint slick in better wahy, but maybe it’s just bug?
Hi all.
I’m building a website that is for booking events, the e-commerce platform is woocommerce. I’ve acted an action to add a new page to the user account called family, where I want the user to be able to manage their family members and contact info.
So far I’ve added the page, created a repeater for user where they can add a family member and fill in that members information, it saves and all works great.
Now I’d like to add an ACF group before the repeater that has main information such as emergency contact details and next of kin, but I can’t figure out how to display two fields (the group and the repeater) within the one action and only have one save button to save the details to the users account.
This is what I have so far that works for the repeater, but if anyone could steer me in the right direction as to how to add the group field, it’d be hugely appreciated.
<?php
acf_form_head();
get_header();
?>
Family
<div class="family-members-form"><?php
if ( !is_user_logged_in() ){
echo 'You are not logged in. <br /> <a href="' . get_permalink(31) .'">Log In →</a>';
} else {
$user = wp_get_current_user();
$options = array(
// 'field_groups' => ['group_5cbd99ef0f584'],
'fields' => ['field_5f24194f719ca'],
'form_attributes' => array(
'method' => 'POST',
'action' => admin_url("admin-post.php"),
),
'html_before_fields' => sprintf(
'<input type="hidden" name="action" value="adaptiveweb_save_profile_form">
<input type="hidden" name="user_id" value="user_%s">',
$user->ID
),
'post_id' => "user_{$user->ID}",
'form' => true,
'html_submit_button' => '<button type="submit" class="acf-button button button-primary button-large" value="Update Profile">Update Profile</button>',
);
acf_form($options);
}
?>
</div>
Hey guys.
I wanted to understand the new beta features, the innerblocks. So I copied the Code from here:
https://www.advancedcustomfields.com/blog/acf-5-9-exciting-new-features/
but nothing is working.
function.php
require get_template_directory() . ‘/enqueue/enqueue_blocks.php’;
enqueue_blocks.php
<?php
/*Test*/
add_action('acf/init', 'my_acf_blocks_init');
function my_acf_blocks_init() {
// Check function exists.
if( function_exists('acf_register_block_type') ) {
// Register a restricted block.
acf_register_block_type(array(
'name' => 'restricted',
'title' => 'Restricted',
'description' => 'A restricted content block.',
'category' => 'formatting',
'mode' => 'preview',
'supports' => array(
'__experimental_jsx' => true
),
'render_template' => get_template_directory_uri() . '/template-parts/blocks/restricted/restricted.php',
'enqueue_style' => get_template_directory_uri() . '/template-parts/blocks/restricted/restricted.css'
));
}
}
restricted.php
<?php
/**
* Restricted Block Template.
*
* @param array $block The block settings and attributes.
* @param string $content The block inner HTML (empty).
* @param bool $is_preview True during AJAX preview.
* @param (int|string) $post_id The post ID this block is saved to.
*/
// Create class attribute allowing for custom "className" and "align" values.
$classes = '';
if( !empty($block['className']) ) {
$classes .= sprintf( ' %s', $block['className'] );
}
if( !empty($block['align']) ) {
$classes .= sprintf( ' align%s', $block['align'] );
}
// Load custom field values.
$start_date = get_field('start_date');
$end_date = get_field('end_date');
// Restrict block output (front-end only).
if( !$is_preview ) {
$now = time();
if( $start_date && strtotime($start_date) > $now ) {
echo sprintf( 'Content restricted until %s. Please check back later.
', $start_date );
return;
}
if( $end_date && strtotime($end_date) < $now ) {
echo sprintf( 'Content expired on %s.
', $end_date );
return;
}
}
// Define notification message shown when editing.
if( $start_date && $end_date ) {
$notification = sprintf( 'Content visible from %s until %s.', $start_date, $end_date );
} elseif( $start_date ) {
$notification = sprintf( 'Content visible from %s.', $start_date );
} elseif( $end_date ) {
$notification = sprintf( 'Content visible until %s.', $end_date );
} else {
$notification = 'Content unrestricted.';
}
?>
<?php echo var_dump($start_date);?>
<div class="restricted-block <?php echo esc_attr($classes); ?>">
Test
<span class="restricted-block-notification"><?php echo esc_html( $notification ); ?></span>
<InnerBlocks />
</div>
restricted.css
.restricted-block{
padding: 15px;
background-color: #0c5460;
border: 12px solid #20c997;
}
.restricted-block-notification
{
background-color: #43494e;
padding: 5px;
float: left;
color: white;
}
I checked the Path – as I saw, the restricted.css file is loading. (network analysis, 304) Also the Data-Blocks is showing up on the right side.
But the var-dump doesnt show anything and even when I messed up the .php, there doesn’t come a fatal error. But the Path is the Same as the CSS.
I also made sure, that I downloaded the beta-version.
so… what i am doing wrong?