2

界面图片
我创建了一个 jquery 脚本来 AJAX 加载内容。此内容包含由自定义帖子类型制作的帖子。作为参考,我使用了 Loading WordPress posts with Ajax 和 jQuery

问题我将用什么替换 .load URL,以允许每次单击 #ajaxNext 时它都会转到上一个提交的自定义帖子类型 URL?

示例:当前显示的自定义帖子发布于 2011 年 8 月 30 日。*点击 #ajaxNext 后帖子更新为 2011 年 8 月 24 日的帖子*

参考 Wordpress 文档。
jQuery此代码保存在 single-custom_type.php 的标头中

<script type="text/javascript">
    $(document).ready(function(){
    $.ajaxSetup({cache:false});
    $("#ajaxNext").click(function(){
        var post_id = $(this).attr("rel")
        $("#container").html("loading...");
        $("#container").load("<?php get_adjacent_post_rel_link( $title, $in_same_cat, $excluded_categories, $previous ); ?>");
        return false;
    });
});
</script>

$("#container").load('<?php get_adjacent_post_rel_link( $title, $in_same_cat, $excluded_categories, $previous ); ?>');就是创建请求 URL 的原因(我输入了一个直接 URL 用于测试目的,它可以工作。不幸的是,这个 php 不起作用)。我希望 .load 的 URL 成为 wordpress 中的下一篇文章。我已经按照教程进行了操作。由于我使用自定义帖子类型,不得不进行一些调整。它仍然没有运气:(

PHP 逻辑上的 home.php 包含下面的 PHP 参考

<div id="wrapper">
  <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
        <div id="inner">
            <?php 
                if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
                  the_post_thumbnail(full);
                } 
            ?>

        </div>
</div>

HTML 这个特定的 html 块包含执行 AJAX 调用的锚点

    <section class="summary">
  <div>
    <header> <img src="<?php echo get_template_directory_uri(); ?>/library/images/works.png"  />
      <h1>Process</h1>
      <h2><a id="ajaxNext" href="<?php the_permalink() ?>" rel="<?php the_ID(); ?>" title="<?php the_title_attribute(); ?>">Next</a></h2>
    </header>
    <p>
      <p><?php getCustomField('Process'); ?></p>
    </p>
  </div>
</section>

如果我缺少任何东西,请不要犹豫,问!我会立即回复。

最后,如果有前一个按钮并且代码完全不同,我将如何创建该 .load(); URL 请求,即转到最新的帖子。

4

1 回答 1

0

您使用了错误的功能。get_adjacent_post_rel_link()返回一个完整的链接——<link rel="prev"...>您需要原始 URL,而不是整个链接。不幸的是,令我惊讶的是,我找不到一个“正确”的功能。有很多解决方案可以解决这个问题,但我倾向于从我所拥有的内容中提取 URL。

$next = get_adjacent_post_rel_link();
preg_match('/href=\'([^\']*)\'/',$next,$matches);
var_dump($matches[1]);
于 2012-10-14T16:59:19.593 回答