{"id":1997123,"date":"2021-03-23T08:21:05","date_gmt":"2021-03-23T08:21:05","guid":{"rendered":"https:\/\/toolset.com\/?post_type=course-lesson&#038;p=1997123"},"modified":"2021-04-14T09:34:42","modified_gmt":"2021-04-14T09:34:42","slug":"adding-custom-javascript-to-views-templates-and-archives","status":"publish","type":"course-lesson","link":"https:\/\/toolset.com\/es\/course-lesson\/adding-custom-javascript-to-views-templates-and-archives\/","title":{"rendered":"Adding Custom JavaScript to Views, Templates and Archives"},"content":{"rendered":"\n<p>Toolset allows you to add custom JavaScript code directly to your Views, Content Templates, and WordPress Archives. Like everything else in Toolset, you do this from the Block Editor itself.<\/p>\n\n\n\n<p>This way, you don&#8217;t have to add or edit any files in your theme.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps for adding JavaScript to a View<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Edit the page or a template with the View you want to add JavaScript to.<\/li><li>Select the <strong>View<\/strong> in block navigation.&nbsp;<\/li><li>In the right sidebar expand the <strong>Custom JS and CSS <\/strong>section and paste the code into the <strong>Custom JS<\/strong> text area.<\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"4ee142c81fb6a30681026dfbf3349f92\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/view-block-custom-js.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/view-block-custom-js-500x225.png\" alt=\"\" class=\"wp-image-1997155\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Adding custom JavaScript to a View<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>Click the <strong>pencil<\/strong> icon to open a pop-up dialog for more convenient pasting and editing of your custom code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps for adding JavaScript to a template<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Go to <strong>Toolset \u2192 Content Templates<\/strong> and click the Content Template you wish to edit.<\/li><li>In the right sidebar, scroll to the <strong>JS editor<\/strong> section with a text area for entering your code.<\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"8e7ae8df636c291de339c1c92b02c5e4\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/content-template-js.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/content-template-js-500x243.png\" alt=\"\" class=\"wp-image-1997277\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Adding custom JavaScript to a Content Template<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Steps for adding JavaScript to an archive<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Go to <strong>Toolset \u2192 WordPress Archives <\/strong>and click the archive you want to edit.<\/li><li>In the right sidebar, make sure you&#8217;re on the <strong>WordPress Archive <\/strong>tab.<\/li><li>Expand the <strong>Custom CSS and JS<\/strong> section and paste the code into the <strong>Custom JS<\/strong> text area.<\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-dynamic aligncenter\" data-toolset-blocks-image=\"e46d57a3a10312173c21455bacff9a44\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/04\/toolset-add-custom-javascript-to-wordpress-archives.png\"><img decoding=\"async\" width=\"1502\" height=\"639\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/04\/toolset-add-custom-javascript-to-wordpress-archives.png\" alt=\"\" class=\"wp-image-2020495\" srcset=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/04\/toolset-add-custom-javascript-to-wordpress-archives.png 1502w, https:\/\/toolset.com\/wp-content\/uploads\/2021\/04\/toolset-add-custom-javascript-to-wordpress-archives-300x128.png 300w, https:\/\/toolset.com\/wp-content\/uploads\/2021\/04\/toolset-add-custom-javascript-to-wordpress-archives-1024x436.png 1024w, https:\/\/toolset.com\/wp-content\/uploads\/2021\/04\/toolset-add-custom-javascript-to-wordpress-archives-150x64.png 150w, https:\/\/toolset.com\/wp-content\/uploads\/2021\/04\/toolset-add-custom-javascript-to-wordpress-archives-768x327.png 768w, https:\/\/toolset.com\/wp-content\/uploads\/2021\/04\/toolset-add-custom-javascript-to-wordpress-archives-805x342.png 805w\" sizes=\"auto, (max-width: 1502px) 100vw, 1502px\" \/><\/a><div class=\"tb-image-caption\"><figcaption>Adding custom JavaScript to a WordPress Archive<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>Click the <strong>pencil <\/strong>icon to open a pop-up dialog for more convenient pasting and editing of your custom code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Achieving great results with little coding by using jQuery<\/strong><\/h2>\n\n\n\n<p>jQuery is a JavaScript library that allows you to achieve impressive results with little effort. jQuery allows you to produce many visual effects, including show, hide, scroll, and fade, by using only a few lines of code.<\/p>\n\n\n\n<p>The following example uses jQuery to toggle the display of the \u201cmy-contents\u201d section on a page whenever a link or a button (labeled \u201cshow\u201d) is clicked:<\/p>\n\n\n\n                    <div class='codesnipdiv' id='bdc6b'>\n                    <a class='\tjQuery-show-hide-example' data-titleselector='jquery-show-hide-example' data-randspinner ='bdc6b' data-sniphost='1997123' title='Click to Expand or Minimize' id='codesnipfrontend'><img src='https:\/\/toolset.com\/wp-content\/plugins\/code-snips\/res\/img\/code_icon.png' \/>jQuery-show-hide-example<\/a>\n                    <div id='bdc6b_innerdiv' class='clearfix'>\n                    <pre class='brush:php;'>jQuery( document ).on( &quot;click&quot;, &quot;#show&quot;, function() {\r\n  jQuery( &quot;#my-contents&quot; ).slideToggle( &quot;slow&quot;, function() {\r\n  });\r\n});<\/pre>\n                    <script>\n                    if ('undefined' === typeof cacheObj_open) {\n                        window.cacheObj_open = {};\n                    }\n                    SyntaxHighlighter.highlight();\n                    <\/script>\n                    <input type='hidden' id='bdc6b_minimize' value='Minimize'>\n                    <input type='hidden' id='bdc6b_cachedstartopen' value='cachedstartopen'>\n                    <\/div>\n                    <\/div>\t\t\t\n                    <script>\t\t\t\t\t\t\t\n                    var insertname_outputs_startopen\t= 'bdc6b_jquery-show-hide-example';\t \t\t\t\n                    cacheObj_open[insertname_outputs_startopen]='<pre class=\"brush: php;\">jQuery( document ).on( &quot;click&quot;, &quot;#show&quot;, function() {\\r\\n  jQuery( &quot;#my-contents&quot; ).slideToggle( &quot;slow&quot;, function() {\\r\\n  });\\r\\n});<\/pre>';            \n                    <\/script>\n\n\n\n<p>Notice the syntax of this code. Even without full knowledge of jQuery or JavaScript, we can determine what this code does. Based on this example, we can tell that the toggle will happen slowly.<\/p>\n\n\n\n<p>jQuery ships with WordPress and loads automatically. You need to understand only a few of its features and functions before you can begin using it.<\/p>\n\n\n\n<p><strong>Tip for advanced jQuery users:<\/strong> to avoid conflicts, use the standard namespace instead of the \u201c$\u201d symbol in your scripts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Further reading<\/strong><\/h2>\n\n\n\n<p>If you are using JavaScript and jQuery for the first time, or you want to expand your knowledge of this scripting language, many great learning resources are available online. The following are exceptional training aids that we recommend:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.learn-js.org\/\">interactive online JavaScript tutorial<\/a><\/li><li><a href=\"http:\/\/javascriptissexy.com\/how-to-learn-javascript-properly\/\">comprehensive JavaScript study guide<\/a><\/li><li><a href=\"http:\/\/learn.jquery.com\/\">jQuery learning center<\/a><\/li><\/ul>\n\n\n\n<p>Need other JavaScript libraries?<\/p>\n\n\n\n<p>You can obtain access to additional libraries through your theme\u2019s <em>functions.php<\/em> file. To learn how to do this, please refer to the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/wp_enqueue_script\">WordPress official reference page for the wp_enqueue_script function<\/a>.<\/li><li><a href=\"http:\/\/code.tutsplus.com\/articles\/a-beginners-guide-to-enqueuing-jquery--wp-30615\">A Beginner\u2019s Guide to Enqueuing jQuery tutorial, available on the Tutsplus website<\/a>.<\/li><\/ul>\n\n\n\n<p><\/p>\n","protected":false},"author":84277,"featured_media":1997373,"template":"","related-lesson":[8409,8427,8411],"class_list":["post-1997123","course-lesson","type-course-lesson","status-publish","has-post-thumbnail","hentry","related-lesson-archive-pages","related-lesson-post-templates","related-lesson-view"],"_links":{"self":[{"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/course-lesson\/1997123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/course-lesson"}],"about":[{"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/types\/course-lesson"}],"author":[{"embeddable":true,"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/users\/84277"}],"version-history":[{"count":7,"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/course-lesson\/1997123\/revisions"}],"predecessor-version":[{"id":2020499,"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/course-lesson\/1997123\/revisions\/2020499"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/media\/1997373"}],"wp:attachment":[{"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/media?parent=1997123"}],"wp:term":[{"taxonomy":"related-lesson","embeddable":true,"href":"https:\/\/toolset.com\/es\/wp-json\/wp\/v2\/related-lesson?post=1997123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}