WordPress:如何在灯箱或新窗口中打开the_content()?

时间:2014-05-14 12:31:10

标签: javascript php jquery wordpress shadowbox

我试图在灯箱中打开the_content(只是图片)。 我喜欢像圣诞节日历那样的页面,这样你就可以看到帖子(以及计划中的帖子),如果点击发布的帖子,图像(the_content)应该显示在影子框,灯箱或其他任何内容中,我认为你会明白我的意思。现在我的代码看起来像:

<header class="entry-header" rel="shadowbox" onclick="location.href='<?php the_permalink();?>';"  style="cursor:pointer;">
        <?php if ( ! post_password_required() && ! is_attachment() ) :
            the_post_thumbnail();
        endif; ?>

        <?php if ( is_single() ) : ?>
        <h1 class="entry-title"><?php the_title(); ?></h1>
        <?php else : ?>
                    <?php endif; // is_single() ?>
        <?php if ( comments_open() ) : ?>
            <div class="kalender">
                <p1> <?php the_field('nummer'); ?> </p1>
                <br>
                <p2><?php the_time('j. F Y'); ?> </p2> 
            <p3><?php the_title(); ?></p3>

             </div>
            <!-- .comments-link -->
        <?php endif; // comments_open() ?>
    </header>

我认为最简单的方法是像read_more链接一样处理the_content,然后在shadowbox中说onclick show。但是现在我只是不知道该怎么做。你可以看看here(现在只在Firefox或Safari中,不知道为什么Chrome会讨厌我: - )

2 个答案:

答案 0 :(得分:0)

考虑使用Fancybox插件:http://fancybox.net/howto

然后在每个帖子中加入一个锚标记,例如“阅读更多”,并附上内容的唯一ID:

<a class="fancybox" href="#content">Read More - This shows content of element who has id="content"</a>

确保每个帖子的#content DIV都有一个唯一的ID,在你的循环中增加一个变量并将其附加到以下行中读取:

#content-01, content-02, etc.

然后用CSS隐藏内容DIV。

.fancybox { display: none; }

P.S。在Chrome 34.0.1847.131

中显得很好

答案 1 :(得分:0)

这是将服务器端数据传递到客户端的有用模式:
 1.使用wp_localize_script将脚本片段输出到DOM  2.在该脚本片段中,将the_content的值分配给a     JavaScript全局字符串变量。
 3.使用此JavaScript全局     在JavaScript中读取the_content

的值