Lensa Blog Shortcode

Hello again!

There are a lot of question about this Lensa theme coming into my mail. Some of them is interesting, for example is this question:

How can i have a blog template that can displaying a posts from my specific category. For example if i have 2 or more categories, i want to have that blog page for each of them.

If you are using the standard WordPress functions, you will never able to achieve that. The main question is how to add the category into the page. The only solution is by using a shortcode. By using a shortcode, you can do everything you want.

To do this modification, you only have to edit 1 file: functions.php.

Step #1

At this step you have to edit your Lensa’s functions.php. Open the file and put this code at the very end of it:

function custom_blog($atts){
   extract(shortcode_atts(array(
      'cat' => 1,
   ), $atts));

	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
	query_posts(array('post_type' => 'post', 'paged'=>$paged, 'cat' => $cat));
	  if(have_posts()) :
?>

   <div class="main-content block-background column col8">
      <div class="block-inner">
	<?php while(have_posts()): the_post(); ?>
        <article class="entry-post">

          <header class="entry-header">
            <h2 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
		    <?php colabs_post_meta(); ?>
          </header>

          <figure class="entry-media">
			<?php
					$single_top = get_post_custom_values("colabs_single_top");
					if (($single_top[0]!='')||($single_top[0]=='none')){
						if ($single_top[0]=='single_video'){
							$embed = colabs_get_embed('colabs_embed',400,231,'single_video',$post->ID);
							if ($embed!=''){
								echo '<div class="single_video">'.$embed.'</div>';
							}
						}elseif($single_top[0]=='single_image'){
							colabs_image('width=620&link=img');
						}
					}else{
						colabs_image('width=620&link=img');
					}
			?>
          </figure><!-- .entry-media -->

          <div class="entry-content">
			<?php colabs_custom_excerpt(); ?>
			<p class="more"><a href="<?php the_permalink() ?>"><?php _e("More","colabsthemes"); ?></a></p>
		  </div><!-- .entry-content -->

        </article>

    <?php
	  endwhile;
	  colabs_pagenav();
	  endif;
	?>
	  </div><!-- .block-inner -->
    </div><!-- .main-content -->
	<?php
	get_sidebar();
    wp_reset_query();
}

function register_shortcodes(){
   add_shortcode('post-blog', 'custom_blog');
}

add_action( 'init', 'register_shortcodes');

The first step is done. Right now, the shortcode can be used. And here are some explanations about the code:

function register_shortcodes(){
   add_shortcode('post-blog', 'custom_blog');
}

add_action( 'init', 'register_shortcodes');

Those code is like telling the WordPress if you have a shortcode, please register them. And when i need it (call it inside a post/page), please give a response.

The response is the codes inside the loop.

Step #2

At this step you won’t do more coding. At this step i will show you how to use the shortcode.

You can use the shortcode at your post/page by adding this code:

[post-blog cat="1"]

The cat value is up to you. The value is the your category ID. You can add more than 1 ID by separating them by a coma.

Note: Make sure if you are not removing the quotes.

Tips

When you add the shortcode into your post/page, the comment forms will be displayed.

download (2)

 

When you create a new post/page, make sure if you click on the screen options button on the right top of your page and activate an option named discussion.

Once you made it a box named discussion will be displayed. Turn off the option named allow comments and allow trackbacks and your shortcode result will looked like a normal page (no one will know :)).

Note: This modification can also be used for another ColorLabs theme.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s