自定义循环丢失其数据

时间:2015-08-30 05:34:34

标签: php jquery wordpress popup

所以我有一个自定义循环。

假设一页上有5个帖子如下:

发布1:

 Title : Post 1

 Content : Content 1

 Show more button

帖子2:

 Title : Post 2

 Content : Content 2

 Show more button

依旧......

代码:

<div class="title">
    <?php the_title(); ?>
</div>
<div class="content">
    <?php the_content(); ?>
</div>
<button type="button" class="show_me_open">
  Show more button
</button>
<div id="show_me">
   <?php the_title(); ?>
   <?php the_content(); ?>
</div>

因此,5个帖子的标题和内容都不同。弹出按钮(显示更多按钮)来自http://dev.vast.com/jquery-popup-overlay/

问题

假设您没有启用弹出式布局,并使用其他方式来显示它。然后标题和内容与原始标题和内容相匹配(如下所示)。

但是,如果标题和内容由弹出窗口显示(显示更多按钮),则它仅显示第一个帖子的标题和内容(下面的示例)。

1。没有弹出窗口:

发布1:

 Title : post 1
 Content: content 1
 Show more button:  
        Title : post 1
        Content: content 1

帖子2:

 Title : post 2
 Content: content 2
 Show more button:  
        Title : post 2
        Content: content 2

第3篇:

 Title : post 3
 Content: content 3
 Show more button:  
        Title : post 3
        Content: content 3

启用弹出窗口:

发布1:

 Title : post 1
 Content: content 1
 Show more button:  
        Title : post 1
        Content: content 1

帖子2:

 Title : post 2
 Content: content 2
 Show more button:  
        Title : post 1
        Content: content 1

第3篇:

 Title : post 3
 Content: content 3
 Show more button:  
        Title : post 1
        Content: content 1

我不确定为什么会这样做,但任何建议都会非常感激!

谢谢!

修改

Js代码非常简单:

  $(document).ready(function() {          
    $('#show_me').popup();                  
    }); 

编辑2

有人向我指出,所有其他帖子都使用了一个ID,只是拉了第一个。

所以,我正在向按钮添加post_id,它只会触发具有相同post_id的div。

 <?php echo '<button type="button" class="show_me_open" data-post_id="' . esc_attr( $post->id ) . '">' ;?>
    <i class="material-icons royal_setting">settings</i>
<?php echo '</button>';?>

<?php echo '<div class="show_me" data-post_id="' . esc_attr( $post->id ) . '">' ;?>                     
    <?php the_title(); ?>                       
<?php echo '</div>';?>  

现在对于jQuery,我该如何更改它,以便只有post_idshow_me被定位?

谢谢!

1 个答案:

答案 0 :(得分:1)

@Emily,我认为问题只与你的弹出式ID“show_me”有关。

如上所述, show_me 帖子1 div 元素。同样,您必须为每个帖子提供不同的ID,例如发布2 show_me_2 等等,然后在您的document.ready中初始化所有弹出元素。

$('#show_me_1').popup();  
$('#show_me_2').popup();  
$('#show_me_3').popup();  
$('#show_me_4').popup();  
$('#show_me_5').popup(); 

EDIT-4解决方案

<script>
$(document).ready(function() { 
    $('.show_me').popup(); //it was #show_me                    
    }); 
</script>
<?php echo '<button type="button" class="show_me_'. esc_attr( $post->id ) .'_open">' ;?>
    Show me open
<?php echo '</button>';?>

<?php echo '<div id="show_me_'. esc_attr( $post->id ) .'" class="show_me">' ;?>                     
    <?php the_title(); ?>                       
<?php echo '</div>';?> 

我将id添加到div元素并添加“$ post-&gt; id”。可能这可能可以解决您的问题。