Support

Account

Forum Replies Created

  • By this setup this particular page (“Banner 1”) and all products each are given two fields: A link and a banner. Now you can assign them a link and a banner each. So, if you call get_field( 'my_custom_link_in_pages' ) on a product page, you are grabbing the link that you have assigned to this product. If you have not assigned this product a link, the function will return an empty value.

    Depending on what exactly your are trying to accomplish, there are three ways to solve this:

    1. You can assign your product the same link and banner that you have assigned “Banner 1”, so they now belong to the product as well.

    2. You can fetch the fields of “Banner 1” directly on every product page like this: get_field( 'my_custom_link_in_pages' , $the_id_of_the_banner1_page )

    3. You can remove the second field group condition, so the fields no longer apply to product pages, then create a second field group, which contains a Post Object field. Then you can assign each product a page the link and banner of which to use, like this:

    <?php
    $the_page_id = get_field( 'the_assigned_page' );
    ( is_object( $the_page_id ) ) ? $the_page_id = $the_page_id->ID : null ;
    $the_link = get_field( 'my_custom_link_in_pages' , $the_page_id  );
  • The arrays cannot be combined while they are being fetched and printed. Store them in another array instead and then process them all at the same time.
    I did not run this code, so watch out for missing punctuation and such:

    <?php
    $this_array  = get_field( 'this_array' );
    $that_array  = get_field( 'that_array' );
    $final_array = []; // An array of all rows.
    
    // Add all rows of $this_array to the array of all rows.
    if ( have_rows( 'this_array' ) ) :
    	while ( have_rows( 'this_array' ) ) : the_row();
    		$final_array[] = [
    			'arg1' => get_sub_field( 'arg1' ) ,
    			'arg2' => get_sub_field( 'arg2' ) ,
    		];
    	endwhile;
    endif;
    
    // Add all rows of $that_array to the array of all rows.
    if ( have_rows( 'that_array' ) ) :
    	while ( have_rows( 'that_array' ) ) : the_row();
    		$final_array[] = [
    			'arg1' => get_sub_field( 'arg1' ) ,
    			'arg2' => get_sub_field( 'arg2' ) ,
    		];
    	endwhile;
    endif;
    
    // Sort the $final_array here.
    // See: https://www.php.net/manual/en/array.sorting.php
    
    // Print each row.
    foreach ( $final_array AS $row ) {
    	echo '<li>' . $row['arg1'] . '<span>' . $row['arg2'] . '</span></li>';
    }
  • Good to hear. One correction though: I was missing an isset(). Otherwise, if there are multiple reviews, they will overwrite each other. Good luck!

    <?php
    // File this review by its game's version.
    if ( !isset( $version_array[$version] ) ) {
    	$version_array[$version] = [];
    }
    $version_array[$version] = $v;
  • If I am not missing anything, your problem is that you grab the reviews version numbers, but once you have found the highest version, you no longer know, which review that version number applies to, right?

    <?php
    // Fetch all reviews dealing with this game.
    $reviews = get_posts(array(
        'post_type'     => 'reviews',
        'meta_query'    => array(
            array(
                'key'         => 'related_games',
                'value'       => '"' . get_the_ID() . '"',
                'compare'     => 'LIKE'
            )
        )
    ));
    
    // An associative array of each review of each version.
    $version_array = [];
    
    foreach ( $reviews AS $k => $v ) {
    	
    	// Fetch the version.
    	$version = get_field( 'review_game_version' , $v->ID ) ?? false;
    	
    	// Ignore reviews that have no version.
    	if ( $version === false ) continue;
    	
    	// Save the version in the post object for later use.
    	$reviews[$k]->review_game_version = $version;
    	
    	// File this review by its game's version.
    	if ( $version_array[$version] ) {
    		$version_array[$version] = [];
    	}
    	$version_array[$version] = $v;
    }
    unset( $version );
    
    // Put the highest version at the end.
    ksort( $version_array );
    $highest_version = array_key_last() ?? false;
    
    // Make sure there is a highest version.
    if ( $highest_version !== false ) {
    	
    	// Iterate all reviews that are dealing with the highest version.
    	foreach ( $version_array[$highest_version] AS $review ) {
    		// Do something...
    	}
    }
  • I take it you created a Field Group containing my_custom_link_in_pages and assigned it to the Page post type. But is it assigned to the Product post type as well?

  • With a bit of work that could be done, I suppose. Actually, I was just about to look into that as well for a similar case, but have not come around to it yet.

    The idea is to create a custom Text field for Creator, that is hidden from the post edit screen by contradictory logic, such as: “Show only if another random field contains any value and contains no value at the same time.” Then we would hook into the post edit screen whenever a post is saved, check if it is a Project, iterate the Repeater, fetch each Creator mentioned, and save the data in the hidden field as JSON, so on the Creator template it suffices to get_field( 'hidden_text' ) and unpack the JSON without querying the Project post type.

    It would cause a bunch of additional database queries every time a post is saved, but that is trivial, especially if the alternatives are to either cause a bunch of additional database queries every time the front-end is called, or do it all manually and go insane.

  • I should have encapsulated the damn thing in <pre>...</pre>. Would have been much easier to read.
    Now, the dump says that this should, in fact, have been the solution. But if the roles still do not show up, I must have overlooked something. Please try the following and, when you censor the sensitive data, take care to not paint over any brackets. Those are important.

    <h3>Asistentes</h3>
    <div>
    <?php $users = get_field('asistentes'); ?>
    <!--<ul>-->
    <?php
    foreach ($users as $user):
    $roles = new WP_User( $user['ID'] ) ?? false ;
    echo '<pre>';
    var_dump( $user , $roles );
    $roles = is_object( $roles ) ? $roles->roles : [] ;
    var_dump( $roles );
    echo '</pre>';
    ?>
    <li>
    	<p><?php echo $user['display_name'];
    	echo 'User roles: ' . implode(', ', $roles) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>
  • Wow, that was quick. A few minutes after having sent that post I noticed an error and corrected it.
    Please confirm that you have applied exactly what my previous post shows now.
    If you have done so and it did not work, please try this next and show me the dumped values from at least one iteration of that foreach. We are indeed getting closer:

    <h3>Asistentes</h3>
    <div>
    <?php $users = get_field('asistentes'); ?>
    <ul>
    <?php
    foreach ($users as $user):
    $roles = new WP_User( $user['ID'] ) ?? false ;
    var_dump( $user , $roles );
    $roles = is_object( $roles ) ? $roles->roles : [] ;
    var_dump( $roles );
    ?>
    <li>
    	<p><?php echo $user['display_name'];
    	echo 'User roles: ' . implode(', ', $roles) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>
  • If $user['display_name'] works just fine, then perhaps this will to do the trick. At least it should:

    <h3>Asistentes</h3>
    <div>
    <?php $users = get_field('asistentes'); ?>
    <ul>
    <?php
    foreach ($users as $user):
    $roles = new WP_User( $user['ID'] ) ?? false ;
    $roles = is_object( $roles ) ? $roles->roles : [] ;
    ?>
    <li>
    	<p><?php echo $user['display_name'];
    	echo 'User roles: ' . implode(', ', $roles) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>
  • Without you providing any code samples I can only guess what is really happening on your site, but the following might be applicable:

    <?php
    $i = 0;
    while( ... ) {
    	if ( $i == 0 ) {
    		// Do an odd row...
    		$i++;
    	} else {
    		// Do an even row...
    		$i--;
    	}
    }
    unset( $i );
  • The form input is sanitized by wp_kses_normalize_entities( string $string ), see:
    https://developer.wordpress.org/reference/functions/wp_kses_normalize_entities/.

    You can deactivate it by using the kses option, see:
    https://www.advancedcustomfields.com/resources/acf_form/

    <?php
    acf_form(array(
    	'fields'          => array('res'),
    	'submit_value'    => 'Send',
    	'updated_message' => __("", 'acf'),
    	'kses'            => false,
    ));?>

    However, it is never a good idea to not sanitize user input. If you pass up on kses, you should sanitize it some other way that watches out for links and leaves them intact.

  • You could create a Repeater field for your Projects containing the following:

    • A post type Relationship, that allows only a single post, limited to Creator
    • A taxonomy Relationship, limited to your role taxonomy (Director, Animator etc.)

    When editing the Project you can then create a new Repeater item for each Creator and assign it roles. The downside is that in the Creator templates you would have to query Projects in order to check what the Creator at hand is involved with, which, depending on the scale of your website, could be rather expensive.

  • Assuming that the schedule field is part of the Event post type, you can grab the schedule this way:

    <?php
    $event_id = get_field( 'the_users_event_field_name' );
    if ( is_object( $event_id ) ) {
        $event_id = $event_id->ID;
    }
    get_field( 'the_schedule_field_name' , $event_id );
  • For implode() to work, the second argument has to be an array, but I do not see the variable $user_info set anywhere.
    Now, since there could be anything behind that variable, please dump it and post what it spits out, provided that it does not reveal any sensitive information:

    <h3>Asistentes</h3>
    <div>
    <?php $users = get_field('asistentes'); ?>
    <ul>
    <?php
    foreach ($users as $user):
    var_dump( $user , $user_info );
    ?>
    <li>
    	<p><?php echo $user['display_name'];
    	echo 'User roles: ' . implode(', ', $user_info['role']) . "\n";?></p>
    </li>
    <?php endforeach; ?>
    </div>
  • Currently, ACF’s only elements for layouting in the strict sense are Accordions and Tabs, both of which are meant to hide their contents. If I want to, for example, arrange a lot of small input fields in a row, I have to use the group element, which is its own field and comes with its own slug. The same applies to repeaters and flex content.

    I, too, think that ACF lacks elements, that merely serve to visually improve the back-end, but do not affect the field group hierarchy, and I would like to see more such elements implemented.

  • Hey RD101,

    as you already have a plugin to set the date and move posts to a different category, I do not quite see what ACF is supposed to accomplish here.
    In order to change the sorting on the blog page, you have to modify the main query. A quick search did not reveal any plugins to that end, so it seems you will not get around using PHP to hook into it. The plugin defines a meta key _expiration-date that can be used for sorting (cf. https://plugins.trac.wordpress.org/browser/post-expirator/trunk/post-expirator.php#L78).

    What theme are you running, is it a so-called child-theme, and do you have any experience with PHP?

  • I use to retrieve block fields with get_field, both on the front-end and the back-end. If you experience this problem only on the back-end, try changing all instances of $block[‘data’] into $data and adding this at the beginning of the function:

    <?php
    $data = is_admin() ? get_field( 'data' ) : $block['data'] ;
  • In the problem line:

    <?php
    <p class="project-subject"><?php echo the_field('top_post_content')['title']?></p>

    you may want to replace the_field with get_field, or omit echo.

    I take it, top_post_content refers to a post. In the back-end you can pick if you want the field to return a post object or merely the post ID, in which case it will not return the title. You could use:

    <?php
    get_the_title( get_field('top_post_content') )
Viewing 19 posts - 26 through 44 (of 44 total)