ThemeShaper Forums » Thematic

[closed]

How Can I Place JQuery Masonry On My Home Page?

(25 posts)
  • Started 4 years ago by Launch83
  • Latest reply from helgatheviking
  • This topic is not resolved
  1. Launch83
    Member

    Hey Guys,

    First I'd like to thank everything for their help up to this point with my questions.

    My current question is regarding the JQuery Masonry Script.

    I am using the UpThemes Gallery Theme and I'd like to use the Masonry Script on the home page to give my post thumbnails that Vertical Masonry Effect. Similar to the UpTheme Evo theme.

    I'd like to be completely honest that I've been really confused with this, I've never worked with any JQuery code or this theme structure so I'm really overwhelmed.

    I'm sure this is probably a simple procedure but I could really do with some guidance.

    Thanks in advance.

    Chris.

    Posted 4 years ago #
  2. google search brings up:
    http://wordpress.org/support/topic/help-with-jquerys-masonry

    there are some posts in the forum but the basic way to get jquery stuff happening on your site is to 1. load jquery 2. load the jquery plugin you are trying to use 3. call the plugin's function

    to register the scripts you do something like:

    function add_stuff{
       wp_enqueue_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js</a>"), false, '1.3.2');
      wp_enqueue_script('masonry',get_bloginfo('stylesheet_directory') . '/scripts/masonry.js');
    }
    add_action('init','add_stuff');

    and then to call the function you'd do:

    function my_script() {?>
      <script> whatever needs to be here </script>
    <?php }
    add_action('wp_head', 'my_script');
    Posted 4 years ago #
  3. edit to add that the latest jquery is

    http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js

    and not 1.3.2

    Posted 4 years ago #
  4. Launch83
    Member

    This might sound stupid but when you say load the plugin does that simply mean upload it to my "js" folder? Also possibly another silly question, where would I place the code to register the script and then the code for the call t function.

    I really do apologize if I'm being a pain!

    Chris.

    Posted 4 years ago #
  5. I'm not trying to be an ass, so I'm trying to find a way to say this in a way that actually comes across as helpful.

    adding custom jquery to wordpress + thematic involves a few moving parts.

    Helga opened the door, but its sort of a long corridor.

    I'd google and/or forum search "wp_enqueue_script" to help get your walking the path.

    The good news, once you start to "get it", adding jquery stuff isn't terribly painful.

    The bad news is that there are thousands of cool jquery effects and jquery plugins, and most of them don't play nicely together in wordpress, at least not without some wrestling with the jquery itself, as most jquery developers didn't code their plugin to specifically address wordpress's way of implimenting jquery. Again, "wp_enqueue_script" will get you headed in the right direction.

    To be a little more helpful, here's a chunk out of a functions.php file in a thematic childtheme:

    //add sidebar jquery slide effect
    
    	function pretty_sidebar () {
    		if (!is_front_page()) { ?>
    			<script type="text/javascript">
    				jQuery(document).ready(function(){
    				jQuery('.aside ul ul li ').mouseover(function(){
    				jQuery(this).animate({paddingLeft:"20px"}, {                                    queue:false, duration:200});
    				});
    					jQuery('.aside ul ul li ').mouseout(function(){
    					jQuery(this).animate({paddingLeft:"0"}, {queue:true, duration:200})
    					});
    				});
    			</script>
    				<?php }
    						}
    add_action ('wp_head','pretty_sidebar');

    This is sticking a tiny little jquery script in the document header that makes sidebar list elements do a little dance when you mouse over them. Its about as simple as it gets.

    This one adds Cufon fonts to H1 tags:

    //cufon fontification
    function fontification() { ?>
    	<script type="text/javascript" src="<?php echo bloginfo('stylesheet_directory') ?>/js/cufon-yui.js"></script>
    	<script type="text/javascript" src="<?php echo bloginfo('stylesheet_directory') ?>/js/vera-font.js"></script>
    	<script type="text/javascript">
    			Cufon.replace('h1' , { hover: true
    				});
    			</script>
          <?php }
    add_action ('wp_head','fontification');

    See whats happening here? First we're loading our java libraries into the html document header (the scripts are in /theme-folder/js), then loading the script that tells them what to do. Hope this gives you a head start.

    Posted 4 years ago #
  6. sorry i didn't actually answer your question...its just that more often than not, its going to appear that you did everything right, but the script just won't work...you may get lucky with masonry, maybe you won't. Just trying to help you build a foundation for when things don't go as planned, which tends to be the rule, or is it murphy's law...

    Everything Helga posted belongs in your child-theme's functions.php file.

    You can place your actual jquery libraries in a subdirectory, like /child-theme/js, but you use the functions inside functions.php to add the scripts to your wordpress pages.

    Her first script adds the libraries, you'll see the use of 'init' and "wp_enqueue_script". This is the correct way to add jquery libraries to wordpress in a way that minimizes the chances of compatibility issues...normally...

    Her second example is to demonstrate the function you would use to actually add the script used to make masonry work. My first example code skips the use of her first example because i'm using the jquery included in wordpress, so i don't need to add any libraries, but, in the Cufon example, i probably should have used wp_enqueue_script instead of adding the libraries directly with the <script></script> lines...

    confused yet?

    Posted 4 years ago #
  7. to quickly answer your questions before going deeper

    to load the plugin you must 1. upload it and 2. tell WP to load it using wp_enqueue_script

    the add_stuff and my_script functions need to go in your functions.php like everything else for your child theme.

    and now for more.... i agree w/ tarpontech. it is a long corridor. many swear words have been uttered by me in regards to jquery. and it is 'supposed' to be an easy language. my biggest hurdle was right where you are now: getting it implemented.

    take tarpontech's advice and lookup enqueue script. but in general this is how it works....

    wp_enqueue_script('script nickname','script location')

    the nickname is a generic handle name for your script (this makes it so things don't get loaded twice and makes it easier to deregister stuff you dont want).

    the location tells WP where to find your script. if you are packaging it w/ your theme then you need to point this to your theme folder... which i do below- by assuming it is in a scripts subfolder of your child theme

    armed w/ this new knowledge take a look again at:

    function add_stuff{
       wp_enqueue_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
      wp_enqueue_script('masonry',get_bloginfo('stylesheet_directory') . '/scripts/masonry.js');
    }
    add_action('init','add_stuff');

    EDIT TO ADD: if you only want to use this script on your home page then you ought to add the condition is_home() to your enqueue. this will cut load time on other pages where the script isnt needed.

    i am just guessing the name of your masonry script file. also guessing at a scripts folder. you can call it anything you want. get_bloginfo('stylesheet_directory') is the location of your child theme's style.css file. so the location is relative from there.

    another major headache is how jquery works in what i think is called no conflict mode? i don't understand it at all, BUT if you make your document ready function look as follows you can pretty much then be safe to use the code the way the plugin author's write it... typ using the $ shortcut. if you don't use this document ready setup your jquery will never work and it just doesn't play nice w/ WP and lots of swearing will ensue.

    function my_script() {?>
    <script> 
    
    jQuery(document).ready(function($) {
    
    //now you can use jquery as normally written w/ $('#selector).whatever();
    
    });
    
    </script>
    <?php }
    add_action('wp_head', 'my_script');

    hope that helps. there is a learning curve for jquery too. don't try to take on too much. also one of these days we ought to make a definitive sticky post on how to implement jquery plugins.

    Posted 4 years ago #
  8. btw...helga...i'm very ignorant about forum etiquette, so if i hijacked the thread or anything..totally not intended...you're infinitely more qualified than me for this stuff..

    I just have a jquery pet-peeve...they don't make it easy AT ALL in wordpress, especially when you add thematic. You're right, there seriously needs to be some kind of indepth sticky.

    And sorry i'm not a good teach chris...and overly verbose...

    Posted 4 years ago #
  9. you're not hijacking at all. hopefully between both our posts Chris will either magically understand jquery faster than we did OR run screaming. lets hope for the former.

    and i would hardly consider myself more qualified, but i'll take the compliment anyway. thanks! but i couldnt agree more about how difficult it is to get jquery going in WP. i probably lost a few days off my life from the stress of figuring it out! maybe i will work on a sticky. i really think my grip on jquery is tenuous at best, but i think i have it sussed out how to get plugins loaded properly. whether they work from there and whether multiple plugins play nice together... meh- out of my league.

    Posted 4 years ago #
  10. Launch83
    Member

    Hey Guys,

    I've just got to go ahead and first thank you for all of your help up until this point, it been really refreshing to see people come forward like you have.

    Now the problem is that honestly I'm totally overwhelmed by all of this information, with me not knowing much about this to start off then having to try and understand your examples and then apply it to my actual situation is pretty overwhelming!

    I don't want to ask to much of you but would it be ok to post the code for my theme's function.php and would it be possible for someone to show me what I need to place and where I need to place it so that the masonry script will apply to just my homepage post thumbnails?

    I feel that I'd have a much better understanding of this if I can actually see what I'm trying to do and how it works, if it's to much to ask though I completely understand.

    If that is ok, could some also tell me the correct way to post code in this forum so I don't mess anything up?

    Thanks,

    Chris.

    Posted 4 years ago #
  11. chris, you can always post your code. that doesn't guarantee that you'll get an answer. this is a volunteer forum and most everyone operates out of a pay-it-forward sort of approach. i got (and still get) a lot of help here, so i try to pay that back a bit. plus helping others teaches me more stuff.... though it can be annoying to post the same information sometimes. AND more importantly, tarpontech and myself have already admitted to not being soundly versed in jquery. so no guarantees. for as easy/intuitive as jquery is supposed to be.... implementing it in WP is freaking hard.

    if you are a rookie to childtheming in general, i'd say be careful of getting in over your head. (i can say that b/c i would never do something like that... <cough>tries to implement youtube api</cough>. everyone likes to think they can design/code websites, but it isn't as easy as it would appear. sometimes it really is a better move to hire help.

    you post code between two ` tick marks.

    Posted 4 years ago #
  12. Launch83
    Member

    For some reason it won't let me post the function page code, so I saved it in a text document and uploaded it to my site so here is a link:

    www.launch83.com/launchpress/wp-content/uploads/Function%20Code.rtf

    The masonry script is located at:

    www.launch83.com/launchpress/wp-content/themes/gallery/js/jquery.masonry.js

    Like I said I'd like to have the masonry script function on the homepage post thumbnails.

    Thanks,

    Chris.

    Posted 4 years ago #
  13. try the tickmarks. they're right up by the number 1 on the keyboard. well on mine anyway.
    i think you can also use

    <code>PASTE CODE HERE</code>

    your rtf file is a formatting disaster. with bonus / everywhere. if you still can't post code use pastebin.com

    where did you get the masonry script from? where is the documentation?

    also, i went back to your site and you aren't enqueuing the masonry script as far as i can see. nor are you calling any jquery functions in the head

    <script type="text/javascript">
    		jQuery.noConflict();
    	</script>

    is all you have in that regard. i have already given you what you need to post into your functions file. what i provided earlier will put the script on every page. to only have it on the front page you'd do this:

    function add_stuff{
       if (is_home()){
      wp_enqueue_script('masonry',get_bloginfo('stylesheet_directory') . '/scripts/masonry.js');
       }
    }
    add_action('init','add_stuff');

    and use the my_script() function from above to insert whatever code the masonry docs say. idk what it is, b/c i've never heard of masonry. add both of these to your functions.php file (at the bottom or wherever really... it doesn't matter as long as you aren't plopping this stuff into the middle of existing functions). and if you point us to the masonry documentation we might be able to see what isn't working.

    right now i've given you about as much as i know on the subject, which theoretically ought to be enough to get started, if you will try it.

    Posted 4 years ago #
  14. Launch83
    Member

    For some reason any time I try and paste the code it doesn't end up posting anything, so here is a pastebin link:

    http://pastebin.com/VHEtTHWR

    Chris.

    Posted 4 years ago #
  15. Launch83
    Member

    Also I got the Masonry code directly from: http://desandro.com/resources/jquery-masonry/

    Posted 4 years ago #
  16. 1. Put your jquery.masonry.js file inside of a subdirectory of your theme. Name the directory js

    2. Add this to the end of your functions.php, BEFORE the last ?>

    function add_stuff{
       if (!is_admin()){
      wp_enqueue_script('masonry',get_bloginfo('stylesheet_directory') . '/js/jquery.masonry.js');
       }
    }
    add_action('init','add_stuff');

    3. Then add this below the first stuff:

    function my_script() {?>
      <script> jQuery('#primary').masonry({
        columnWidth: 100,
        itemSelector: '.box'
    }); </script>
    <?php }
    add_action('wp_head', 'my_script');
    Posted 4 years ago #
  17. don't think .box will work for anything b/c gallery doesn't have any .box classes in the markup. might need .post there? will be interested to hear how it works. now that i see what masonry is (which is super cool) i can't see what good it does w/ gallery where everything is already so tightly gridded up.

    Posted 4 years ago #
  18. i cut-n-pasted that directly from the masonry site... so you're absolutely right here...

    Posted 4 years ago #
  19. Launch83
    Member

    Thanks for the input I'm going to give that a try when I get back to a computer. With regards to what you said Helga my plan once I get masonry working is to have my thumbs all different sizes based on the project.

    Posted 4 years ago #
  20. Launch83
    Member

    Hey,

    Ok I completed section 1. now when I comes to section 2. whenever I placed the code at the end of my Function.php my site doesn't load, here is what the code looks like:

    ?>
    
    </table>
    
    <p class="submit">
    <input name="save" type="submit" value="Save changes" />
    <input type="hidden" name="action" value="save" />
    
    </form>
    <form method="post">
    <p class="submit">
    <input name="reset" type="submit" value="Reset" />
    <input type="hidden" name="action" value="reset" />
    
    </form>
    
    <?php _e('For more information about this theme, <a href="http://themeshaper.com">visit ThemeShaper</a>. If you have any questions, visit the <a href="http://forums.themeshaper.com/">ThemeShaper Forums</a>.', 'thematic'); ?>
    
    <?php
    }
    
    add_action('admin_menu' , 'childtheme_add_admin');
    
    function add_stuff{
       if (!is_admin()){
      wp_enqueue_script('masonry',get_bloginfo('stylesheet_directory') . '/js/jquery.masonry.js');
       }
    
    add_action('init','add_stuff');
    
    ?>

    I'm also a little confused with what you meant by "3. Then add this below the first stuff:" does this mean below the first set of code you gave me that was placed at the bottom of the function.php or below the first set of code at the top of the function.php.

    I really apologize if I'm asking stupid questions!

    Chris.

    Posted 4 years ago #
  21. Launch83
    Member

    Does anyone have any input left on this? Or am I beyond help? Lol

    Posted 4 years ago #
  22. check your squiggly brackets. you aren't properly closing your function...

    function add_stuff{
       if (!is_admin()){
       wp_enqueue_script('masonry',get_bloginfo('stylesheet_directory') . '/js/jquery.masonry.js');
       }
    } //you missed this one
    add_action('init','add_stuff');
    
    function my_script() {?>
      <script> jQuery('#primary').masonry({
        columnWidth: 100,
        itemSelector: '.box'
    }); </script>
    <?php }
    add_action('wp_head', 'my_script');

    i don't know what was so unclear about #3. you simply paste that function under what you've pasted in #2. it will be one block of code like i have above. paste the whole thing to the end of your functions.php. just remember my comment from above... the itemSelector .box isn't likely to DO anything since there is no .box in the markup.

    in the future you can add:
    define('WP_DEBUG', true);

    to wp-config.php in the root of wp to get useful error messages instead of just the white screen of death.

    AND you should def use Notepad++ as it has syntax highlighting which really helps you find those errors.

    Posted 4 years ago #
  23. this may be of help, but the posts above do pretty much cover all bases .
    i'm using masonry for a child theme i'm developing

    http://www.arrival.virtualpudding.com

    still much work to do (the jquery doesn't play well with ie).
    it only uses 1 plugin, thats wp-pagenavi. the rest is jquery.

    //ENQUEUE SCRIPTS
    function my_init() {
    	if (is_home()) {
    		wp_deregister_script('jquery');
    		wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, '1.4.2', true);
    		wp_enqueue_script('jquery');
    		//masonry
    		wp_register_script('masonry',
            get_bloginfo('stylesheet_directory') . '/js/jquery.masonry.min.js', array('jquery'), '1.0', true );
       		wp_enqueue_script('masonry');
    		//my jquery
    		wp_register_script('myjquery',
            get_bloginfo('stylesheet_directory') . '/js/myjquery.js', array('jquery'), '1.0', true );
       		wp_enqueue_script('myjquery');
    		}
    		}
    add_action('init', 'my_init');
    
    THE ABOVE IS LOADING FROM FUNCTIONS
    It calls
    *the masonry script
    *my custom jquery script (as i have various jquery throughout)
    
    MYJQUERY.js includes...
    
    // Masonry
    $(document).ready(function() {
    $('body.home #content').masonry({
    singleMode: true,
    itemSelector: '.hentry'
    });

    thats it. it applies the masonry script to the 'hentry' div. that wraps posts.
    i hope this helps in any way.
    in my child theme the masonry script is applied to home, categories, tags, archives, etc.

    thanks

    Posted 4 years ago #
  24. hey johnny your theme is really sweet.

    Posted 4 years ago #
  25. old thread, but it was pointed out to me that my function needed some parens.... so if you were using my code and it was breaking that is why. use this:

    function add_stuff() {
       wp_enqueue_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js</a>"), false, '1.3.2');
      wp_enqueue_script('masonry',get_bloginfo('stylesheet_directory') . '/scripts/masonry.js');
    }
    add_action('init','add_stuff');

    or Johnny's code above is also correct

    Posted 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.