Skip to content

Instantly share code, notes, and snippets.

@mrfoxtalbot
Created February 1, 2016 09:04
Show Gist options
  • Save mrfoxtalbot/6343c4b454c938bdcafb to your computer and use it in GitHub Desktop.
Save mrfoxtalbot/6343c4b454c938bdcafb to your computer and use it in GitHub Desktop.
WordPress shortcode to loop through child
<?
add_image_size( 'areas', 400, 300, true );
add_shortcode( 'areas', 'custom_query_shortcode' );
function custom_query_shortcode( $atts ) {
// EXAMPLE USAGE:
// [areas show_posts="100" post_type="page" post_parent="246"]
// Defaults
$defaults = array(
"show_posts" => 100,
"post_type" => 'page',
"post_parent" => false
);
$atts = shortcode_atts( $defaults, $atts );
extract( $atts );
// El post type está bien?
if ( in_array( $post_type, array( 'attachment', 'revision', 'nav_menu_item' ) ) ) {
return 'nothing found';
}
// El post parent existe?
if ( ! get_post( $post_parent ) )
return "Nothing found";
$query_args = array(
'ignore_sticky_posts' => true,
'post_parent' => $post_parent,
'post_type' => $post_type,
'posts_per_page' => $show_posts,
'order' => 'ASC'
);
$the_query = new WP_Query( $query_args );
// Reset and setup variables
$output = '';
// the loop
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post();
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
$url = $thumb['0'];
$slug = get_post_field( post_name, $post->ID );
$output .= "<div class='servicio-item col span_1_of_4'><a class='servicio-foto' href='http://www.babylandibiza.com/servicios/#".$slug."'><img src='".$url."'></a><div class='servicio-excerpt'><a href='http://www.babylandibiza.com/servicios/#".$slug."'>". get_the_excerpt() ."</a></div><div class='servicio-titulo'><a href='http://www.babylandibiza.com/servicios/#".$slug."'>". get_the_title() ."</a></div></div>";
endwhile;
wp_reset_query();
else:
$output .= "";
endif;
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment