{"id":138,"date":"2020-04-30T11:06:12","date_gmt":"2020-04-30T11:06:12","guid":{"rendered":"https:\/\/codexcoach.com\/?p=42"},"modified":"2023-10-19T05:54:39","modified_gmt":"2023-10-19T05:54:39","slug":"how-to-create-loop-element-using-visual-composer-api","status":"publish","type":"post","link":"https:\/\/codexcoach.com\/how-to-create-loop-element-using-visual-composer-api\/","title":{"rendered":"How to Create Loop Element Using Visual Composer API"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full is-style-twentytwentyone-border\"><img loading=\"lazy\" width=\"1667\" height=\"814\" src=\"https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.12.28-17_23_25.png\" alt=\"\" class=\"wp-image-1985\" srcset=\"https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.12.28-17_23_25.png 1667w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.12.28-17_23_25-300x146.png 300w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.12.28-17_23_25-1024x500.png 1024w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.12.28-17_23_25-768x375.png 768w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.12.28-17_23_25-1536x750.png 1536w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.12.28-17_23_25-1568x766.png 1568w\" sizes=\"(max-width: 1667px) 100vw, 1667px\" \/><\/figure>\n\n\n\n<p>When we are making site with visual composer some time not possible to built some designs using visual composer elements. So at that time we need to use HTML element for built designs. But visual composer API provide facility to built own custom element for some special requirements and its very easy. We can built custom element using <strong>vc_before_init<\/strong> action. Add below code in theme\u2019s <strong>function.php<\/strong> file.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"function.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_action( &#39;vc_before_init&#39;, &#39;cxc_create_loop_element_in_vc&#39; );\n\nfunction cxc_create_loop_element_in_vc() {\n\tvc_map(\n\t\tarray(\n\t\t\t&quot;name&quot; =&gt; __(&quot;My Loop Element&quot;, &quot;my-text-domain&quot;), \/\/ Element name\n\t\t\t&quot;base&quot; =&gt; &quot;cxc_my_loop_element&quot;, \/\/ Element shortcode\n\t\t\t&quot;class&quot; =&gt; &quot;loop-element&quot;,\n\t\t\t&quot;category&quot; =&gt; __(&#39;Content&#39;, &#39;my-text-domain&#39;),\n\t\t\t&quot;params&quot; =&gt; array(\n\t\t\t\tarray(\n\t\t\t\t\t&quot;type&quot; =&gt; &quot;textfield&quot;,\n\t\t\t\t\t&quot;holder&quot; =&gt; &quot;div&quot;,\n\t\t\t\t\t&quot;class&quot; =&gt; &quot;&quot;,\n\t\t\t\t\t&quot;heading&quot; =&gt; __(&quot;Title&quot;, &quot;my-text-domain&quot;),\n\t\t\t\t\t&quot;param_name&quot; =&gt; &quot;cxc_my_loop_element_title&quot;,\n\t\t\t\t\t&quot;value&quot; =&gt; __(&quot;&quot;, &quot;my-text-domain&quot;),\n\t\t\t\t\t&quot;description&quot; =&gt; __(&#39;Add a Main section\\&#39;s title.&quot;.&#39;, &quot;my-text-domain&quot;)\n\t\t\t\t),\n\t\t\t\tarray(\n\t\t\t\t\t&quot;type&quot; =&gt; &quot;loop&quot;,\n\t\t\t\t\t&quot;holder&quot; =&gt; &quot;div&quot;,\n\t\t\t\t\t&quot;class&quot; =&gt; &quot;&quot;,\n\t\t\t\t\t&quot;heading&quot; =&gt; __(&quot;Query&quot;, &quot;my-text-domain&quot;),\n\t\t\t\t\t&quot;param_name&quot; =&gt; &quot;cxc_my_loop_element_query&quot;,\n\t\t\t\t\t&quot;value&quot; =&gt; __(&quot;&quot;, &quot;my-text-domain&quot;)\n\t\t\t\t),\n\t\t\t\tarray(\n\t\t\t\t\t&quot;type&quot; =&gt; &quot;textfield&quot;,\n\t\t\t\t\t&quot;holder&quot; =&gt; &quot;div&quot;,\n\t\t\t\t\t&quot;class&quot; =&gt; &quot;&quot;,\n\t\t\t\t\t&quot;heading&quot; =&gt; __(&quot;Extra class name&quot;, &quot;my-text-domain&quot;),\n\t\t\t\t\t&quot;param_name&quot; =&gt; &quot;cxc_my_loop_element_extra_class&quot;,\n\t\t\t\t\t&quot;value&quot; =&gt; __(&quot;&quot;, &quot;my-text-domain&quot;),\n\t\t\t\t\t&quot;description&quot; =&gt; __(&#39;Style particular content element differently - add a class name and refer to it in custom CSS.&quot;.&#39;, &quot;my-text-domain&quot;)\n\t\t\t\t),\n\t\t\t)\n\t\t)\n\t);\n}\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<p>Now we should create shortcode for base <strong>cxc_my_loop_element<\/strong>. So we will create shortcode for <strong>cxc_my_loop_element<\/strong>. It will look like below.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"Function.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_shortcode( &#39;cxc_my_loop_element&#39;, &#39;cxc_my_loop_element_func&#39; );\n\nfunction cxc_my_loop_element_func( $query_loop ) {\n\tob_start();\n\t\n\t$query_loop = shortcode_atts(\n\t\tarray(\n\t\t\t&#39;cxc_my_loop_element_title&#39; =&gt; &#39;&#39;,\n\t\t\t&#39;cxc_my_loop_element_query&#39; =&gt; &#39;&#39;,\n\t\t\t&#39;cxc_my_loop_element_extra_class&#39; =&gt; &#39;&#39;,\n\t\t),\n\t\t$query_loop, &#39;cxc_my_loop_element&#39;\n\t);\n\n\t$title = isset( $query_loop[&#39;cxc_my_loop_element_title&#39;] ) ? $query_loop[&#39;cxc_my_loop_element_title&#39;] : &#39;&#39;;\n\t$class = isset( $query_loop[&#39;cxc_my_loop_element_extra_class&#39;] ) ? $query_loop[&#39;cxc_my_loop_element_extra_class&#39;] : &#39;&#39;;\n\t$query = isset( $query_loop[&#39;cxc_my_loop_element_query&#39;] ) ? $query_loop[&#39;cxc_my_loop_element_query&#39;] : &#39;&#39;;\n\n\t$query = explode(&#39;|&#39;, $query);\n\n\tif( $query ){\n\t\tforeach( $query as $query_part ) {\n\t\t\t$q_part = explode(&#39;:&#39;, $query_part);\n\t\t\tswitch( $q_part[0] ) {\n\n\t\t\t\tcase &#39;post_type&#39;:\n\t\t\t\t$query_post_type = explode(&#39;,&#39;, $q_part[1]);\n\t\t\t\tbreak;\n\n\t\t\t\tcase &#39;size&#39;:\n\t\t\t\t$query_posts_per_page = ($q_part[1] == &#39;All&#39; ? -1 : $q_part[1]);\n\t\t\t\tbreak;\n\n\t\t\t\tcase &#39;order_by&#39;:\n\t\t\t\t$query_meta_key = &#39;&#39;;\n\t\t\t\t$query_orderby = &#39;&#39;;\n\n\t\t\t\t$public_orders_array = array(&#39;ID&#39;, &#39;date&#39;, &#39;author&#39;, &#39;title&#39;, &#39;modified&#39;, &#39;rand&#39;, &#39;comment_count&#39;, &#39;menu_order&#39;);\n\t\t\t\tif( in_array( $q_part[1], $public_orders_array ) ) {\n\t\t\t\t\t$query_orderby = $q_part[1];\n\t\t\t\t} else {\n\t\t\t\t\t$query_meta_key = $q_part[1];\n\t\t\t\t\t$query_orderby = &#39;meta_value_num&#39;;\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\t\tcase &#39;order&#39;:\n\t\t\t\t$query_order = $q_part[1];\n\t\t\t\tbreak;\n\n\t\t\t\tcase &#39;by_id&#39;:\n\t\t\t\t$query_by_id = explode(&#39;,&#39;, $q_part[1]);\n\t\t\t\t$query_by_id_not_in = array();\n\t\t\t\t$query_by_id_in = array();\n\t\t\t\tif( $query_by_id ){\n\t\t\t\t\tforeach( $query_by_id as $ids ) {\n\t\t\t\t\t\tif ( $ids &lt; 0 ) {\n\t\t\t\t\t\t\t$query_by_id_not_in[] = $ids;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t$query_by_id_in[] = $ids;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\t\tcase &#39;categories&#39;:\n\t\t\t\t$query_categories = explode(&#39;,&#39;, $q_part[1]);\n\t\t\t\t$query_cat_not_in = array();\n\t\t\t\t$query_cat_in = array();\n\t\t\t\tif( $query_categories ){\n\t\t\t\t\tforeach( $query_categories as $cat ) {\n\t\t\t\t\t\tif ( $cat &lt; 0 ) {\n\t\t\t\t\t\t\t$query_cat_not_in[] = $cat;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t$query_cat_in[] = $cat;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\t\tcase &#39;tags&#39;:\n\t\t\t\t$query_tags = explode(&#39;,&#39;, $q_part[1]);\n\t\t\t\t$query_tags_not_in = array();\n\t\t\t\t$query_tags_in = array();\n\t\t\t\tif( $query_tags ){\n\t\t\t\t\tforeach( $query_tags as $tags ) {\n\t\t\t\t\t\tif ($tags &lt; 0) {\n\t\t\t\t\t\t\t$query_tags_not_in[] = $tags;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t$query_tags_in[] = $tags;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\t\tcase &#39;authors&#39;:\n\t\t\t\t$query_author = explode(&#39;,&#39;, $q_part[1]);\n\t\t\t\t$query_author_not_in = array();\n\t\t\t\t$query_author_in = array();\n\t\t\t\tif( $query_author ){   \n\t\t\t\t\tforeach( $query_author as $author ) {\n\t\t\t\t\t\tif ( $tags &lt; 0 ) {\n\t\t\t\t\t\t\t$query_author_not_in[] = $author;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t$query_author_in[] = $author;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\n\t\t\t\tcase &#39;tax_query&#39;:\n\t\t\t\t$all_tax = get_object_taxonomies($query_post_type);\n\t\t\t\t$tax_query = array();\n\t\t\t\t$query_tax_query = array(&#39;relation&#39; =&gt; &#39;AND&#39;);\n\t\t\t\tif( $all_tax ){\n\t\t\t\t\tforeach( $all_tax as $tax ) {\n\t\t\t\t\t\t$values = $tax;\n\t\t\t\t\t\t$query_taxs_in = array();\n\t\t\t\t\t\t$query_taxs_not_in = array();\n\t\t\t\t\t\t$query_taxs = explode(&#39;,&#39;, $q_part[1]);\n\t\t\t\t\t\tif( $query_taxs ){\n\t\t\t\t\t\t\tforeach( $query_taxs as $taxs ) {\n\t\t\t\t\t\t\t\tif ( term_exists( absint( $taxs ), $tax ) ) {\n\t\t\t\t\t\t\t\t\tif ($taxs &lt; 0) {\n\t\t\t\t\t\t\t\t\t\t$query_taxs_not_in[] = absint($taxs);\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\t$query_taxs_in[] = $taxs;\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif ( count( $query_taxs_not_in ) &gt; 0 ) {\n\t\t\t\t\t\t\t$query_tax_query[] = array(\n\t\t\t\t\t\t\t\t&#39;taxonomy&#39; =&gt; $tax,\n\t\t\t\t\t\t\t\t&#39;field&#39; =&gt; &#39;id&#39;,\n\t\t\t\t\t\t\t\t&#39;terms&#39; =&gt; $query_taxs_not_in,\n\t\t\t\t\t\t\t\t&#39;operator&#39; =&gt; &#39;NOT IN&#39;,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t} else if( count( $query_taxs_in ) &gt; 0 ) {\n\t\t\t\t\t\t\t$query_tax_query[] = array(\n\t\t\t\t\t\t\t\t&#39;taxonomy&#39; =&gt; $tax,\n\t\t\t\t\t\t\t\t&#39;field&#39; =&gt; &#39;id&#39;,\n\t\t\t\t\t\t\t\t&#39;terms&#39; =&gt; $query_taxs_in,\n\t\t\t\t\t\t\t\t&#39;operator&#39; =&gt; &#39;IN&#39;,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t}\n\n\t$query_final = array(\n\t\t&#39;post_type&#39; =&gt; $query_post_type,\n\t\t&#39;post_status&#39; =&gt; &#39;publish&#39;,\n\t\t&#39;posts_per_page&#39; =&gt; $query_posts_per_page,\n\t\t&#39;orderby&#39; =&gt; $query_orderby,\n\t\t&#39;order&#39; =&gt; $query_order,\n\t\t&#39;paged&#39; =&gt; $paged,\n\t\t&#39;post__in&#39; =&gt; $query_by_id_in,\n\t\t&#39;post__not_in&#39; =&gt; $query_by_id_not_in,\n\t\t&#39;category__in&#39; =&gt; $query_cat_in,\n\t\t&#39;category__not_in&#39; =&gt; $query_cat_not_in,\n\t\t&#39;tag__in&#39; =&gt; $query_tags_in,\n\t\t&#39;tag__not_in&#39; =&gt; $query_tags_not_in,\n\t\t&#39;author__in&#39; =&gt; $query_author_in,\n\t\t&#39;author__not_in&#39; =&gt; $query_author_not_in,\n\t\t&#39;tax_query&#39; =&gt; $query_tax_query\n\t);\n\n\t$my_query = new WP_Query( $query_final );\n\n\tif( $my_query-&gt;have_posts() ) {\n\t\t?&gt;\n\t\t&lt;div class=&quot;main_title &lt;?php echo $class; ?&gt;&quot;&gt;\n\t\t\t&lt;h2&gt;&lt;?php echo $title; ?&gt;&lt;\/h2&gt;\n\t\t\t&lt;?php\n\t\t\twhile ($my_query-&gt;have_posts()) : $my_query-&gt;the_post();\n\t\t\t\t?&gt;\n\t\t\t\t&lt;!-- add your code here.  --&gt;\n\t\t\t\t&lt;?php\n\t\t\tendwhile;\n\t\t}\n\t\t?&gt;\n\t&lt;\/div&gt;\n\t&lt;?php\n\twp_reset_query();\n\n\t$result = ob_get_clean();\n\treturn $result;\n}\n?&gt;<\/code><\/pre><\/div>\n<div id=\"ezoic-pub-ad-placeholder-118\"><\/div>","protected":false},"excerpt":{"rendered":"<p>When we are making site with visual composer some time not possible to built some designs using visual composer elements. So at that time we need to use HTML element for built designs. But visual composer API provide facility to built own custom element for some special requirements and its very easy. We can built [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":2517,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_aib_schema_json_ld":""},"categories":[10,11],"tags":[],"_links":{"self":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/138"}],"collection":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/comments?post=138"}],"version-history":[{"count":8,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":13211,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/138\/revisions\/13211"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media\/2517"}],"wp:attachment":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media?parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/categories?post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/tags?post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}