0

I have a custom taxonomy called Album Link (album_link). I created this so I could link all music that has the term e.g (Glorious Album) to an album post type that uses the same term.

The goal here is to insert a shortcode [album] into the album post type which will look into the current selected Term of Taxonomy (Album Link) and retrieve all the music post type that uses that same term.

Let me break it down further:

  1. I have a custom tax (Album Link) term called (Glorious Album) and Post Types (Music) and (Album).
  2. I select the (Album Link) term Glorious Album while posting songs through the music post type.
  3. I now create an Album post type, choosing the Album Link term Glorious Album and inserting [album] shortcode to retrieve all the songs (music post type) posted using the same term.
  4. The [album] will now auto-detect the current selected Album link term (Glorious Album) and display all posts from the music post type that uses the same term.

MY CODE ONLY WORKS IF I ENTER THE TERM ID OF THE ALBUM LINK TAX E.G [album id="123"]

// ALBUM TRACKLIST SHORTCODE
function album_shortcode( $atts , $content = null ) {
extract(shortcode_atts(array(
"title" => '',
"id" => '', 
"url" => '#',
"text" => '',
), $atts));
ob_start(); ?>
  <?php  
$albumitems = get_posts(array(
'post_type' => 'music',
'orderby'=>'date','order'=>'ASC',
'posts_per_page'=>-1,   
'tax_query' => array(
            array(
                'taxonomy' => 'album',
                'field' => 'term_id',
                'terms' => $id,
            )
        )   
)); ?>
<?php
$postNumber = 1;
?>
<?php if( $albumitems ): ?>
<div class="mu-o">
  <div class="mu-o-c">
    <div class="mu-o-unit mu-o-head">
      <div class="mu-o-ind">No.
      </div> 
      <div class="mu-o-info">Title
      </div>
      <div class="mu-o-act">Download
      </div>
    </div>
    <?php foreach( $albumitems as $albumitem ) { ?>
    <div class="mu-o-unit-c">
      <div class="mu-o-unit">
        <div class="mu-o-ind ind">
          <b class="rindx"><?php echo $postNumber++; ?>
          </b>
        </div>
        <div class="mu-o-info">
          <h4 class="mu-o-title">
            <a href="<?php echo get_permalink( $albumitem->ID ); ?>">
              <?php the_field( 'track_title', $albumitem->ID ); ?> 
            </a> 
          </h4>
          <div class="mu-o-prod"><?php
$artists = get_field('artiste_name', $albumitem->ID );
                echo 'By ';
                $artist_terms = '';                         
                foreach($artists as $index => $artist) {
                    $enIndex = sizeof($artists) - 2;
        $end = (isset($artists[$enIndex]) && $index == $enIndex ? ' x ' : ' x ');
        $artist_link = get_term_link( $artist );
        $artist_terms .= '<a href="' . esc_url( $artist_link ) . '">' . $artist->name . '</a>' . $end;    
                }
                echo rtrim($artist_terms, ' x ' );       
                
                  ?>
              
              
            <?php $featuring = get_field( 'featuring', $albumitem->ID ); 
            if ( $featuring ): 
                    echo 'feat. ';
                    $feature_terms = '';                                    
                foreach($featuring as $index => $feature) {
                    $enIndex = sizeof($featuring) - 2;
        $end = (isset($featuring[$enIndex]) && $index == $enIndex ? ' and ' : ', ');
        $feature_link = get_term_link( $feature );
        $feature_terms .= '<a href="' . esc_url( $feature_link ) . '">' . $feature->name . '</a>' . $end;     
                }
        echo rtrim($feature_terms, ' , ' );        

?> <?php endif; ?>
          </div>
        </div>
    </div>
    </div>
    <?php } ?>
  </div>
</div>
<?php endif; ?>

<?php
return ob_get_clean();
}
add_shortcode( 'album', 'album_shortcode' );

MY PROBLEM: I want it to auto-detect the current selected Album Link terms without the need for manual id="" insertion. But if I use an id="" it will override the auto-detect.

Also, If 2 or more terms are selected it will display their posts respectively.

Thanks.

1
  • To do this you would need a shortcode to provide context to the shortcodes nested inside it, which isn't something that shortcodes support. It can be done with blocks though. Unless I'm missing something and I've misunderstood? It's not clear how this shortcode is being used or where it is being used, and there are no full examples of how the two shortcodes interact or where they're used. E.g. are they on a page template? What do you mean by selection? Are there form inputs in the other shortcode we can't see? Or did you mean click on a link? Which link? Use the edit link to clarify your Q
    – Tom J Nowell
    Apr 20 at 19:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.