如何编写自定义短码代码簿?

时间:2020-12-27 作者:Harshvardhan
<?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 个回复
SO网友:brothman01

嗯,好吧,我知道你想做什么了。好的,没问题,我可以帮忙。通过为短代码生成函数,然后将生成的代码添加到函数中,可以使用以下站点创建短代码。php文件:https://generatewp.com/shortcodes/

只要您在每个代码生成器上的book中填写“book”及其要求的信息,它就会为您创建帖子类型和简短代码。祝你好运

相关推荐

Image Shortcode from ACF ID

我想创建一个快捷码,用于显示ACF自定义字段中的图像,该字段当前的返回格式设置为Image ID. 短代码需要接受post\\u id,以便从特定页面获取字段。因此,短代码是:[acf_featured_item post_id=\"88\"] 我知道有一个可用的ACF快捷码,但已经有另一个位置依赖于ID的返回格式。因此我无法更改此位置。我当前的代码是:function acf_featured_item_shortcode( $atts ) { // Attributes $a