S
S
Sergey Shilov2018-05-04 16:24:33
WordPress
Sergey Shilov, 2018-05-04 16:24:33

How to load the get_template_part parameter in wordpress using ajax?

I’ll say right away that I’m not strong in this matter, but now my response from wordpress occurs in the functions.php file, and I would like to make the output template a separate file, but I don’t understand how to do it correctly.
Now the code looks like this:
js

jQuery(function($) {
        $('body').on('click', '.portfolio-item-link', function() {
            var id_post = $(this).attr('rel'),
                ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
            $.ajax({
                type: 'POST',
                url: ajaxurl,
                data: {
                    'post_id': id_post,
                    'action': 'f711_get_post_content'
                },
                success: function(result) {
                    alert('done');
                }
            });
        });
    });

php in function.php:
add_action( 'wp_ajax_f711_get_post_content', 'f711_get_post_content_callback' );
add_action( 'wp_ajax_nopriv_f711_get_post_content', 'f711_get_post_content_callback' );

function f711_get_post_content_callback() {

    // retrieve post_id, and sanitize it to enhance security
    $post_id = intval($_POST['post_id'] );
    
    // get the post
    $thispost = get_post( $post_id, ARRAY_A );
    
    $post_thumb = get_the_post_thumbnail($post_id);
    $post_title = get_the_title($post_id);
    $post_content = apply_filters( 'the_content', get_the_content() );

    echo '
        <div class="ajax-post-content text-block">
            <div class="post-content">
                <h1 class="post-content-title">' . $post_title . '</h1>
                ' . apply_filters('the_content', get_post_field('post_content', $post_id)) . '
            </div>
        </div>
    ';
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alvvi, 2018-05-04
@alvvi

Try the function hm_get_tempalte_partfrom here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question