0

I have stored the post-id info in a data-* attribute, I wish to display this content in a div via &.ajax() function. this is the code I'm working on.

  1. A list item that display the post thumb

    <li class="homus-partners-section-single" data-post-id="<?php the_ID(); ?>">
        <?php the_post_thumbnail(); ?>
    </li>     
    
  2. the div where i want to display the item

    <div class="homus-partners-detalis">
        <div class="homus-partners-detalis-img">
            <?php the_post_thumbnail(); ?>
        </div>
        <div class="homus-partners-detalis-info">
            <h4>
                <?php the_title(); ?>
            </h4>
            <p>
                <?php the_content(); ?>
            </p>
        </div>
    </div> 
    
  3. the ajax function

    $(document).delegate('li.homus-partners-section-single', 'click', function(event) {
        event.preventDefault();
        var pb_post_id = $(this).data('post-id');
        var data = {
            'action': 'GetPost',
            postURL : pb_post_id,
        };
        $.post(ajaxURL, data, function(response) {
            $( '.homus-partners-detalis' ).html(response);
        });
    
    });
    
  4. the php function

    function GetPost(){
        $cat = $_POST['catURL'];
        get_template_part($cat);
        exit();
    }
    add_action('wp_ajax_nopriv_GetPost', 'GetPost');
    

EDIT 2

now my code look like this but i have no answer

  1. markup of the clickable element

    <li class="homus-partners-section-single" data-post-slug="<?php echo $post->post_name;?>">
    <?php the_post_thumbnail(); ?>
    

  2. div where i want to display the item

     <div class="homus-partners-detalis">
       <?php get_template_part('single_pb_post_details'); ?>
     </div>
    
  3. php i'm calling in the div

      <div class="homus-partners-detalis-img">
        <?php the_post_thumbnail(); ?>
     </div>
     <div class="homus-partners-detalis-info">
     <h4>
       <?php the_title(); ?>
     </h4>
     <p>
      <?php homus_excerpt('homus_pb_excerpt'); ?>
    </p>
    </div>
    
  4. ajax function

     $(document).delegate('li.homus-partners-section-single', 'click', function(event) {
      event.preventDefault();
      var pb_post_slug = $(this).data('post-slug');
      var data  = {
      'action': 'GetPostDetails',
       postURL : "single_pb_post_details.php?slugid="+ pb_post_slug,
      };
     $.post(ajaxURL, data, function(response) {
        $( '.homus-partners-detalis' ).html(response);
        alert('Got this from the server: ' + response);
    
     });
    
    });
    
  5. the php function

    function GetPostDetails(){
       $details = $_POST['postURL'];
       get_template_part($details);
    exit();
    }
    add_action('wp_ajax_nopriv_GetPostDetails', 'GetPostDetails');
    
16
  • Your GetPost will only return, what? You need to put something to output there, so that there is something in the response of the ajax call... Commented Feb 29, 2016 at 10:17
  • This: .load("../single_pb_post_details.php?slugid=" + pb_post_slug); Is not a good way to load your .php file. Use wp_localize_script instead. Commented Feb 29, 2016 at 10:41
  • i edited the php functin like this function GetPostDetails(){ $details = $_POST['postURL']; get_template_part($details); exit(); } add_action('wp_ajax_nopriv_GetPost', 'GetPostDetails'); but the only answere i have is a zero Commented Feb 29, 2016 at 10:53
  • 0 usually happens if you didn't use die() or exit() in your output (the php function that you call in ajax). Commented Feb 29, 2016 at 10:54
  • i removed the exit(); but the answer is still zero Commented Feb 29, 2016 at 10:56

1 Answer 1

1

As get_template_part just accepts the name of the file without extension, in the comments we've reached this code:

$(document).delegate('li.homus-partners-section-single', 'click', function(event) {
    event.preventDefault();
    var pb_post_slug = $(this).data('post-slug');
    var data  = {
        'action': 'GetPostDetails',
        postURL : "single_pb_post_details",
        post_id : pb_post_slug
    };
    $.post(ajaxURL, data, function(response) {
        $( '.homus-partners-detalis' ).html(response);
        alert('Got this from the server: ' + response);
    });
});

the php code that responds to ajax request:

function GetPostDetails(){
    get_template_part($_POST['postURL']);
    wp_die();
}
add_action('wp_ajax_GetPostDetails', 'GetPostDetails');
add_action('wp_ajax_nopriv_GetPostDetails', 'GetPostDetails');

and in the template file you can check the post_id value with:

$_POST['post_id'];
Sign up to request clarification or add additional context in comments.

3 Comments

@FelipeWlia helped me out, but we can't figure out why the post content is still not displayng
Why is there $_POST['postURL'] inside of get_template_part ?
@Daniel this is quite old... if OP doesn't intend to reuse it for another stuff, that var would have always the same value, so he could call get_template_part( 'single_pb_post_details' );. I hope @NicolaBertelloni had found his way to the right answer...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.