Skip to content

Instantly share code, notes, and snippets.

@nielsvr
Last active Aug 29, 2015
Embed
What would you like to do?
<?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