0
<?php




add_shortcode('Book','book_shortcode_templete');

function book_shortcode_templete($atts, $content)
{

  $attributes = shortcode_atts(
        [
            'id'          => 0,
            'author_name' => '',
            'publisher'   => '',
            'year'        => 0000,
            'tag'         => '',
            ''    => '',
        ],
        $atts,
        'book'
    );



if ($attributes['category'] != "" || $attributes["tag"] != "") {
    $args = [
        'p'              => $attributes['id'],
        'post_type'      => 'book',
        'post_status'    => 'publish',
        'tax_query'      => [
            'relation' => 'OR',
            [
                'taxonomy'         => 'Book Category',
                'field'            => 'slug',
                'terms'            => explode(',', $attributes['category']),
                'include_children' => true,
                'operator'         => 'IN',
            ],
            [
                'taxonomy'         => 'Book Tag',
                'field'            => 'slug',
                'terms'            => explode(',', $attributes['tag']),
                'include_children' => false,
                'operator'         => 'IN',
            ],
        ],
    ];
} else if ($attributes['author_name'] != "" || $attributes["publisher"] != "" || $attributes["year"] != "") {
   
    $args = [
        'post_type'      => 'book',
        'post_status'    => 'publish',
       
        'meta_query'     => [
            'relation' => 'OR',
            [
                'key'     => 'book_author',
                'value'   => explode(',', $attributes['author_name']),
                'compare' => 'IN',
            ],
            [
                'key'     => 'book_publisher',
                'value'   => explode(',', $attributes['publisher']),
                'compare' => 'IN',
            ],
            [
                'key'     => 'book_published_date',
                'value'   => explode(',', $attributes['year']),
                'compare' => 'IN',
            ],
        ],
    ];
} else {
    $args = [
        'p'              => $attributes['id'],
        'post_type'      => 'book',
        'post_status'    => 'publish',
        
    ];
}//end if

$query = new WP_Query($args);
if ($query->have_posts() == true) {
    while ($query->have_posts() == true) {
        $query->the_post();
       
      $price      = get_metadata('book', get_the_ID(), 'book_price', true);


        // Iterate post index in loop.
        $content .= '<article id="Book-'.get_the_ID().'">';
        $content .= '<center><h3 style="color: maroon;">'.get_the_title().'</h3></center>';
        $content .= '<p>'.get_the_content().'</p>';
      $content .= '<p>Author :- '.get_metadata('book', get_the_ID(), 'author_name', true);
        $content .= '<br> publisher :- '.get_metadata('book', get_the_ID(), 'book_publisher', true);
        $content .= '<br> year :- '.get_metadata('book', get_the_ID(), 'book_published_date', true);
       $content .= '<br> price :- ' .$price ;
        $content .= '<br> URL :- <a href='.get_metadata('book', get_the_ID(), 'book_url', true).'>'.get_metadata('book', get_the_ID(), 'book_url', true).'</a>';
        $content .= '</article>';
    }//end while
} else {
    $content .= "No Book Found....";
}

return $content;
return $attributes;


}
1
  • 2
    What is a "shortcode name book"? Commented Dec 27, 2020 at 13:53

1 Answer 1

0

hmm ok I see what you are trying to do. Ok no problem I can help. Use the following site to create a short code by generating the function for a short code then adding the code it generates to your functions.php file: https://generatewp.com/shortcodes/

As long as you fill in book on each code generator with “book” and the information it asks for it will create the post type and the short code for you. Good luck

6
  • Thanks for your support. i generate this code but i want dynamic output of author,publisher,year,price and url. but i can't fetch it ... Commented Dec 28, 2020 at 3:22
  • Yes ok that other data you want to store for each book is post metadata. Adding places to store all that is a different and more complicated question. This has answered the question you asked but that is something else.
    – brothman01
    Commented Dec 28, 2020 at 19:37
  • yes i want to store that data for each book ...and want to show with the help of shortcode. Commented Dec 29, 2020 at 4:58
  • this is my assignment learn.rtcamp.com/topic/plugin-development-assignment i am on creating shortcode step.please help me Commented Dec 29, 2020 at 5:08
  • you can find my project on github hvsr26 Commented Dec 29, 2020 at 5:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.