首页 > 解决方案 > jquery 获取 wordpress php

问题描述

在我的functions.php中我有这个代码:

我不确定在jQuery('.switch_to_prev').click[...]和上写什么jQuery('.switch_to_next').click[...],所以我会得到我的帖子的 AJAX。(我的头脑告诉我,“点击时,查找下一个帖子并在帖子轮播框中切换”或“点击时,查找上一篇帖子并在帖子轮播框中切换”)

函数.php

   // POST CAROUSEL
    function kean_post_loop() {  ?>
    <div id="post_carousel">
    <?php


    $args = array(
        'post_type' => 'post',
        'orderby'   => 'rand',
        'posts_per_page' => 1, 
        //'cat' => 7
        );

    $global = $post;
    $the_query = new WP_Query( $args );

    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
            $the_query->the_post(); ?>
            <?php
                    $terms = get_the_terms( $post->ID , 'category');
                if($terms) {
                    foreach( $terms as $term ) {
                        $cat_obj = get_term($term->term_id, 'category');
                        $cat_slug = $cat_obj->slug;
                    }
                }

                function get_previous_post_id( $post_id ) {
                // Get a global post reference since get_adjacent_post() references it
                global $post;
                // Store the existing post object for later so we don't lose it
                $oldGlobal = $post;
                // Get the post object for the specified post and place it in the global variable
                $post = get_post( $post_id );
                // Get the post object for the previous post
                $previous_post = get_previous_post();
                // Reset our global object
                $post = $oldGlobal;
                if ( '' == $previous_post ) 
                    return 0;
                return $previous_post->ID; 
            } 

            function get_next_post_id( $post_id ) {
                // Get a global post reference since get_adjacent_post() references it
                global $post;
                // Store the existing post object for later so we don't lose it
                $oldGlobal = $post;
                // Get the post object for the specified post and place it in the global variable
                $post = get_post( $post_id );
                // Get the post object for the next post
                $next_post = get_next_post();
                // Reset our global object
                $post = $oldGlobal;
                if ( '' == $next_post ) 
                    return 0;
                return $next_post->ID; 
            } 

                $postcat = get_the_category( $query->post->ID );
                    if ( ! empty( $postcat ) ) {

            ?>
            <div class="carousel_inner relative post_<?php echo $postcat[0]->cat_ID; ?>">

                <div class="post_switch post_prev switch_to_prev">
                <?php 
                        //previous_post_link( '%link', '<div class="post_switch post_prev switch_to_prev"></div>', true ); 
                        echo get_previous_post_id( $current_id );
                        //echo get_previous_post_id( $current_id );
                ?>
                </div>

                <div class="random_post <?php echo $cat_slug; ?>">
                    <h6><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h6>
                    <?php the_excerpt(); ?>
                </div>

                <div class="post_switch post_next switch_to_next">
                <?php 
                        //next_post_link( '%link', '<div class="post_switch post_next switch_to_next"></div>', true );
                        echo get_next_post_id( $current_id );
                        //echo get_next_post_id( $current_id );
                ?>
                </div>


            </div>
            <?php
            }
        }
        wp_reset_postdata();
    } else {
    }

    ?>
    </div>
    <?php
    } 
    add_shortcode('post_carousel','kean_post_loop');

自定义.js

    jQuery(document).ready(function () {
    // ToDo
        jQuery('.switch_to_next').click(function () {
            jQuery('.post_7').addClass("hidden");
            jQuery('.post_7').removeClass("active");
            jQuery('.post_8').addClass("active");
            jQuery('.post_8').removeClass("hidden");
        });

        jQuery('.switch_to_prev').click(function () {
            jQuery('.post_1').addClass("hidden");
            jQuery('.post_1').removeClass("active");
            jQuery('.post_8').addClass("active");
            jQuery('.post_8').removeClass("hidden");
        });
    });

标签: phpjquerywordpress

解决方案


推荐阅读