0

Im not precisely sure, what to write at jQuery('.switch_to_prev').click[...] and jQuery('.switch_to_next').click[...], so I will get an AJAX of my posts. (My head tells me this, "On click, look for next post and switch it in the post carousel box" or "On click, look for previous post and switch it in the post carousel box". Basically Prev and Next buttons, that load next or previous posts by AJAX, not by redirecting you to another posts site)

functions.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');

custom.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");
        });
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.