Support » Fixing WordPress » Show post content in other divs

  • Hi, I would like to ask if there is anybody who knows how to display the post content in the other div without having to refresh the page once you click the post title?

    Here’s my code in the single.php:

    <?php if ($slideQuery->have_posts()) : while ($slideQuery->have_posts()) : $slideQuery->the_post(); ?>
        <div id="<?php $category = get_the_category(); echo $category[0]->cat_name; ?>"> 
          <a class="post-link" rel="<?php the_ID(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?> </a> 
        </div>
    
        <?php $post = get_post($_POST['id']); ?>
        <div id="single-post-container"></div>
    
        <div id="single-post post-<?php the_ID(); ?>">
          <?php the_content();?>
        </div>
     
        <?php endwhile; endif; ?>
    

    and this is my code in the js:

    
     $(document).ready(function(){
     
            $.ajaxSetup({cache:false});
            $(".post-link").click(function(){
                var post_link = $(this).attr("href");
     
                $("#single-post-container").html("content loading");
                $("#single-post-container").load(post_link);
            return false;
            });
     
        });
    

    Thank you in advaced.

    • This topic was modified 1 year, 4 months ago by dreamy.
Viewing 1 replies (of 1 total)
  • Hi @maryann17 ,

    I would suggest to use default wordpress ajax through admin-ajax URl. Please refer the link below for the same:

    https://codex.wordpress.org/AJAX_in_Plugins

    Now steps for loading post content:

    1. Setup an ajax through Admin URL as specified above
    2. The ajax call should send post_id as a parameter
    3. Fetch the post content from the database as shown in code below:
      
      $contentPost = get_post($post_id);
      $content = $contentPost->post_content;
      
    4. Send the $content variable as response to the ajax call
    5. Use this response anywhere you want

    Considering all the desired content is stored in post_content column only. Some page builders store the content in postmeta table.

Viewing 1 replies (of 1 total)
  • The topic ‘Show post content in other divs’ is closed to new replies.