How to write a custom shortcode name book?

Question
<?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;

}

0
Harshvardhan 3 months 2020-12-27T07:10:29-05:00 0 Answers 4 views 0

Leave an answer

Browse
Browse