{"id":62,"date":"2017-07-06T07:17:25","date_gmt":"2017-07-06T07:17:25","guid":{"rendered":"http:\/\/webprow.com\/blog\/?p=62"},"modified":"2017-11-15T05:31:25","modified_gmt":"2017-11-15T05:31:25","slug":"shortcode-creation-cpt","status":"publish","type":"post","link":"https:\/\/webprow.com\/blog\/shortcode-creation-cpt\/","title":{"rendered":"Shortcode creation for CPT"},"content":{"rendered":"<p>Create the shortcode for our custom post type.<\/p>\n<p><strong>Step 1:<\/strong> Add the below code into <strong>functions.php<\/strong><\/p>\n<p>Here custom post type name is <strong>&#8216;events&#8217;<\/strong><\/p>\n<pre><code>add_shortcode( 'all-events', 'display_custom_post_type' );\r\n\r\nfunction display_custom_post_type()\r\n{\r\n $args = array(\r\n 'post_type' =&gt; 'events',\r\n 'post_status' =&gt; 'publish'\r\n );\r\n\r\n$string = '';\r\n $query = new WP_Query( $args );\r\n if( $query-&gt;have_posts() ){\r\n $string .= '&lt;ul&gt;';\r\n while( $query-&gt;have_posts() ){\r\n $query-&gt;the_post();\r\n $string .= '&lt;li&gt;' . get_the_title() . '&lt;\/li&gt;';\r\n $string .= '&lt;p&gt;' . get_the_excerpt() . '&lt;\/p&gt;';\r\n }\r\n $string .= '&lt;\/ul&gt;';\r\n }\r\n wp_reset_postdata();\r\n return $string;\r\n }<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Add the short code wherever you want to display the events list<\/p>\n<pre><code>[all-events]<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Create the shortcode for our custom post type. Step 1: Add the below code into functions.php Here custom post type name is &#8216;events&#8217; add_shortcode( &#8216;all-events&#8217;, &#8216;display_custom_post_type&#8217; ); function display_custom_post_type() { $args = array( &#8216;post_type&#8217; =&gt; &#8216;events&#8217;, &#8216;post_status&#8217; =&gt; &#8216;publish&#8217; ); $string = &#8221;; $query = new WP_Query( $args ); if( $query-&gt;have_posts() ){ $string .= &#8216;&lt;ul&gt;&#8217;; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":67,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[12],"class_list":["post-62","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-cpt"],"_links":{"self":[{"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":2,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":199,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/posts\/62\/revisions\/199"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/media\/67"}],"wp:attachment":[{"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webprow.com\/blog\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}