1

I am working on a new blog site, and I am far from seasoned when it comes to PHP.

http://www.theredo.ca/

What I am trying to do is the following:

Each post has a custom field, "timer".

Inside the custom field, there is a piece of javascript:

<script type="text/javascript">      
    $j(document).ready(function(){

        //init plugin
        $j('#event-1').fancyCountdown({year:2011, month:9, day:31, hour:0, minute:0, second:0, timezone:0, dayDigitsAmount: 3, digits:{days:true,hours:true,minutes:true,seconds:true}});


    });     
</script>

I need to loop through all my posts and place this javascript in the footer - each post will have a slightly unique javascript (ID and other variables).

Currently I am using this piece of code, which isn't a loop and is only placing the last custom field into the footer.

<?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'timer', true);
    wp_reset_query();
?>

I am already using a loop earlier to pull all the posts and display the titles and a couple other custom fields above, which was causing a conflict when I originally was placing the Javascript inline with the HTML...

Long story short - help?

8
  • what do you mean "Inside the custom field, there is a piece of javascript" ?? what do you want to acheive? Nov 24, 2011 at 20:34
  • If you have a look at the page I linked - there is the timers date. Currently showing only one... I need to be able to set the timer for each post, to a specific date and time. So each post has a "timer" custom field and inside that field I have the Jquery. Make sense?
    – Ian
    Nov 24, 2011 at 20:36
  • Basically what I want, is a loop to echo all custom fields named "timer" in the footer... :D
    – Ian
    Nov 24, 2011 at 20:39
  • so you mean the value of custom field 'timer' is the javascript code? Nov 24, 2011 at 20:43
  • That is correct - sorry for being so vague.
    – Ian
    Nov 24, 2011 at 20:45

3 Answers 3

0

try this, not sure this is what you want.

<?php
query_posts();//get all posts
while ( have_posts() ) : the_post();
    echo get_post_meta($post->ID,'timer', true);
endwhile;
wp_reset_query();
?>
5
  • Throws the following: "Parse error: syntax error, unexpected T_WHILE"
    – Ian
    Nov 24, 2011 at 21:13
  • Getting there.. query_posts('posts_per_page=3');/ had to ad that to the query for it to not toss an error.
    – Ian
    Nov 24, 2011 at 21:28
  • Think that should work.. need to troubleshoot the plugin for the timer now.. the javascript is there - just need to figure out whats going wrong with the calculations.. thanks again.
    – Ian
    Nov 24, 2011 at 21:29
  • glad to help. you can actually thumb up the answer :))) Nov 24, 2011 at 21:36
  • it does not matter.forget it. Nov 24, 2011 at 21:41
0

I can't imagine why you would need JS which is inside a doc.ready call to be in the footer (it's going to wait for the DOM to be ready anyway), but you can use the wordpress add_action hook to get your content down there.

Looking at the docs, my guess would be something like this:

<?php add_action('wp_footer', function() { echo get_post_meta($postid, 'timer', true);}) ?>

You would do this inside of "the loop" so that it is run once for each post, and all the JS piles up in the footer.

2
  • The doc.ready call is kind of pointless - yes. It was my attempt to wait for the PHP to finish resorting / displaying all the posts on the homepage. Not sure how / where I would use this code you provided. I will have to have a look at it later this evening when I have more time. thanks again.
    – Ian
    Nov 24, 2011 at 21:08
  • No, the doc.ready is probably very important to your scripts. What is pointless is putting them in the footer, but I see from the answer that you've accepted that you agree ;)
    – Sinetheta
    Nov 24, 2011 at 22:27
0

each post will have a code snippet:

$j('#event-1').fancyCountdown({year:2011, month:9, day:31, hour:0, minute:0, second:0, timezone:0, dayDigitsAmount: 3, digits:{days:true,hours:true,minutes:true,seconds:true}});

while in your wordpress loop concat these values:

$countdown_script .= get_post_meta($post, 'timer', true);

then after the loop echo the script:

<script type="text/javascript">      
$j(document).ready(function(){
    <?php echo $countdown_script; ?>
});     
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.