Support

Account

Forum Replies Created

  • Well it’s 6 years old but thought I’d highlight another way to do this as I too used the counter solution but then discovered array_slice which is less verbose.

    
    $posts = get_field('books');
    $books = array_slice( $posts, 0, 3 );
    foreach( $books as $post):
      setup_postdata( $post );
      the_title();
    endforeach;
    wp_reset_postdata();
    
  • Hey Nikolay,

    Do you want to delete the row from the database or just prevent it displaying on the page/post?

    Cheers, Dan

  • Hey there, I might not fully understand what you’re aiming to achieve, but, you can either apply the field/group to the other page or post so you can add the data there directly.

    Or you can grab the data from another page or post like this.

    If you’re talking about re-using the mark-up itself you could use an include. With PHP I tend to use get_template_part like this:

    <?php get_template_part( 'name_of_code_block' ); ?>.

    Using a combination of these methods, you could essentially re-create the page on a different post/page. So whilst it wouldn’t be embedded as such it would mimic the effect.

    I hope this helps.

  • This reply has been marked as private.
  • Hey @jfarr,

    I’ve just tested this, and whilst the function won’t work from within the include, it will work within your theme’s functions.php file.

    Move it there and you should get the result you seek.

    Cheers, Dan

  • Hey Jon,

    All seems fine. I did notice you’re using the ID return method for your image. The setting defaults to an array return so I’ve modified my answer to suit.

    I’ve tidied up your code and tested both in and out of the loop within a page template and it works perfectly.

    
    <div class="stats allignfull">
        <div class="wp-block-columns has-6-columns">
            <?php if( have_rows('stat_repeater') ): ?>
                <?php while( have_rows('stat_repeater') ): the_row(); ?>
                    <div class="wp-block-column">
                        <div class="wp-block-classic align">
                            <?php
                            $image = get_sub_field('stat_image');
                            if( $image ) {
                                $url = $image['url']; ?>
                                <p><img src="<?php echo esc_url($url); ?>" alt="<?php the_sub_field('stat_headline'); ?>" /></p>
                            <?php } ?>
                            <p class="headline">
                                <span class="font-color-blue-dark"><strong><?php the_sub_field('stat_headline'); ?></strong></span>
                            </p>
                            <p class="center-text"><?php the_sub_field('stat_text'); ?></p>
                        </div>
                    </div>
                <?php endwhile; ?>
            <?php endif; ?>
        </div>
    </div>
    

    The only reason I could see this not working is if you’re trying to get data from a different page. If this is the case you’d need to include the ID that is the source of the data in the initial query so, it’d be something like this:

    
    <?php
    $page = get_page_by_title( 'About' );
    $about_page_id = $page->ID;
    ?>
    
    <?php if( have_rows('stat_repeater', $about_page_id) ): ?>
        <?php while( have_rows('stat_repeater', $about_page_id) ): the_row(); ?>
    

    I hope this helps!

  • Hey Jon,

    Just to rule out a couple of things, you say this is outside of the loop so is it not between the following lines?

    
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
      <?php // The code is not here? ?>
    <?php endwhile; ?>
    

    Also, is the repeater located on the page you’re trying to display it on? Or is it elsewhere?

    Cheers, Dan

  • Ah well, I’m glad you sorted it @daretothink !

  • Hey @daretothink,

    Is your return value set to Term Object or Term ID?

    Dan

  • Hey @johnykalesony,

    It’s not the most descriptive question so there’s some guesswork on my part. I’m assuming your image field is returning an array as opposed to an ID or a URL.

    
    function my_custom_gallery() {
        query_posts(array(
            'post_type' => 'galeria',
            'orderby' => 'publish_date',
            'order' => 'ASC'
        ));
        while (have_posts()) : the_post();
            $image_field = get_field('image_field');
            $image_url = $image_field['url'];
            $image_alt = $image_field['alt']; ?>
            <img src="<?php echo esc_url($image_url); ?>" alt="<?php echo esc_attr($image_alt); ?>" />
        <?php endwhile;
    }
    add_shortcode('mygallery-1', 'my_custom_gallery');
    

    I hope this helps!

    Dan

  • Hey @jags, okay I think I know what you want to achieve, but I think you’ll need to apply the playlist position field to the playlist post itself. You could then customise the post lists screen to show the playlist position column and data for each post.

    I’m guessing you’re aiming for an end result of being able to edit the playlist position via the post lists page itself. This could be done, by exploring the customisation of “quick edit”, but it doesn’t look like an easy task.

    The other thing you could do, and this might be more straightforward, is use taxonomies. You could create a “position” taxonomy and then have terms for the position, you could have first, second, third and standard. It looks like Custom Post Type UI has a feature that allows you to add taxonomies to quick edit.

  • I’ve got a feeling @jags would like to assign custom fields to the Posts list page itself as its own entity and I’m pretty sure that’s not possible. However, I think what you could do is customise the in-built filter to achieve the same result.

    https://wordpress.stackexchange.com/questions/16637/how-to-filter-post-listing-in-wp-dashboard-posts-listing-using-a-custom-field/16641

  • Apologies, my response talked about capturing the fields themselves rather than the related data. This is what happens when you answer questions late at night after a couple of beers. Interesting question though and good of you to post the solution, which is, coincidently, going to be useful to me in the near future.

  • Hi Frank,

    The HTML titles you’ve posted look perfectly fine but you could add them into the acf_form function using html_before_fields or html_after_fields.

    So the second block would be amended as follows:

    
    acf_form( array(
        'post_id'	      => 'new',
        'field_groups'    => array(267),
        'form'            => false,
        'post_status'.    => 'publish',
        'post_type'	      => 'parutions',
        'updated_message' => '',
        'html_before_fields' => '<h2>1 title</h2>'
    ));
    

    I hope this helps.

  • Thanks for this John, to accompany your helpful links I’ve created a PHP function which transforms the comma separated string into an array which matches the format of a repeater. The gist and a more detailed explanation are here:

    Comma separated string into ACF compatible multidimensional array

    Posting in case it helps anyone else.

  • Hey there,

    This sounds like a complicated process, I wonder if we can simplify? Are you aware of the following ACF functionality:

    This sounds like it might be more helpful. Particularly if you’re trying to make ACF fields/groups transportable. I personally use the acf-json method which means that the fields I create locally are version controlled and can be deployed to remote servers without me needing to edit databases.

    I hope this helps.

  • Hey Bruno,

    You could indeed use a nested repeater. Each row of the repeater would contain a text title field, a subtitle text field and a nested repeater.

    One other solution that might offer you more flexibility, is the flexible content field.

    This allows you to create section blocks, a “table” block could contain a title, a subtitle and a repeater.

    I hope this helps, Dan

  • Hey Jags,

    What you’re talking about is the customisation of the Posts list table. This is done via functions.php using the two following hooks:

    I actually saved a gist which provides a similar music based example.

    I hope this helps!

    Dan

  • Are you able to post the code? I’m thinking that you use count and only display the field if the result is equal to one.

    So, if ( user comment count == 1 ) { echo $field }.

  • My pleasure Darren, glad it worked!

  • I’m reading between the lines here but I think you mean something like this:

    <?php $custom_title = get_field('custom_title');
    if( ! $custom_title ) { $custom_title = get_the_title(); } ?>
    <h1><?php echo $custom_title; ?></h1>

    So basically we say; if no custom field exists use the post title. It’s not the most detailed of questions but I’m hoping this is a suitable answer and helps you.

  • Hey Darren,

    Two stages to this, first of all create a function that will add the suffix to the number, so in your theme’s functions.php file add the following:

    function add_suffix( $n ) { $suffix = date( 'S', mktime( 1, 1, 1, 1, ( (($n>=10)+($n%100>=20)+($n==0))*10 + $n%10) )); return $n . $suffix; }

    Then amend your code in the post/page by removing the line echo $interval->y. ‘ Year of Service’;

    And then replace it with the two following lines:
    $interval_with_suffix = add_suffix( $interval->y );
    echo $interval_with_suffix . ' Year of Service';

    I hope this helps, it’s been semi-tested, but no guarantees.

    If you’re interested in learning about the suffix function there’s a detailed answer here. Credit to Andrew Kozak for the solution I’ve just modified the function slightly to return the original number as well as the suffix.

    Cheers, Dan

  • Hi Richard,

    This might be some help:
    updating-user-meta-on-save-post

    You’ll be looking at creating a function that uses the WordPress save_post hook, more info on that here.

    Not a complete answer I know, but it’ll hopefully give you some direction.

    Cheers, Dan

  • Hi you could do something like this:

    <?php if( get_field('field_one') || get_field('field_two') ) { ?>
    <?php the_field('field_one'); ?> <?php the_field('field_two'); // Display the fields not the content ?>
    <?php } else { ?>
    <?php the_content(); ?>
    <?php } ?>

    So we check to see if any fields exist and if they do we display them, if not we display the post’s content.

    I hope that helps!

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