在JavaScript函数中显示PHP代码

时间:2012-02-17 14:03:50

标签: php javascript jquery wordpress

我在我的WordPress模板中使用Isotope(JQuery),我想使用添加项目选项(前置)。你可以在这里看到它:http://isotope.metafizzy.co/demos/adding-items.html

我使用的脚本有效:

   $('#prepend a').click(function(){
       var $newItems = $(<div>Hello World</div>);
       $('#container').prepend( $newItems)
         .isotope( 'reloadItems' ).isotope({ sortBy: 'original-order' });
               });

当我点击此链接时:

<li id="prepend"><a href="#">More</a></li>

该脚本在我的页面上添加了一个Hello。

问题是我不想添加div但我想添加一个帖子。

以下是我想使用的邮政编码:

<?php  query_posts('category_name=offers'); while (have_posts()) : the_post(); ?>
<a href="<?php echo get_permalink(); ?>" class="element <?php $posttags = get_the_tags();
if ($posttags) { foreach($posttags as $tag) { echo $tag->slug . " ";   }  }  ?>">

<div>                           
<?php the_title("<h3>", "</h3>"); ?>

</div>
</a>
<?php endwhile;?>

你知道一种让它有效的方法吗?

(原谅我可怕的英语......)

2 个答案:

答案 0 :(得分:1)

你需要Ajax。这样做的方法是,你从服务器上获取你想要的东西,然后prepend那个。

var postsData = { param1: "value", param2: "value" };
$.get('www.example.com/get_posts.php', postsData,
  function(content) {
    var $newItems = $('<div/>').html(content);
    $('#container').prepend($newItems);
  }
);

并创建一个打印HTML片段的get_posts.php。您可以从postsData的{​​{1}}结构中获取内容,或者如果您不需要,可以完全保留$_GET[]参数。

答案 1 :(得分:0)

如果要进行php渲染,则必须使用Ajax请求。你习惯这个可怕的名字'AJAX'吗?

使用jquery:http://api.jquery.com/jQuery.get/进行ajax调用,如果帖子呈现,你可以获得html并将其写在你的页面上。

你明白吗?

的Seb