【问题标题】:How to get content from a single *page* of a post?如何从帖子的单个 *page* 获取内容?
【发布时间】:2012-01-31 10:24:56
【问题描述】:

我正在尝试做一个画廊排序的事情,将帖子拆分为多个页面并使用 jQuery 对它们进行分页。有没有人尝试过类似的东西,你能帮我提供一些设置指南吗?

网站上的类似画廊和类似画廊:http://hitfix.com/galleries/most-anticipated-tv-premieres-and-returns-of-2012

我已经设法制作了类似的东西,但没有使用 jQuery,而且我知道当网站每次都必须为每个页面重新加载时,用户会很恼火。

编辑: 我将在此处添加一个后续问题:

我可以使用什么函数来获取帖子中单个页面的内容? 假设我想将我的帖子分成 5 页 - 我怎样才能获得第 3 页的内容?

【问题讨论】:

    标签: jquery wordpress pagination


    【解决方案1】:

    您可以使用各种slider/slideshow 插件,但如果您想自己制作,那么您可以设置一个导航栏,如您链接到的站点以及单击其中一个数字时您可以对输出所需信息的 PHP 脚本进行 AJAX 调用:

    HTML--

    <ul id="navigation">
        <li><a href="#">1</a></li>
        <li><a href="#">2</a></li>
        <li><a href="#">3</a></li>
        <li><a href="#">4</a></li>
        <li><a href="#">5</a></li>
        <li><a href="#">6</a></li>
    </ul>
    <div id="page"></div>
    

    CSS-

    #navigation li {
        display : inline-block;
    
        /*Fix display:inline-block in IE7*/
        *display : inline;
        zoom     : 1;
    }
    

    jQuery--

    $(function () {
    
        //cache the `#page` element as it will be needed later
        var $page = $('#page');
    
        //bind a `click` event handler to all the `<li>` elements that are children of the `#navigation` ul element
        $('#navigation').children().on('click', function (event) {
            event.preventDefault();
    
            //you could display a loading animation/message here while the new content is being returned from the server
    
            //when making the AJAX call you need to send some data to identify the "page" to output, I used the `id` key and set it's value to the index of the `<li>` element clicked in respect to all the other `<li>`s
            $.get('path/to/server-side.php', { id : $(this).index() }, function (serverResponse) {
    
                //now set the `#page` element's HTML to the response from the server
                $page.html(serverResponse);
    
                //if you played a loading animation/message you can hide it now
            }
        });
    });
    

    这是一个演示:http://jsfiddle.net/jGujw/(请注意,我无法使用 AJAX 函数进行测试,因此我将一些服务器响应存储在一个数组中)

    也可以通过在 AJAX 请求中反复抓取同一页面并解析 serverReponse 以仅将所需元素附加到 DOM 来进行分页。

    请注意,.on() 是 jQuery 1.7 中的新增功能,如果您使用的是旧版本,则与 .bind() 相同。

    【讨论】:

    • 谢谢伙计。我对这个详细的答案感到非常惊喜。我可能问的问题太笼统了,我正在寻找一些指导方针,如何在 Wordpress 方面做到这一点,但这也有帮助,因为我对 jQuery 没有太多经验(但对 php 和 WP 有更多经验)。无论如何,我目前的问题是:我可以使用什么函数来获取帖子中单个页面的内容?假设我想将我的帖子分成 5 页 - 我怎样才能获得第 3 页的内容?我可以在新的...线程/问题中问这个问题,还是被视为垃圾邮件?
    • @stackhouse 您可能想将 Jasper 的问题标记为答案!
    【解决方案2】:

    在 wordpress 世界中,您确实可以访问所有的 wordpress……但您如何获得这种可见性是一个问题。下面是我在主页上使用的代码 sn-p 来获取 3 个最新帖子并显示它们的 sn-ps。如果您超出了 wordpress 的范围,则需要找到一种方法来包含一些 wordpress 文件。

    我相信我提供的内容和一些谷歌搜索可以帮助您解决问题...如果 $myposts 有 3 个帖子数组作为成员,您可以选择。

    <!-- language: php -->
    <?  $myposts = get_posts('numberposts=3');
            foreach($myposts as $post) :
            setup_postdata($post);  ?>
    <h2 id="page_title"><a class="noblock" href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <p class="ccw_light location"><small><?php the_time('l, F jS, Y') ?></small></p>
                            <br style="line-height:5px">
                            <div class="content_text" id="noblock">
                                    <?php the_content('... continues','','') ?>
                                            <span class="right"><small>Posted in <?php the_category(', ') ?> <strong>|</strong> <?php edit_post_link('Edit','','<strong> |</strong>'); ?></small>
                                            </span>
                                    </div>
                            </div>
    <br> <!-- <?php trackback_rdf(); ?> -->
    <?php endforeach; ?>
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多