1

I have this code to show all posts of category and thumbnail for the first post of them.

<?php $recent = new WP_Query(); ?>
<?php $recent->query( 'cat=1&showposts=5' ); ?>
<?php $is_first_post = true; ?>
<?php while( $recent->have_posts() ) : $recent->the_post(); ?>
    <ul>
      <li>
      <?php 
      if ( $is_first_post  && has_post_thumbnail() ) {
        the_post_thumbnail(); 
        $is_first_post = false; 
        }
        ?>
        <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
        </a>
      </li>
    </ul>
<?php endwhile; ?>

I want to run this code by using a shortcode, which is using category and post number.

How do I create such a shortcode in WordPress?

4 Answers 4

2

Add this code in function.php and this is your shortcode "[my_form_shortcode cat="1" showposts="5"]".

function my_form_shortcode($atts) {
 ob_start();
 $atts = shortcode_atts(
 array(
        'cat' => '1',
        'showposts' => '5',
 ), $atts, 'my_form_shortcode' );

//YOUR CODE START

 $recent = new WP_Query(); 
 $query = "cat=".$atts['cat']."&showposts=".$atts['showposts'];
 $recent->query( $query ); 
 $is_first_post = true; 
 while( $recent->have_posts() ) : $recent->the_post(); ?>
<ul>
  <li>
  <?php 
   if ( $is_first_post  && has_post_thumbnail() ) {
    the_post_thumbnail(); 
    $is_first_post = false; 
   }
  ?>
  <a href="<?php the_permalink(); ?>">
  <?php the_title(); ?>
  </a>
 </li>
 </ul>
 <?php endwhile; 
 //YOUR CODE END

 return ob_get_clean(); 
 }

add_shortcode( 'my_form_shortcode', 'my_form_shortcode' );
Sign up to request clarification or add additional context in comments.

2 Comments

hi are you there sir?
Add me on Skype prashant1879
0

A shortcode is a PHP function. You need a function that accepts all your arguments. For example-

function get_posts($atts) {
  extract( shortcode_atts( array(
    'cat_id' => 'cat_id',
    'num_posts' => 'num_posts'
  ), $atts ) );

  $loop = array(
    'cat' => $cat_id,
    'posts_per_page' => $num_posts
  );

  if ( $loop->have_posts() ) :
    while ( $loop->have_posts() ) : $loop->the_post();
      // YOUR CODE HERE
    endwhile;
  endif;
}

add_shortcode( 'getposts', 'get_posts' );

Your Shortcode will look like this-

getposts[cat_id="1", num_posts="5"]

This code have not been tested but this is pretty much how you do it

1 Comment

sir, This code showing error when i put in functions.php .how can i do now?
0

First of all you just change your function name.

In wordpress get_posts() is one function so you did not create your custom function same name.

https://developer.wordpress.org/reference/functions/get_posts/

Comments

0
function ShowProduct()
{
  $data = "Welcome to wordpress shortcode.";
  return $data;
}

add_shortcode('products', 'ShowProduct');

http://www.codexwp.com/issues/how-to-create-shortcode-in-wordpress/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.