最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - How to write a custom shortcode name book?

programmeradmin0浏览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;


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


}
Share Improve this question edited Dec 27, 2020 at 13:53 Jacob Peattie 44.1k10 gold badges50 silver badges64 bronze badges asked Dec 27, 2020 at 13:08 HarshvardhanHarshvardhan 1 1
  • 2 What is a "shortcode name book"? – Jacob Peattie Commented Dec 27, 2020 at 13:53
Add a comment  | 

1 Answer 1

Reset to default 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/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

发布评论

评论列表(0)

  1. 暂无评论