Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The posts on our music blog usually include multiple tracks of music.

I want to have each individual track be its own entity--a custom post type called "track"--which I want to be able to insert freely into a post where I wish using a shortcode.

The image below explains pretty well what I am looking to do. (Link to the page that I took the screenshot from: http://iloveomfg.com/210/don-rimini-whatever-kaptain-cadillac-remix/ )

enter image description here

I want to create a shortcode, [track], which echoes the entire contents of the 'track' custom post type which id is specified. For example, if the track id is 34, the shortcode to display it within the post would be: [track id=34] or [track id="34"].

I have taken a look at all the posts related to this topic on this site, and have tried a few of the solutions, but to no avail, so I decided to go ahead and ask for my problem specifically. Can you tell me exactly what I need to do to achieve this?

Let me know if you need me to clarify anything.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

I would suggest creating a new custom taxonomy for relations between a post and its "tracks" or grouping of track posts if you'd like that way you can easily create a shortcode that will query all the needed tracks at once using a shortcode instead of calling your shortcode over and over and to order them you can create a custom field in track so in your query you can order by that field so it would be something like this (this assumes that you have a custom taxonomy named "post_tracks" and that all of the posts tracks were added to the same term of that taxonomy, also that you have a custom field to order you tracks named "in_order" :

function get_tracks($atts, $content = null) {  
        extract(shortcode_atts(array(  
            "post_tracks" => '',
            "tracks" => '',
        ), $atts)); 

        //if post_tracks relation term was passed:
        if ($atts['post_tracks'] != ''){
            $tracks = NEW WP_Query(array('post_type' => 'track', 'post_tracks' => $atts['post_tracks'], 'orderby' => 'meta_value', 'meta_key' => 'in_order' ));
            while($tracks->have_posts()){
                $tracks->the_post();
                //do whatever you want with each track eg:
                $out .= '<div class="track">
                    <h3>'.get_the_title($post->ID).'<h3>
                    <div class="track_inner">
                        <div class="track_img">'. get_the_post_thumbnail($post->ID, 'thumbnail').'</div>
                        <div class="track_content">'.apply_filters('the_content',get_the_content()).'<div>
                    </div></div>';
            }
            return $out;
        }

        //if its a single track you want:
        $tracks = NEW WP_Query(array('post_type' => 'track','post__in' => array($tracks) ));
            while($tracks->have_posts()){
                $tracks->the_post();
                //do whatever you want with each track eg:
                $out .= '<div class="track">
                    <h3>'.get_the_title($post->ID).'<h3>
                    <div class="track_inner">
                        <div class="track_img">'. get_the_post_thumbnail($post->ID, 'thumbnail').'</div>
                        <div class="track_content">'.apply_filters('the_content',get_the_content()).'<div>
                    </div></div>';
            }
        return $out;
}  

add_shortcode('tracks','get_tracks');

with this you can simple call your shortcode like this:

[tracks post_tracks="relation_tern_name"]

or just get your single track on at a time:

 [tracks tracks="track_id"]
share|improve this answer
    
Thanks, I'm going to try this out and see if it works. –  Harley Barnes May 9 '11 at 19:16
    
I'm getting an error: Parse error: syntax error, unexpected T_DOUBLE_ARROW for the following line: $tracks = NEW WP_Query(array('post_type' => 'track', 'post_tracks' => $atts['post_tracks']),'orderby' => 'meta_value', 'meta_key' => 'in_order' )); –  Harley Barnes May 9 '11 at 19:41
    
There was an extra ) ,I updated the answer, the the code now. –  Bainternet May 9 '11 at 20:33

You have two issues here I think.

1 - Making a short code. This Nettuts tutorial is excellent at stepping through each of the steps needed. (though you don't need the second half about adding a tinymce button).

2 - A way of storing track details. I am guessing you could you use a new post for each track with custom meta with the link to the audio file and any other details you want. Then the track=ID would refer to the ID of the post and you would use the shortcode to grab the ID and then hunt down the post with that ID and then include the meta of that post.

Hope that helps.

share|improve this answer
    
Yes, I have already created the custom post type with the extra meta fields, your #2. I need help on the specific way to create a shortcode which queries a specific id of a custom post type and displays it. –  Harley Barnes May 9 '11 at 19:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.