简体   繁体   English

在 php 循环中包含 javascript function

[英]Include javascript function in a php loop

Using wordpress I am grabbing the first image attachment from the posts.使用 wordpress 我正在从帖子中获取第一个图像附件。 This works fine:这工作正常:

    <?php
        global $post;
  $args = array( 
    'post_parent' => $post->ID, 
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'orderby' => 'menu_order', 
    'order' => 'ASC', 
    'numberposts' => 2 );
   $images = get_posts($args);
   if ( $images ) {
    $i = 0;
    while($i <= 1){
        $image = wp_get_attachment_url( $images[$i]->ID );
    echo "<img src='$image' />";            
      $i++;     
    }
  }
 ?>

I am also trying to process these images which in conjunction with timthumb resizes the images depending on browser size.我也在尝试处理这些图像,这些图像与 timthumb 一起根据浏览器大小调整图像大小。 I can only get this to work on one of the two images.我只能让它在两个图像之一上工作。 I would expect it to log and resize twice but the script is not running in the loop.我希望它记录和调整大小两次,但脚本没有在循环中运行。 Can someone please help?有人可以帮忙吗? This is what the full snip I am working with looks like:这就是我正在使用的完整片段的样子:

    <?php
        global $post;
  $args = array( 
    'post_parent' => $post->ID, 
    'post_type' => 'attachment', 
    'post_mime_type' => 'image', 
    'orderby' => 'menu_order', 
    'order' => 'ASC', 
    'numberposts' => 2 );
   $images = get_posts($args);
   if ( $images ) {
    $i = 0;
    $z = 1;
    while($i <= 1){
        $image = wp_get_attachment_url( $images[$i]->ID );
    echo "<img src='$image' class='image_$z' />";   
        ?>
    <script type="text/javascript">
    var image = "<?php echo $image ; ?>";
    var under700_<? echo $z ?> = "/wp-content/themes/Loupe/scripts/timthumb.php?src=<? echo $image ?>&w=340";
     var under900_<? echo $z ?> = "/wp-content/themes/Loupe/scripts/timthumb.php?src=<? echo $image ?>&w=440";

     function imageresize() { 
       var contentwidth = $('#two_up').width();  
         if ((contentwidth) < '700'){   
             console.log('under 700_<? echo $z ?>' + under700_<? echo $z ?>);   
             $('img .image_<? echo $z ?>').attr('src', under700_<? echo $z ?>);
             } else if ((contentwidth) < '900') {
            // console.log('under 900');             
             $('img .image_<? echo $z ?>').attr('src', under900_<? echo $z ?>);
             }
              else {
              image;
             }
     }
        </script>
    <?php
      $i++;
      $z++;
    }
  }
 ?>

Use json_encode() it makes a JavaScript object (or array or combined) of a PHP variable and makes it使用json_encode()它生成 JavaScript object (或数组或组合) PHP 变量并使其

I'm assuming $images is an array;我假设$images是一个数组; if not, tweak this solution to fit.如果没有,请调整此解决方案以适应。

Javascript: Javascript:

var images = <? echo json_encode($images); ?>;
for( x=0; x<images.length-1; x++)
{
    someFunction(images[x]);
}

I would recommend setting specific height/width in your css and then using jquery to replace the images as the page loads.我建议在您的 css 中设置特定的高度/宽度,然后在页面加载时使用 jquery 替换图像。

As an alternative, try doing the entire processing in php.作为替代方案,尝试在 php 中进行整个处理。 You'll save your users loading time, and imho, it's much easier.您将节省用户加载时间,而且恕我直言,这要容易得多。 http://www.imagemagick.org/script/index.php http://www.imagemagick.org/script/index.php

What I guess you are trying to do is something like this:我猜你正在尝试做的是这样的事情:

<?php
global $post;
$args = array( 
  'post_parent' => $post->ID, 
  'post_type' => 'attachment', 
  'post_mime_type' => 'image', 
  'orderby' => 'menu_order', 
  'order' => 'ASC', 
  'numberposts' => 2 
);
$images = get_posts($args);
if ( $images ) {
    $i = 0;
    ?>
    <script type="text/javascript">

        function imageresize(imgElement, srcString) {
            var under700 = "/wp-content/themes/Loupe/scripts/timthumb.php?src=" + srcString + "&w=340";
            var under900 = "/wp-content/themes/Loupe/scripts/timthumb.php?src=" + srcString + "&w=440";
            var contentwidth = $('#two_up').width();


            if ((contentwidth) < '700'){   
                console.log('under 700' + under700);   
                $(imgElement).attr('src', under700);

                imgElement.onload = ''; //stop the onload event from happening again
            } else if ((contentwidth) < '900') {
                // console.log('under 900');             
                $(imgElement).attr('src', under900);

                imgElement.onload = ''; //stop the onload event from happening again
            }
            else {
                image; //don't know what to make of this (maybe you do)
            }
        }
    </script>
    <?php
    while($i < count($images)){
        $image = wp_get_attachment_url( $images[$i]->ID );
        echo "<img src='$image' class='image' onload='imageresize(this, \"$image\")' />";   
        $i++;
    }
}
?>

But, as suggested before by @Toast: I'd do this in PHP and prevent the user from reloading all the images on the page.但是,正如@Toast 之前建议的那样:我会在 PHP 中执行此操作,并防止用户重新加载页面上的所有图像。 Because that is effectively done by the above code.因为这是由上面的代码有效地完成的。

If you have the path of the source image files that you are trying to resize, then you could use getimagesize to get the image size, and adjust the src uri off the images accordingly.如果您有要调整大小的源图像文件的路径,则可以使用getimagesize获取图像大小,并相应地调整图像的 src uri。

The only guess I have, at you using javascript to do this, is that you are using the DHTML/CSS box model to help you calculate the width of the container ( #two_up ) for the images.在您使用 javascript 来执行此操作时,我唯一的猜测是您正在使用 DHTML/CSS 框 model 来帮助您计算图像的容器宽度( #two_up )。 This I would strongly discourage, but still the above code should work;我强烈反对这样做,但上面的代码仍然可以工作; ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM