在wordpress的主页上自定义css和js

时间:2016-04-08 20:05:14

标签: javascript jquery html css wordpress

我的html主页的标题如下所示。

<!--referring to CSS-->

<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="css/animate.min.css">


<script type="text/javascript" src="http://updateyourbrowser.net/uyb.js"> </script> <!-- Update older browser -->
<!-- load the javascript library for the function in the script DOM-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!--load the javascript library-->
<script src="js/external/jquery/jquery.js"></script>
<!--??what is this? we load the javascript library-->
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<!-- Google Analytics script -->
<script>//Google Analytics script
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-61904449-3', 'auto');
  ga('send', 'pageview');

</script>


<!-- Facebook Pixel Code -->

<script>

!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?

n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;

n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;

t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,

document,'script','//connect.facebook.net/en_US/fbevents.js');



fbq('init', '384494271757075');

fbq('track', "PageView");</script>

<noscript><img height="1" width="1" style="display:none"

src="https://www.facebook.com/tr?id=384494271757075&ev=PageView&noscript=1"

/></noscript>

<!-- End Facebook Pixel Code -->


<script>


    // Scrolling function for the top navigation
// ??top left? which navigation? => when you click to go back to the top, to make an ease effect
    $(document).ready(function(){
        $('a').click(function(){
            $('html, body').animate({
                scrollTop: $( $(this).attr('href') ).offset().top
            }, {duration: 1500, easing: 'easeInOutExpo'});
            return false;
        }); 
    });



<!--The following script tag downloads a font from the Adobe Edge Web Fonts server for use within the web page. -->
<script>var __adobewebfontsappname__="dreamweaver"</script>
<script src="http://use.edgefonts.net/source-sans-pro:i6,n6,n4,i4,i3,n3,n7,i7:default.js" type="text/javascript"></script>

我需要将它作为Wordpress主题的主页。我把这个页面作为主题的主页通过方法 https://www.youtube.com/watch?v=ClXmWsE7wo8, 我打算将此方法用于自定义css和Js How to add custom css/js to just one page in WordPress?

但是我在定义自定义CSS和JS时遇到了问题。

在我的主页标题中,有 1.从外部调用的css文件 2. js从外面打来电话 3. js定义

我的第一个问题是,对于1和2,我如何在wp_enqueue_script中定义路径('your_script','path'); wp_enqueue_style('your-style','path');?

我应该

wp_enqueue_style( 'main.css','/tstandtest/css/main.css' );
		wp_enqueue_style( 'font-awesome.min.css','/tstandtest/font-awesome/css/font-awesome.min.css' );
		wp_enqueue_style( 'jquery-ui.css','/tstandtest/font-awesome/css/font-awesome.min.css' );
		wp_enqueue_style( 'font-awesome.min.css','//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css' );

我的第二个问题是,对于html标题中定义的js,如何使其作为wordpress中的主页工作,例如google analytics脚本和Facebook像素脚本?

1 个答案:

答案 0 :(得分:0)

首先,如果您尚未获得主题资产的正确路径,则可能需要使用get_template_directory_uri()

// example
wp_enqueue_style('sage/css', get_template_directory_uri() . '/dist/styles/main.css', false, null);

你的第二个问题我完全不了解。如果您对ga和facebook脚本进行了硬编码,它应该可以正常工作。但是我会像 this 这样的插件管理插入最新的ga代码。

并且,我没有看到您链接的视频教程,我认为它需要在<?php wp_head(); ?>标记之前放置</head>:{{3}并且您已从模板文件中删除了所有引用的样式表和外部脚本文件。

您可能还想关闭以:

开头的脚本块
<script>


    // Scrolling function for the top navigation
    // ??top left? which navigation? => when you click to go back to the top, to make an ease effect
    $(document).ready(function(){

最后,我最后检查过Wordpress要求你指定jQuery对象的全名,因为它默认设置为&#34; no-conflict&#34;模式。

为了帮助解决这个问题,我使用这个方便的闭包,传入完整的jQuery对象:

(function ($) {

    // Now we can use $ within this closure

    $(document).ready(UTIL.loadEvents);

})(jQuery);