public
Created

Helping out someone on WPSE.

  • Download Gist
wpse28770.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
<?php
/*
Plugin Name: wpse28770 Fixer
*/
 
add_action('init', 'wpse28770_add_types' );
function wpse28770_add_types()
{
register_post_type( 'video', array( 'public' => true, 'label' => 'Videos' ) );
register_taxonomy( 'video_category', 'video', array( 'label' => 'Video Category' ) );
add_shortcode( 'wpse28770videos', 'wpse28770_shortcode_cb' );
}
 
function wpse28770_shortcode_cb()
{
// get the terms
$terms = get_terms( 'video_category' );
// no terms? bail.
if( ! $terms ) return '';
$out = '';
// loop through the terms
foreach( $terms as $term )
{
// get videos in each term
$videos = get_posts(array(
'post_type' => 'video',
'tax_query' => array(
array(
'taxonomy' => 'video_category',
'field' => 'id',
'terms' => $term->term_id,
'operator' => 'IN'
)
)
));
// no videos? continue!
if( ! $videos ) continue;
$out .= '<h2>' . esc_html( $term->name ) . '</h2>';
$out .= '<ul>';
// loop through the video posts
foreach( $videos as $v )
{
$link = sprintf(
'<a href="%s">%s</a>',
esc_url( get_permalink( $v ) ),
esc_html( $v->post_title )
);
$out .= '<li>' . $link . '</li>';
}
$out .= '</ul>';
}
return $out;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.