WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Problems Getting jQuery Masonry To Work In WordPress (7 posts)

  1. antonel1
    Member
    Posted 1 year ago #

    Hello,

    I'm trying to incorporate jQuery Masonry into my wordpress theme. I understand how it works. I've created a sample of it on a plain html page and everything works fine. I've now inserted the same template in wordpress, inserting the same css and js files in my wordpress theme, but it doesn't work. I'm not certain it's picking up the js file.

    This is what I have in my head.php file:

    <?php
    
    		?><!DOCTYPE html>
    
    <html <?php language_attributes(); ?>>
    
    <head>
    
    		<meta charset="<?php bloginfo( 'charset' ); ?>" />
    		<title><?php
    		wp_title( '|', true, 'right' );
    
    		?></title>
    		<link rel="profile" href="http://gmpg.org/xfn/11" />
    		<link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo( 'stylesheet_url' ); ?>" />
    		<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    		<?php
    		if ( is_singular() && get_option( 'thread_comments' ) )
    		wp_enqueue_script( 'comment-reply' );
    		wp_head();
    		?>
    
    <! -- masonry -->
    <script src="../js/jquery-1.7.1.min.js"></script>
    <script src="../jquery.masonry.min.js"></script>
    
    <script>
      $(function(){
    
        $('#container').masonry({
          itemSelector: '.box'
        });
    
      });
    </script>
    
    </head>
    
    <body <?php body_class(); ?>>
    
    ...

    This is what I have in a page html file:

    <body class="demos ">
    
    <div id="container" class="clearfix">
    
      <div class="box col1">
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p>
      </div>
    
      <div class="box col1">
        <p>Morbi purus libero, faucibus adipiscing, commodo quis, gravida id, est. Sed lectus. Praesent elementum hendrerit tortor. Sed semper lorem at felis.</p>
        <p>Sed ac risus. Phasellus lacinia, magna a ullamcorper laoreet, lectus arcu pulvinar risus, vitae facilisis libero dolor a purus. Sed vel lacus. Mauris nibh felis, adipiscing varius, adipiscing in, lacinia vel, tellus. Suspendisse ac urna. Etiam pellentesque mauris ut lectus. Nunc tellus ante, mattis eget, gravida vitae, ultricies ac, leo. Integer leo pede, ornare a, lacinia eu, vulputate vel, nisl.</p>
      </div>
    
      <div class="box col1">
        <p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. </p>
      </div>
    
      <div class="box col1">
        <p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula  </p>
      </div>
    
      <div class="box col1">
        <h2>Sit amet mi ullamcorper vehicula</h2>
              <p>Ut convallis, sem sit amet interdum consectetuer, odio augue aliquam leo, nec dapibus tortor nibh sed augue.  Sed vel lacus. Mauris nibh felis, adipiscing varius, adipiscing in, lacinia vel, tellus. Suspendisse ac urna. Etiam pellentesque mauris ut lectus. Nunc tellus ante, mattis eget, gravida vitae, ultricies ac, leo. Integer leo pede, ornare a, lacinia eu, vulputate vel, nisl.</p>
      </div>
    
      <div class="box col1">
        <p>adipiscing in, lacinia vel, tellus. Suspendisse ac urna. Etiam pellentesque mauris ut lectus.</p>
      </div>
    
      <div class="box col1">
        <p>Sit amet mi ullamcorper vehicula</p>
      </div>
    
      <div class="box col1">
        <p>Phasellus pede arcu, dapibus eu, fermentum et, dapibus sed, urna.</p>
      </div>
    
      <div class="box col1">
        <p>Ut condimentum mi vel tellus. Suspendisse laoreet. Fusce ut est sed dolor gravida convallis. Morbi vitae ante. Vivamus ultrices luctus nunc. Suspendisse et dolor. </p>
        <p>Phasellus pede arcu, dapibus eu, fermentum et, dapibus sed, urna.</p>
      </div>
    
      <div class="box col1">
        <p>Sed ac risus. Phasellus lacinia, magna a ullamcorper laoreet, lectus arcu pulvinar risus, vitae facilisis libero dolor a purus. Sed vel lacus. </p>
      </div>
    
      <div class="box col1">
        <p>Morbi purus libero, faucibus adipiscing, commodo quis, gravida id, est. Sed lectus. Praesent elementum hendrerit tortor. Sed semper lorem at felis.</p>
      </div>
    
      <div class="box col1">
        <p>Vestibulum volutpat, lacus a ultrices sagittis,</p>
        <p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula  </p>
      </div>
    
      <div class="box col1">
        <p>Sit amet mi ullamcorper vehicula</p>
      </div>
    
      <div class="box col1">
        <p>Morbi purus libero</p>
      </div>
    
      <div class="box col1">
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. </p>
      </div>
    
      <div class="box col1">
        <p>Vestibulum volutpat, lacus a ultrices sagittis,</p>
      </div>
    
      <div class="box col1">
        <p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula  </p>
      </div>
    
      <div class="box col1">
        <p>Sit amet mi ullamcorper vehicula</p>
      </div>
    
      <div class="box col1">
        <h2>Morbi purus libero</h2>
      </div>
    
      <div class="box col1">
        <p>Ut condimentum mi vel tellus. Suspendisse laoreet. Fusce ut est sed dolor gravida convallis. Morbi vitae ante. Vivamus ultrices luctus nunc. Suspendisse et dolor. Etiam dignissim. Proin malesuada adipiscing lacus. Donec metus. Curabitur gravida.</p>
        <p>Phasellus pede arcu, dapibus eu, fermentum et, dapibus sed, urna.</p>
      </div>
    
      <div class="box col1">
        <p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula</p>
      </div>
    
      <div class="box col1">
        <p>Sed ac risus. Phasellus lacinia, magna a ullamcorper laoreet, </p>
      </div>
    
      <div class="box col1">
        <p>adipiscing in, lacinia vel, tellus. Suspendisse ac urna. Etiam pellentesque mauris ut lectus.</p>
      </div>
    
      <div>
        <div class="box col1">
          <p>Sit amet mi ullamcorper vehicula</p>
          <h2>Sit amet mi ullamcorper vehicula</h2>
        </div>
    
        <div class="box col1">
          <p>Phasellus pede arcu, dapibus eu, fermentum et, dapibus sed, urna.</p>
        </div>
    
        <div class="box col1">
          <h2>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula  </h2>
        </div>
    
        <div class="box col1">
          <p>Ut convallis, sem sit amet interdum consectetuer, odio augue aliquam leo, nec dapibus tortor nibh sed augue.</p>
          <p>Ut condimentum mi vel tellus. Suspendisse laoreet. Fusce ut est sed dolor gravida convallis. Morbi vitae ante. Vivamus ultrices luctus nunc. Suspendisse et dolor. Etiam dignissim. Proin malesuada adipiscing lacus. Donec metus. Curabitur gravida.</p>
        </div>
    
        <div class="box col1">
    
          <ul>
            <li>Lacus a ultrices sagittis</li>
            <li>Democratis</li>
            <li>Plummus</li>
          </ul>
        </div>
    
        <div class="box col1">
          <p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula  </p>
          <p>Morbi interdum mollis sapien. Sed ac risus. Phasellus lacinia, magna a ullamcorper laoreet, lectus arcu pulvinar risus, vitae facilisis libero dolor a purus.</p>
        </div>
    
        <div class="box col1">
          <p>adipiscing in, lacinia vel, tellus. Suspendisse ac urna. Etiam pellentesque mauris ut lectus.</p>
    
        </div>
      </div>
    
      <div class="box col1">
        <p>Phasellus pede arcu, dapibus eu, fermentum et, dapibus sed, urna.</p>
      </div>
    
      <div class="box col1">
        <p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula</p>
      </div>
    
      <div class="box col1">
        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p>
      </div>
    
      <div class="box col1">
        <p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. </p>
      </div>
    
      <div class="box col1">
        <p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula  </p>
      </div>
    
      <div class="box col1">
        <p>Vestibulum volutpat, lacus a ultrices sagittis,</p>
      </div>
      <div class="box col1">
        <p>Fusce accumsan mollis eros. Pellentesque a diam sit amet mi ullamcorper vehicula  </p>
      </div>
      <div class="box col1">
    
        <p>Morbi purus libero, faucibus adipiscing, commodo quis, gravida id, est. Sed lectus. Praesent elementum hendrerit tortor. Sed semper lorem at felis.</p>
      </div>
    
    </div> <!-- #container -->
    
    <script src="../js/jquery-1.7.1.min.js"></script>
    <script src="../jquery.masonry.min.js"></script>
    <script>
      $(function(){
    
        $('#container').masonry({
          itemSelector: '.box'
        });
    
      });
    </script>

    I've tried different renditions like reordering the script in the head.php file but nothing changes. What am I doing wrong? Could something be interfering with the script or did I insert the script in the wrong place? I have the js folder in the theme folder, should I be putting it somewhere else? Any help in this matter would be greatly appreciated.

    Antonella

  2. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 1 year ago #

    Have you tried looking for JavaScript errors in the web browser, through tools such as Firefox's Firebug or Google Chrome's Inspect Element?

  3. antonel1
    Member
    Posted 1 year ago #

    Oh, and this is what I have in my CSS file concerning Masonry:

    /**** Demos ****/
    
    #container {
      background: #FFF;
      padding: 5px;
      margin-bottom: 20px;
      border-radius: 5px;
      clear: both;
      -webkit-border-radius: 5px;
         -moz-border-radius: 5px;
              border-radius: 5px;
    }
    
    .centered { margin: 0 auto; }
    
    .box {
      margin: 5px;
      padding: 5px;
      background: #D8D5D2;
      font-size: 11px;
      line-height: 1.4em;
      float: left;
      -webkit-border-radius: 5px;
         -moz-border-radius: 5px;
              border-radius: 5px;
    }
    
    .box h2 {
      font-size: 14px;
      font-weight: 200;
    }
    
    .box img,
    #tumblelog img {
      display: block;
      width: 100%;
    }
    
    .rtl .box {
      float: right;
      text-align: right;
      direction: rtl;
    }
    
    .col1 { width: 80px; }
    .col2 { width: 180px; }
    .col3 { width: 280px; }
    .col4 { width: 380px; }
    .col5 { width: 480px; }
    
    .col1 img { max-width: 80px; }
    .col2 img { max-width: 180px; }
    .col3 img { max-width: 280px; }
    .col4 img { max-width: 380px; }
    .col5 img { max-width: 480px; }

    Again, it works fine when it's not connected to WordPress, but it doesn't work when I insert the same coding and files into WordPress.

  4. antonel1
    Member
    Posted 1 year ago #

    I have Firefox's Firebug. How can I check for JavaScript errors in the web browser. I tested the page using Safari and Firefox, and both are experiencing the same problems.

  5. Andrew Nevins
    Barrel Rider, Spam Zapper & Volunteer Moderator
    Posted 1 year ago #

    In Firebug, look under the 'console' section.

  6. antonel1
    Member
    Posted 1 year ago #

    Alright, so I have a few errors. One that may concern Masonary is the following. I inserted the </p>, but it still doesn't work.

    Error: SyntaxError: syntax error
    $(function(){</p>

    I removed scripts, coding and css to see if those are causing problems, but nothing changes. I'm not sure what is interfering if that's even the problem.

  7. antonel1
    Member
    Posted 1 year ago #

    Okay. I seem to have solved the problem.

    I changed this script...:

    <script src="../js/jquery-1.7.1.min.js"></script>
    <script src="../jquery.masonry.min.js"></script>

    ...to this:

    <script src="/js/jquery-1.7.1.min.js"></script>
    <script src="/jquery.masonry.min.js"></script>

    Simple as that! All that trouble. Thanks Andrew for all your help!! Much appreciated!

Topic Closed

This topic has been closed to new replies.

About this Topic