Support

Account

Forum Replies Created

  • @amjad

    
    $new_value = 'my new value';
    
    if ( $new_value !== get_field( 'field_slug' ) ) {
        update_field( 'field_slug', $new_value ); /* update field */
    }
    
  • It’s an older question, but since I was looking for a solution myself, here is mine:

    
    $textdomain = 'textdomain';
    
    $code = preg_replace( '/\'label\' => (.{3,})\,/', '\'label\' => esc_html_x( $1, \'ACF ' . $field_group['title'] . ' Label\', \'' . $textdomain . '\' ),', $code );
    $code = preg_replace( '/\'instructions\' => (.{3,})\,/', '\'instructions\' => esc_html_x( $1, \'ACF ' . $field_group['title'] . ' Instructions\', \'' . $textdomain . '\' ),', $code );
    $code = preg_replace( '/\'append\' => (.{3,})\,/', '\'append\' => esc_html_x( $1, \'ACF ' . $field_group['title'] . ' Append\', \'' . $textdomain . '\' ),', $code );
    $code = preg_replace( '/\'prepend\' => (.{3,})\,/', '\'prepend\' => esc_html_x( $1, \'ACF ' . $field_group['title'] . ' Prepend\', \'' . $textdomain . '\' ),', $code );
    


    @kokers
    you can use (.{3,}) instead of (.*). Then only matches with 3 or more chars are evaluated.

  • ACF has nothing to do with SEO. Whether and how Google captures the site is determined by how you design your HTML and the fields you fill with ACF.

    Take a look into On-Page SEO.

  • You can simply use this:

    // your repeater
    $items = get_field('competition_schedule');
    
    if( $items ) {
      foreach( $items as $item ) {
        echo $item['band_score'];
      }
    }
  • That’s why you’re a Lead Web Developer! 🙂

  • You can change the layout… just look in your acf field groups, search for your group, edit it and scroll down…

  • Check this out:

    $args = array(
      'post_type' => 'product', /* product post type */
      'posts_per_page' => -1 /* all products */
    );
    
    $products = new WP_Query( $args );
    
    // check if products exists
    if( $products->have_posts() ) {
    
      // loop products
      while( $products->have_posts() ): $products->the_post();
    
      // check if field exists
      if( !get_field( 'field_slug' ) ) {
        update_field( 'field_slug' ); /* update field */
      }
      
      endwhile; 
    
      // reset query to default query
      wp_reset_postdata();
    
    }
  • Its better to create an options page for this and receive you values via get_field( 'field_slug', 'option' );

    On your index.php you’re receiving all your posts, so the index.php has no post id. You can create an page template (with custom post query for your posts) and working with this, but an option page is easier for this.

  • What exactly did you do? Some details would be helpful!

  • <?php the_field(); ?> does not need an echo. (the_field())

    Ah, you need to take get_field_object() for getting the class.

  • Check out global $wp_query; and than print_r( $wp_query );… than you get the global object.

    You also can use query vars and something like this:

    <?php
    
    $arguments = array(
      'post_type' => 'artists',
      'name' => get_query_var( 'name' )
    );
    
    $artists = new WP_Query( $arguments );
    
    if( $artists->have_posts() ):
    
    while( $artists->have_posts() ): $artists->the_post();
    
    // your code
    
    endwhile; wp_reset_postdata();
    
    endif;
  • Then your query is correct. But if you want to query individual artists, you have to use a single-artists.php.

    Another tip: If you name your variable $works and not $posts then it’s easier to read:

    <?php 
    
    $works = get_field( 'featured_works' );
    
    if( $works ):
    
    foreach( $works as $work ):
    
    echo get_the_permalink( $work->ID );
    
    endforeach;
    
    else:
    
    echo sprintf( '<p>%s</p>', __('No work here...') );
    
    endif;

    In your field ‘featured_works’ you have an object with work?

  • Is that correct? <?php echo $post->post_name; ?> $post? 🙂

    And what is the name of the file? Is it an archive-CPT_SLUG.php file?

  • Why not making some “From” and “Till” values (as default) for the selects and setting the fields as required?

    If you need jQuery, you should use the simple selects, not the data selects. Otherwise, you can try something like this:

    (function($) {
     $('select.field-class').find('option').first().text('From');
     $('select.field-class').find('option').first().text('Till');
    })(jQuery)

    Useful link:
    Adding custom javascript to fields

  • function custom_save_date( $post_id ) 
    {
    	// get user from user field
    	$user = get_field( 'user', $post_id );
    	
    	// get user id from user field
    	$user_id = $user['ID'];
    
    	// get date changed field
    	$date_changed = get_field( 'date_de_fin_dabonnement', $post_id );
    	$date = new DateTime( $data_changed );
    	
    	// set format
    	$date = $date->format('d/m/Y');
    	
    	// update user field
    	update_field( 'date_de_fin_dabonnement', $date, 'user_' . $user_id );
    }
    // acf save post hook
    add_action('acf/save_post', 'custom_save_date', 15);

    ACF has its own hook. You can use them for your fields. Also, you forgot the global $post as well as the user in your example. I have not tested the code AND sorry for my english! 🙂

    If you using an other output from user field, just change the output in line 7 and 17.

    Useful links:
    acf/save_post
    ACF user field (first answer from John)

  • You can build an HTML form and then save things in a repeat box within the event, for example. Then you have to be notified, so it’s worth taking a look at wp_mail().

    Usefull links:
    update_field()
    PHP 5 Form Handling
    global $post;

  • First of all, you can use the_field() and you should check if a field exists. Second change all quotation marks… use ‘ or use ” …

    <?php
    $args = array(
    	'post_type' => 'products', // post type slug
    	'post_status' => 'publish', // post status
    	'posts_per_page' => '10' // if not set, you can use the wp-admin settings -> read
    );
    
    // posts per page are only showing 10 posts here... you need to setup an pagination, if you want to show more!
    
    $products_loop = new WP_Query( $args );
    
    if( $products_loop->have_posts() ) :
    	while ( $products_loop->have_posts() ) : $products_loop->the_post();
    	?>
    	<div class="product">
    	
    		<!-- two times the same image? just place the post thumbnail, if exists (wordpress default, no acf) -->
    		<?php if( has_post_thumbnail() ): ?>	
    		
    		<!-- the post thumbnail (wordpress default, no acf) -->
    		<?php the_post_thumbnail(); ?>		
    		<?php endif; ?>
    		
    		<!-- post title, name (wordpress default, no acf) -->
    		<h2><?php the_title(); ?></h2>
    		
    		<!-- two times the same image? just place the acf image here -->		
    		<?php if( get_field( 'product_image' ) ): ?>
    		<img src="<?php the_field( 'product_image' ); ?>" alt="product-detail" class="product-detail align-right" />
    		<?php endif; ?>
    		
    		<!-- post content (wordpress default, no acf) -->
    		<?php the_content(); ?>
    		
    		<!-- download field is filled or exists? -->
    		<?php if( get_field('download') ): ?>
    		<p><a href="<?php the_field('download'); ?>" target="_blank" name="Spec Sheet">Download Spec Sheet</a></p>
    		<?php endif; ?>
    		
    	</div>
    	<?php
    	endwhile;	
    	wp_reset_postdata();	
    endif;

    And if you create an image field with ACF, you can choose how something is output. So you can decide if an array() or the direct image path is output. If you choose an array(), you can also use the alt tags of the image.

    Usefull links:
    has_post_thumbnail()
    the_post_thumbnail()
    the_field()
    get_field()
    PHP: Strings

  • You can define the output value of the checkboxes. Did you choose “output as array”?

    Also important: Are you using the get_field() within a WordPress loop? If not, you have to enter a post id. (get_field)

  • You can have a look at my question and the answer. It’s basically the same thing. You can then retrieve the changed values with ajax, if you put the get_field() in the ajax action.

  • // current post
    global $post;
    
    // args for query
    $query_args = array(
    	'post_type' => 'performers', // performers post_type (or use WP_User_query  for users)
    	'meta_query' => array(
    		array(
    			// performers match field slug
    			'key' => 'event_id', // field slug acf (use post object field and return id)
    			'value' => $post->ID // current post id
    		)
    	)
    );
    
    // set wp  query
    $query = new WP_Query( $query_args );
    
    // query have posts
    if( $query->have_posts() ):
    
    	// loop posts (performers)
    	while( $query->have_posts() ): $query->the_post();
    
    		// update fields you like
    		// field_slug, field_value, current perfomers id
    		update_field( 'YOUR_FIELD_SLUG', 'YOU_FIELD_VALUE', get_the_ID() );
    
    	// end loop
    	endwhile;
    
    	// reset query
    	wp_reset_postdata();
    
    // end
    endif;

    I do not know if I understood you correctly, but I’m thinking (depending on your own needs) that you should reach your goal.

    You can also put that into a function and then use it the way you need it everywhere.

    If you want to do this via / wp-admin, you should look at acf/save_post and combine it.

    usefull links:
    WP_Query
    WP_User_Query
    acf/save_post

  • // if no image, return null
    if( $image == null )	
    	return;
    
    // shorthand for if
    echo $image['alt'] ? $image['alt'] : $image['title'] ? $image['title'] : 'No ALT-Tag';
    
    // equal
    if( $image['alt'] ) {
    	echo $image['alt'];
    } elseif( $image['title'] ) {
    	echo $image['title'];
    } else {
    	echo 'No ALT-Tag';
    }

    My answer refers to the output of a gallery field from ACF. If you want to use the function for a single image (from ACF), you need to do the following:

    <?php $image = get_field( 'image' ); ?>
    
    <img src="" alt="<?php image_alt_tag( $image ); ?>" />
  • functions.php

    function image_alt_tag( $image = null )
    {
    	if( $image == null )	return;
    	echo $image['alt'] ? $image['alt'] : $image['title'] ? $image['title'] : 'No ALT-Tag';
    }

    within the wp loop

    <?php $images = get_field( 'gallery' ); ?>
    
    <?php if( $images ): ?>
    
    	<?php foreach( $images as $image ): ?>
    	
    	<img src="<?=$image['url']?>" alt="<?php image_alt_tag( $image ); ?>" />
    		
    	<?php endforeach; ?>
    	
    <?php else: ?>
    
    <p>No images</p>
    
    <?php endif;?>

    I do not know what your theme or template looks like. I have not explained anything, but you are welcome to ask if something is not understandable.

  • You can access your content within the loop via get_field ('field_slug');. So when you get your “relationship” via get_field (), you have access to the content.

    The loop should look like this:

    // check if entries exists
    if( have_posts() ):
    
    // loop start
    while( have_posts() ): the_post();
    
    // YOUR FIELD
    
     // loop end
    endwhile;
    
    // reset query
    wp_reset_postdata();
    
    // end
    endif;

    You can check the contents of the field with print_r( get_field('field_slug') );.

    Some Links:
    get_field
    wp_query

Viewing 25 posts - 1 through 25 (of 35 total)