【问题标题】:How to use AJAX to change pagination in Wordpress如何使用 AJAX 更改 Wordpress 中的分页
【发布时间】:2011-08-01 04:19:27
【问题描述】:

我正在为客户设计一个基于主题的 wordpress 子主题,其中主要工作组合分页必须能够在用户每次点击“按年份排序”、“按标题排序”和“按标题排序”时更改集合”(使用分类分页)全部使用 ajax。 我在twopoststwoanswers之后编写了一些函数,但它不起作用。我对ajax完全陌生,所以请耐心等待! 任何关于保持干燥的更好方法的建议都非常感谢。 非常感谢您。

更新 我自己解决了这个问题。我打电话给不同的班级,所有的程序都很好。更正下面的代码。

init.js:

$(function(){

var obra_links = $(".select_date, .select_title, .select_collection");
var obra_archive = $(".obra_archive");

obra_links.bind('click', function() {
    var $archive_class = $(this).attr("class");
    $(this).parent().find('.selected').removeClass('selected');
    $(this).toggleClass('selected');
    obra_archive.fadeOut(500);
    $.ajax(
        MyAjax.url,{
            type: 'POST',
            cache: false,
            data: {
                action: 'obra_date_toggle',
                archive_class: $archive_class
            },
            success: function(new_archive){
                obra_archive.html(new_archive);
            },
        })
    obra_archive.fadeIn(500);
}, false);
});

functions.php:

`[...]`

function childtheme_override_head_scripts() {
if ( !is_admin() ) {
// jQuery Google APIs
    wp_deregister_script('jquery');
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false);
    wp_enqueue_script('jquery');
// Custom
    wp_enqueue_script('init', JS . '/init.js', array('jquery'), false, false); // Change last value to true but separate typekit.
// AJAX
    wp_localize_script( 'init', 'MyAjax', array( 'url' => admin_url( 'admin-ajax.php' ) ) );
} add_action('init', 'childtheme_override_head_scripts');

function select_date() {
// New Query
$args = array(
    'post_type' => 'obra',
    'posts_per_page' => '-1',
    'orderby' => 'date',
    'order' => 'ASC',
    //'orderby' => 'title',
    //'taxonomy' => 'colecciones'
); $wp_query = new WP_Query( $args );

if ($wp_query`->`have_posts()) {
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
            <div class="entry">
              <?php the_content('Read the rest of this entry &raquo;'); ?>
            </div>
            <p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?>  <?php comments_popup_link('No Comments ', '1 Comment ', '% Comments '); ?></p>
        </div><?php
    endwhile;
} else { ?>
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn't here.</p>
    <?php get_search_form();
}
}

function select_title() {
`[...]`
}

function select_collection() {
`[...]`
}


function obra_date_toggle() {
if(isset($_POST['archive_class'])) {
    $archive_class = $_POST['archive_class'];
}
switch ($archive_class) {
    case "select_date":
        select_date();
        die();
        break;

    case "select_title":
        select_title();
        die();
        break;

    case "select_collection":
        select_collection();
        die();
        break;

    default:
        echo select_date();
        // For initial load, don't die
}

} add_action( 'wp_ajax_nopriv_obra_date_toggle', 'obra_date_toggle' );
add_action( 'wp_ajax_obra_date_toggle', 'obra_date_toggle' );

obra.php:

<?php
/*
Template Name: Obra
*/

// calling the header.php
get_header();

// action hook for placing content above #container
thematic_abovecontainer();

?>

    <div id="container">

        <?php thematic_abovecontent(); ?>

        <div id="content">
            <p>
                <a class="select_date" href="#">Ordenar por fecha</a><br />
                <a class="select_title" href="#">Ordenar por t&iacute;tulo</a><br />
                <a class="select_collection" href="#">Ordenar por colecci&oacute;n</a><br />
            </p>

            <div class="obra_archive"><?php

                obra_date_toggle();

            ?></div><!-- .obra-archive --><?php

                wp_reset_postdata();

                // calling the widget area 'page-top'
                get_sidebar('page-top');

                // calling the widget area 'page-bottom'
                get_sidebar('page-bottom');

            ?></div><!-- #content -->

        <?php thematic_belowcontent(); ?> 

    </div><!-- #container -->

<?php 

// action hook for placing content below #container
thematic_belowcontainer();

// calling the standard sidebar 
thematic_sidebar();

// calling footer.php
get_footer();

?>

【问题讨论】:

  • íctor,您能详细说明问题吗?您是否收到来自 AJAX 调用的任何数据?它们是否被触发了?
  • 谢谢@AJweb,我就是这么做的。问题是html类。他们有破折号而不是下划线,我忘了退出;谢谢!
  • 请使用 jquery wpmods.com/easily-ajax-wordpress-pagination查看下面的 url 以了解 wordpress 中的 ajax apgination

标签: php jquery ajax wordpress


【解决方案1】:

我还没有尝试过这个插件,但它可能会为你完成这项工作。 http://wordpress.org/extend/plugins/ajaxd-wordpress/

【讨论】:

    【解决方案2】:

    更新我自己解决了这个问题。我打电话给不同的班级,所有的程序都很好。更正下面的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-08
      • 2017-08-06
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      相关资源
      最近更新 更多