Support » Themes and Templates » Help wanted with shortcode to display categories / posts

  • Resolved Guido

    (@guido07111975)


    Hi,

    When I add this code to a template file it will display a nice list of my Categories and Posts:

    <?php  $cats = get_categories('hide_empty=0&orderby=name&order=asc');
    
    foreach ($cats as $cat) :
    
    	echo '<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
    );
    
    query_posts($args); 
    
    if (have_posts()) :
    
    	while (have_posts()) : the_post(); ?>
    		<li class="name-post">
    		<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf(__('Permalink to %s', 'translation'), the_title_attribute('echo=0')); ?>"> <?php the_title(); ?></a>
    		</li>
    	<?php endwhile;
    endif;
    
    wp_reset_query(); ?>
    
    <?php echo '</ul>'; ?>
    
    <?php endforeach; ?>

    Code above works 100%. But I want to turn this into a shortcode, so I can add this shortcode while creating a post/page.

    But, simply adding this before and after this code results in an error:

    function display_posts_function() {
    My code above
    }

    Btw, of course I have added this too, otherwise nothing happens:

    function register_shortcodes(){
       add_shortcode('myshortcode', 'display_posts_function');
    }
    add_action( 'init', 'register_shortcodes');

    So, who can help me?

    Guido

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Help wanted with shortcode to display categories / posts’ is closed to new replies.