3

Sometime I will have 2 links or 1 link.

<a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>

I want to add "or" between the 2 links. So if there are 2 links, add "or" For example, "Soundcloud or Youtube"

If there is 1 links don't add "or". For example, "Soundcloud"

AFC Repeater field type

<div class="container-wrap" id="music">

    <?php

    $args  = array('post_type' => 'music-playlist-1');
    $query = new WP_Query($args);

    $cntr = 0;

    while( $query -> have_posts() ) : $query -> the_post(); $cntr++; ?>

<section class="row-wrap">
    <div class="row-inner">

        <?php if ($cntr % 2 == 1) { ?>

        <?php 

        $image = get_field('artwork');

        if( !empty($image) ): ?>

            <img class="artwork" src="<?php echo $image['url']; ?>">

        <?php endif; ?>


        <div class="artwork-content">
            <h1><?php the_field('playlist-name'); ?></h1>

            <button class="btn-wrap">
                <div class="btn">listen now</div>
            </button>

            <div class="option-bar">

                <?php

                // check if the repeater field has rows of data
                if( have_rows('playlist_link') ):

                    // loop through the rows of data
                    while ( have_rows('playlist_link') ) : the_row();

                ?>

                <a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>

                <?php

                    endwhile;

                else :
                    // no rows found
                endif;

                ?>

            </div>
        </div>


        <?php } else { ?>

                    <div class="artwork-content">
            <h1><?php the_field('playlist-name'); ?></h1>

            <button class="btn-wrap">
                <div class="btn">listen now</div>
            </button>

            <div class="option-bar">

                <?php

                // check if the repeater field has rows of data
                if( have_rows('playlist_link') ):

                    // loop through the rows of data
                    while ( have_rows('playlist_link') ) : the_row();

                ?>

                <a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>

                <?php

                    endwhile;

                else :
                    // no rows found
                endif;

                ?>

            </div>
        </div>


        <?php 

        $image = get_field('artwork');

        if( !empty($image) ): ?>

            <img class="artwork" src="<?php echo $image['url']; ?>">

        <?php endif; ?>

        <?php } ?>

    </div>
</section>

    <?php endwhile; ?>
    <?php wp_reset_query(); ?>

Darren
  • 13,050
  • 4
  • 41
  • 79
waisie
  • 43
  • 3

1 Answers1

0

I've never actually used the repeater rows, but looking at the docs I'll assume you can check if you are on first row if get_row_index() is 0 (since there seems to be no way to check if you are on the last row). Thus:

edit: ok now I HAVE tried it out. sorry. get_row_index() is new in version 5.3.4. Maybe that's your problem?

Here is a solution without get_row_index()

I added + before each line I added.

<?php

// check if the repeater field has rows of data
if (have_rows('playlist_link')):

+   // We set a flag for the first row
+   $is_first = true;

    // loop through the rows of data
    while (have_rows('playlist_link')) : the_row();

+       if ($is_first) :
+           $is_first = false;
+       else :
+           echo " OR ";
+       endif; ?>

        <a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>

        <?php

    endwhile;

else :
    // no rows found
endif;

?>

Edit 2: How do we make this reusable?

Ok, I'll do this the super simple way. I take all the code, wrap it in a function... and put it in functions.php:

function playlist_links() {   // <--This line is new
    if (have_rows('playlist_link')):
        $is_first = true;   
        while (have_rows('playlist_link')) : the_row();    
            if ($is_first) :
                $is_first = false;
            else :
                echo " OR ";
            endif; ?>

            <a target="_blank" href="<?php the_sub_field('playlist-url'); ?>"><?php the_sub_field('media-player'); ?></a>  

            <?php
        endwhile;
    endif;
}      // <--This line is also new

Then in your template-file, you can replace all that code with

<?php playlist_links(); ?>

And use it several times if you so wish.

(If you only use this function on a single page template, then maybe functions.php is not the greatest place for it since it get's loaded everywhere. You can instead make functions directly in your template-file.)

ippi
  • 9,857
  • 2
  • 39
  • 50
  • Thanks for the answer, but it's not working. Should I put the code inside the
    ? After I put the code the playlist link doesn't show up and other music description and title, artwork doesn't show up. Let me know if this is right.
    – waisie May 30 '16 at 14:45
  • Ah my bad then! Sorry, I should have tested it out before making it an answer! I edited it a bit to show only the important part. (I still think it should work though!) – ippi May 30 '16 at 16:00
  • Do you mean add the code before ? – waisie May 30 '16 at 16:04
  • Can you show me the whole thing? I try it but it doesn't work :( – waisie May 30 '16 at 16:06
  • I have 2 while loop, which while loop? – waisie May 30 '16 at 16:22
  • Alright, updated answer. Sorry I kept you waiting. This time I installed the plugin and tried it out for myself. – ippi May 30 '16 at 18:06
  • Omg...it works like a charm...Thank you so much...appreciated :) – waisie May 30 '16 at 18:20
  • I have one more question, I have to use the repeat code for the left design and right design. For example, my design look like this is for left design( artwork on the left, title, description, links) and for right design (title, description, links, artwork on the right). Do you understand what I'm trying to say? :) I just want to update it once, so I don't have to update for the left and right for the same code. Let me know if you understand. Anyway thanks for helping me :) really appreciated. – waisie May 30 '16 at 18:24