Support

Account

Forum Replies Created

  • Cool @emaildano, thanks for sharing your solution.

  • Yes, I would appreciate this too @elliot, so we could keep track of the stuff easily.


    @mikelittle
    : Thanks for co-creating WordPress, you rock!

  • Sadly your image isn’t showing up above. Have you looked at the “Image” field template usage? http://www.advancedcustomfields.com/resources/field-types/image/#template-usage

    Depending on the return value you have set for the “Image” field you’ll have to add different code to your template. I usually use “ID” and get the image myself, but sometimes the “Object” is more appropriate. Please have a look at the demo code and let me know if this is helping you.

  • Happy that it works now, that’s something you would have to ask Elliot. I’m just a coder how loves this plugin and therefore I try to help out in the forum ;-).

    Here is the link to the Feature Request page: http://support.advancedcustomfields.com/forums/forum/feature-requests/

  • Ok I think I figured out a solution for you to test. First we need to loop through the while( has_sub_field( 'schede_prodotti' ) ) and prepare the data for furhter use in $ids and $files. Next we use these arrays containing the $post->IDs from the relationship field and the $files from the other fields in the repeater and prepare our data to get out.

    Here is the code:

    <?php if ( get_field( 'schede_prodotti' ) ): ?>
    <table class="product-list simple-list">
        <?php
    
            $ids   = array(); // Empty array for the $post->IDs from the relationship fields
            $files = array(); // Empty array for the files from the repeater fields
    
            while( has_sub_field( 'schede_prodotti' ) ):
    
                /**
                 * Get the IDs
                 *
                 * Returns an array of the IDs of the posts
                 * selected using the Relationship field.
                 *
                 * @var array
                 */
                $id    = get_sub_field( 'relazione_prodotti' );
                $ids[] = $id[0]; // We need the first array of the return value of the relationship field
    
                /**
                 * Get the files
                 *
                 * Store the files under the $post->ID from the relationship field
                 * and use the field name for reference.
                 *
                 * @var $array
                 */
                $files[$id[0]]['scheda_tecnica'] = get_sub_field('scheda_tecnica');
                $files[$id[0]]['scheda_di_sicurezza'] = get_sub_field('scheda_di_sicurezza');
    
            endwhile; // has_sub_field( 'schede_prodotti' )
    
            if ( $ids ) : // Check if we have $ids from the posts.
    
                /**
                 * Prepare the get_posts arguments
                 *
                 * With the 'orderby' => 'ttile' we accomplish the goal of having the
                 * products ordered by name instead of drag and drop.
                 *
                 * @see http://codex.wordpress.org/Template_Tags/get_posts#Parameters
                 * @var array
                 */
                $args = array(
                    'post__in'  => $ids,        // Use the ids from above
                    'order'     => 'ASC',       // Order
                    'orderby'   => 'title',     // Field to order the posts by
                    'post_type' => 'prodotti'   // The custom post type you use
                );
    
                /**
                 * Query the Posts
                 *
                 * @var array
                 */
                $posts = get_posts( $args );
    
                if ( $posts ) : // Check if we have $posts
    
                    foreach ( $posts as $post ) : setup_postdata( $post ); ?>
                        <tr>
                            <td class="title">
                                <span><?php the_title(); ?></span>
                            </td>
                            <td>
                                <a href="<?php echo $files[$post->ID]['scheda_tecnica']; ?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
                            </td>
                            <td>
                                <a href="<?php echo $files[$post->ID]['scheda_di_sicurezza']; ?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
                            </td>
                        </tr><?php
    
                    endforeach; // ( $posts as $post )
                    wp_reset_postdata();
    
                endif; // ( $posts )
            endif; // ( $ids )
        ?>
    </table>
    <?php endif; // get_field( 'schede_prodotti' ) ?>

    And again I updated the Gist for better readability: https://gist.github.com/neverything/6573468

    Let me know if this works now with the sorting of the titles.

  • Ahh I see the problem now. Instead of having multiple Relationships within one Repeater field, you created a repeater that has a
    – Relationship field to one product
    – A file
    – Another file

    Therefore we get the $ids for each Repeater field which will only return one as we only have one Relationship at the time.

    What the code tries to do is to order the $ids within each repeater, but there is only one. I’ll look at a different solution later today.

  • Ok, just checked out the code on a demo site I created with your XML file. I’m not sure, but we might forgot to tell WordPress about your post_type => ‘prodotti’ in the query. Could you please check if the following code works for you?

    <?php if ( get_field( 'schede_prodotti' ) ): ?>
    <table class="product-list simple-list">
        <?php while( has_sub_field( 'schede_prodotti' ) ): ?>
        <tr>
            <td class="title">
            <?php
                /**
                 * Get the IDs
                 *
                 * Returns an array of the IDs of the posts
                 * selected using the Relationship field.
                 *
                 * @see Screenshot: Change return value from Post Objects to Post IDs
                 * @var array
                 */
                $ids = get_sub_field( 'relazione_prodotti' );
                // var_dump( $ids ); //uncomment this line to see the $ids getting loadded
    
                /**
                 * Prepare the get_posts arguments
                 *
                 * With the 'orderby' => 'ttile' we accomplish the goal of having the
                 * products ordered by name instead of drag and drop.
                 *
                 * @see http://codex.wordpress.org/Template_Tags/get_posts#Parameters
                 * @var array
                 */
                $args = array(
                    'post__in'  => $ids,    // Use the ids from above
                    'order'     => 'ASC',   // Order
                    'orderby'   => 'title',  // Field to order the posts by
                    'post_type' => 'prodotti' // We might forgot about your post type...
                );
    
                /**
                 * Query the Posts
                 *
                 * @var array
                 */
                $posts = get_posts( $args );
    
                if ( $posts ) :
    
                    foreach ( $posts as $post ) : setup_postdata( $post ); ?>
    
                        <span><?php the_title(); ?></span><?php
    
                    endforeach;
                    wp_reset_postdata();
    
                endif; // ( $posts )
            ?>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_tecnica')?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_di_sicurezza')?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
            </td>
        </tr>
    
        <?php endwhile; // has_sub_field( 'schede_prodotti' ) ?>
    </table>
    <?php endif; // get_field( 'schede_prodotti' ) ?>

    You’ll find that I updated two lines:
    https://gist.github.com/neverything/6573468#file-acf-php-L17 and the more important one: https://gist.github.com/neverything/6573468#file-acf-php-L32

    Please let me know if this works for you and if not, please tell me so I’ll gladly have a look at your WordPress installation. Attached you find a screenshot of the working example table I rendered on my demo site.

  • Maybe you’ll have to re-save the relationships on the pages you are using this. So the database is updated.

  • Cool! Good luck and let me know wether the code above can be used as a solution.

  • Well it looks like something is wrong or not updated on your side @gleenk. Because when I import your file, I get the “Return Value” attribute in the repeater as well. Could you please check if ACF and the Repeater Plugin are updated and maybe that you use the latest WordPress version too.

    Have a look at the screenshot and you’ll see your fields with the “Return Value” thing to be selected.

  • Well I also have the “Return Value” fields when the “Relationship” field is within a “Repeater” that is really odd. Have a look at the screenshot attached.

    Could you please export your field groups as an XML file and attach them here? So I could have a look and maybe figure out what’s going on.

  • Hi @gleenk, I just checked again, it seems you might need to update ACF and the relating plugins, as the fields I’m seeing for the “Relationship” Field are a bit different. See the screenshot attached, there you’ll spot the “Return Value” attribute.

  • Did you change the “Return Value” and resave the post?

  • Hi @gleenk, lucky you I have a lazy Sunday and therefore I had some time to check out your code.

    First of all, please change the “Return Value” on your “Relationship” field from “Post Objects” to “Post IDs”, this way we get a nice array of all the WP_Post->IDs instead of the full WP_Post objects. See the screenshot attached for more info.

    So this brings me to the code:

    <?php if ( get_field( 'schede_prodotti' ) ): ?>
    <table class="product-list simple-list">
        <?php while( has_sub_field( 'schede_prodotti' ) ): ?>
        <tr>
            <td class="title">
            <?php
                /**
                 * Get the IDs
                 *
                 * Returns an array of the IDs of the posts
                 * selected using the Relationship field.
                 *
                 * @see Screenshot: Change return value from Post Objects to Post IDs
                 * @var array
                 */
                $ids = get_sub_field( 'relazione_prodotti' );
    
                /**
                 * Prepare the get_posts arguments
                 *
                 * With the 'orderby' => 'ttile' we accomplish the goal of having the
                 * products ordered by name instead of drag and drop.
                 *
                 * @see http://codex.wordpress.org/Template_Tags/get_posts#Parameters
                 * @var array
                 */
                $args = array(
                    'post__in'  => $ids,    // Use the ids from above
                    'order'     => 'ASC',   // Order
                    'orderby'   => 'title'  // Field to order the posts by
                );
    
                /**
                 * Query the Posts
                 *
                 * @var array
                 */
                $posts = get_posts( $args );
    
                if ( $posts ) :
    
                    foreach ( $posts as $post ) : setup_postdata( $post ); ?>
    
                        <span><?php the_title(); ?></span><?php
    
                    endforeach;
                    wp_reset_postdata();
    
                endif; // ( $posts )
            ?>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_tecnica')?>" class="notifiche" data-tipo="Scheda Tecnica">Scarica la Scheda Tecnica</a>
            </td>
            <td>
                <a href="<?php the_sub_field('scheda_di_sicurezza')?>" class="notifiche" data-tipo="Scheda di Sicurezza">Scarica la Scheda di Sicurezza</a>
            </td>
        </tr>
    
        <?php endwhile; // has_sub_field( 'schede_prodotti' ) ?>
    </table>
    <?php endif; // get_field( 'schede_prodotti' ) ?>

    For better readability, I uploaded the code as a Gist.

    Please let me know if this solves your problem.

  • Hi @gleenk it looks like you’ll have to retrieve the WP_Post IDs first and next perform an WP_Query or get_posts to order this stuff properly.

    Have a look at the following post “Order Post Objects by menu_order”. The Post Object and the Relationship field both return an array of WP_Post objects, so this solution should be possible for you to use.

    In your $args you’ll use the following to order by title 'orderby' => 'title'

    Please let me know if this works for you…

  • Hey Elliot, awesome thank you! Let me know if you need further information to solve this issue.

  • Thanks for the info @roflman79, the weird thing is though, that it’s only appearing to be a problem with multsite and the gallery field add-on. Other premium add-ons don’t have the problem.

  • In your frontend, you’ll have to count number of elements in the repeater field array. So something like this:

    <?php
    
    // pre define the allowed columns
    $allowed_classnames = array(
        1 => 'full',
        2 => 'half',
        3 => 'third',
        4 => 'fourth',
        5 => 'fifth',
    );
    // get the count on the repeater field
    // mabye use get_sub_field() instead of get_field() if it's nested
    $number_of_cols = count( get_field( '<repeater>' ) );
    
    // set a default classname
    $classname_to_use = $allowed_classnames[1];
    
    // check if the $number_of_cols exist in the predefined classnames
    if ( array_key_exists( $number_of_cols , $allowed_classnames ) ) {
        // set the classname to be used
        $classname_to_use = $allowed_classnames[$number_of_cols];
    }
    
    while( has_sub_field( '<repeater>' ) ) : ?>
        <div class="<?php echo esc_attr( $classname_to_use ); ?>">
            <?php the_sub_field( '<repeater_content>' ); ?>
        </div>
    <?php endwhile; ?>
    

    Please not that I just briefly tested it on a site I’m working on. You’ll have to replace the '<repeater_content>' and '<repeater>' with your actual field names.

Viewing 20 posts - 1 through 20 (of 20 total)