javascript - WordPress 自定义帖子类型弹出窗口 - jquery 和 ajax?

标签 javascript ajax wordpress popup custom-post-type

我正在尝试使用 jquery (bpopup) 将自定义帖子类型帖子加载到弹出窗口中。我想做的一个例子是 http://outpost.la —只需单击任何图像即可查看弹出窗口。

我认为我最终需要编写一个函数来执行此操作,但我不确定如何执行此操作。到目前为止,我的页面模板中只有两个代码片段:“触发弹出窗口的按钮”和“要弹出的元素”。第一个片段正在执行我想要的操作:将一系列自定义帖子类型标题显示为按钮。但是,应该在弹出窗口中显示自定义帖子类型内容的第二个片段显示了所有自定义帖子类型的标题和内容。

后台带有按钮的弹出窗口的屏幕截图: http://cl.ly/image/1f0G0c2s2J3U

代码: `

<!-- Button that triggers the popup -->

<?php
$args = array( 
    'post_type' => 'portfolio_project',
    'posts_per_page' => -1 );
$loop = new WP_Query( $args );
$i = 0;

echo'<button class="my-button">';

while ( $loop->have_posts() ) : $loop->the_post();

    if ($i % 0 == 0 && $i > 0) {
        echo '</button>' . "\n" . '<button class="my-button">';
    };
?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php

$i++;

endwhile;

echo '</button>';
?>


<!-- Element to pop up -->
<div id="element_to_pop_up">

<?php 
    // the query
    $the_query = new WP_Query( $args ); ?>

    <?php if ( $the_query->have_posts() ) : ?>

      <!-- pagination here -->

      <!-- the loop -->
      <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
      <?php endwhile; ?>
      <!-- end of the loop -->

      <!-- pagination here -->

      <?php wp_reset_postdata(); ?>

    <?php else:  ?>
      <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
                                            <!-- end custom post type loop -->  

    </div>
     <!-- END Element to pop up -->

<script>
// Semicolon (;) to ensure closing of earlier scripting
// Encapsulation
// $ is assigned to jQuery
;(function($) {

     // DOM Ready
    $(function() {

        // Binding a click event
        // From jQuery v.1.7.0 use .on() instead of .bind()
        $('.my-button').bind('click', function(e) {

            // Prevents the default action to be triggered. 
            e.preventDefault();

            // Triggering bPopup when click event is fired
        $('#element_to_pop_up').bPopup({
            appendTo: 'body'
            , position: ['auto','0']
            , positionStyle: 'fixed'
            , scrollBar: 'false'

        });

        });

    });

})(jQuery);
</script>

`

最佳答案

实际上,Wordpressajax 请求使用不同的方法。你必须先明白这一点。所以,简单的原型(prototype)是这样的

// In your functions.php file
add_action( 'wp_ajax_nopriv_ MyAjaxAction', 'MyAjaxFunction' );  
add_action( 'wp_ajax_ MyAjaxAction', 'MyAjaxFunction' ); 
function MyAjaxFunction() {
    // do query for your posts
    // process it
    // echo it
    die();
}

在您的客户端/jQuery 代码中

$(function(){
    $('.myButton').on('click', function(e){
        e.preventDefault();
        // other code
        $.ajax({
            'url':'/wp-admin/admin-ajax.php', // admin-ajax.php will handle the request
            'type':'post',
            'data': {
                // this is the action hook to call the handler "MyAjaxFunction"
                'action': 'MyAjaxAction',
                // pass some data to post variblr to use in php/wordpress (optional)
                'id':'someId' // you can retrieve this using $_POST['id'];
            },
            'success':function(data){
                // returned data by wordpress
            }
        });
    });
});

这是在 WordPress 中处理 ajax 请求的正确方法,请阅读 this article获取更多帮助并作为奖励下载 this book .

关于javascript - WordPress 自定义帖子类型弹出窗口 - jquery 和 ajax?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19326901/

相关文章:

javascript - 对 Shopify's/cart/add.js 的 Ajax POST 请求始终返回错误回调函数

php - 将包含数据的新行添加到 ACF 中的现有转发器

javascript - 带有嵌入 javascript 的 php 在 HTML 标签上输出换行符?

php - 如何让我的 Logo 链接回到 wordpress 22 子主题中的主页?

javascript - 为什么我得到一个 "Uncaught ReferenceError: $ is not defined (anonymous function)"

javascript - 如何使用异步代码在 forEach 循环上进行 promise /回调

javascript - 通过调整 dropzone.js 压缩图像

javascript - 添加到站点时 Js 轮播不工作

javascript - 使用表单参数 onSubmit 运行 PHP 脚本(发送电子邮件)

c# - 对 ASP.NET WebMethod 的 Ajax 调用未命中服务器端方法