Skip to content

Instantly share code, notes, and snippets.

@nielsvr
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nielsvr/ad6f7d36b18d2e6a33da to your computer and use it in GitHub Desktop.
Save nielsvr/ad6f7d36b18d2e6a33da to your computer and use it in GitHub Desktop.
<?php
function display_posts_function() {
$return = "";
$cats = get_categories('hide_empty=0&orderby=name&order=asc');
foreach ($cats as $cat) :
$return .= '<ul class="list"><li class="name-cat"><a href="' . get_category_link( $cat->term_id ) . '" title="' . $cat->name . '" >' . $cat->name . '</a></li>';
$args = array(
'posts_per_page' => -1, // max number of post per category
'cat' => $cat->term_id
);
$posts = get_posts($args);
if ( count($posts) > 0 ) :
foreach( $posts AS $single_post ) :
$return .= '<li class="name-post">';
$return .= '<a href="'.get_permalink( $single_post->ID ).'" rel="bookmark">'.get_the_title( $single_post->ID ).'</a>';
$return .= '</li>';
endforeach;
endif;
$return .= '</ul>';
endforeach;
return $return;
}
function register_shortcodes(){
add_shortcode('myshortcode', 'display_posts_function');
}
add_action( 'init', 'register_shortcodes');
// [myshortcode]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment