WordPress.org

Ready to get started?Download WordPress

Forums

query_posts('showposts=3'); conflicting with if statement (13 posts)

  1. egr102
    Member
    Posted 2 years ago #

    Basically in my footer I have successfully retrieved the last 3 news articles using this code:

    <?php query_posts('showposts=3'); ?>
        	<?php while (have_posts()) : the_post(); ?>
    
        		<a href="<?php the_permalink()?>" class="article">
    	    		<article>
    	    			<?php the_post_thumbnail(); ?>
    	    			<time><?php the_time('M d Y') ?></time>
    	    			<?php the_excerpt(); ?>
    	    		</article>
        		</a>
    
        		<?php endwhile;?>

    Also, in the footer.php file I am trying to load Javascript files on specific pages like this:

    <?php if ( is_page( 'contact' ) ) {  ?>
    	<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    	<script src="<?php bloginfo('template_url'); ?>/js/gmap.js"></script>
    <?php } ?>

    This also works but only when I remove the query_posts loop above. Whats the best way to show the last 3 posts (in the format outlined above) and successfully use IF statements to load in some JS/jQuery (to specific pages) both at the same time?

  2. esmi
    Forum Moderator
    Posted 2 years ago #

    You are implementing your scripts incorrectly. See http://codex.wordpress.org/Function_Reference/wp_enqueue_script

  3. egr102
    Member
    Posted 2 years ago #

    After reading that link I can only say that I am even more confused! I can fathom that my JavaScript files need to be called in differently but didn't quite understand how to ensure each script gets loaded on the correct page.

    I'm not a brilliant WP developer so if anyone could help or advise as to how I could go about that, i'd really appreciate it.

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    Try something like:

    // Enqueue script
    function my_enqueue_scripts() {
    	wp_enqueue_script(
    		'google_script',
    		'http://maps.googleapis.com/maps/api/js?sensor=false',
    		 array('jquery')
    	);
    	wp_enqueue_script(
    		'my_script',
    		get_template_directory_uri() . /js/gmap.js',
    		 array('google_script')
    	);
    }
    if ( is_page( 'contact' ) ) add_action('init', 'my_enqueue_scripts');
  5. egr102
    Member
    Posted 2 years ago #

    I put that code into my functions.php file and it broke the entire site :(

    I have lots of JS files that need to be called in so I will require multiple functions? Really haven't a clue i'm afraid.

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    My bad.There was a missing single quote:

    // Enqueue script
    function my_enqueue_scripts() {
    	wp_enqueue_script(
    		'google_script',
    		'http://maps.googleapis.com/maps/api/js?sensor=false',
    		 array('jquery')
    	);
    	wp_enqueue_script(
    		'my_script',
    		get_template_directory_uri() . '/js/gmap.js',
    		 array('google_script')
    	);
    }
    if ( is_page( 'contact' ) ) add_action('init', 'my_enqueue_scripts');
  7. egr102
    Member
    Posted 2 years ago #

    That doesn't work either. I viewed source and the script isn't being loaded. The page is called 'contact', i've even tried changing it to its ID but that doesn't work either.

    Does my them need to support this in anyway? I am building one from scratch.

    What is that script doing exactly?

  8. esmi
    Forum Moderator
    Posted 2 years ago #

    I thought you knew! Looks like some sort of Google Maps script. Does that ring any bells for you?

  9. egr102
    Member
    Posted 2 years ago #

    Ha :) I wish I did. I get the basic functionality of what its trying to do and yes it is a google maps script but I don't get how the scripts don't get loaded into the page. Am I right in that I remove all scripts from my footer and reference them in my functions.php using the method you've (kindly) given me above?

    Could anything else, any other php (for example) affect it from working successfully?

  10. esmi
    Forum Moderator
    Posted 2 years ago #

    Am I right in that I remove all scripts from my footer and reference them in my functions.php using the method you've (kindly) given me above?

    Yes. WordPress will add them to each page's <head?</head> section automatically.

  11. egr102
    Member
    Posted 2 years ago #

    Well thats bad in itself. Javascript should always be loaded into the footer.

    Might have to try another route with this. Its just not working. If this is the only other way to tidy up my JS, is there another way for me to recall the last three posts without using the loop i've used?

  12. esmi
    Forum Moderator
    Posted 2 years ago #

    Javascript should always be loaded into the footer

    Not always. It depends upon what you are implementing. And if you read http://codex.wordpress.org/Function_Reference/wp_enqueue_script you'll see that you can add an $in_footer parameter.

  13. egr102
    Member
    Posted 2 years ago #

    Has anyone else got any ideas or could advise me on a solution?

Topic Closed

This topic has been closed to new replies.

About this Topic