writing shortcode function from template code

1 replies
How would I write a WordPress shortcode function from this:
Code:
<?php global ; // required
 = array('numberposts'=>1, 'category'=>75,39,46,23, 'order'=>'ASC');
 = get_posts();
foreach( as ) : setup_postdata();?>
<h6 class="home-feature"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h6>
<?php
the_post_thumbnail(); 
the_excerpt( sprintf(__( 'Read More<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ),
	get_the_title()) );  ?>
<a href="<?php echo get_permalink(); ?>" class="purplez"> Read More</a>
<?php  endforeach;
?>
#code #function #shortcode #template #writing
  • Profile picture of the author Nathan K
    I have not tested this but it would be something like this.

    function shortcode_showposts( $attrs, $content = null ){

    global $post;

    $output = '';

    $args = array(
    'numberposts'=> 1,
    'category'=> 75,39,46,23,
    'order'=>'ASC'
    );

    $posts = get_posts( $args );

    foreach($posts as $post){
    setup_postdata($post);
    $output .= sprintf(__('<h6 class="home-feature"><a href="%s" title="%s">%s</a></h6>', 'twentyseventeen'), get_permalink(), the_title_attribute( 'echo=0' ), get_the_title());
    $output .= get_the_post_thumbnail();
    $output .= sprintf(__('Read More<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ), get_the_title());
    $output .= sprintf(__('<a href="%s" class="purplez">Read More</a>','twentyseventeen'));
    }

    return $output;
    }

    add_shortcode('showposts', 'shortcode_showposts');
    {{ DiscussionBoard.errors[11003515].message }}

Trending Topics