• function load_page () {
       $pid = url_to_postid( $_POST['posturl'] );
       $file = null;
       global $post;
       setup_postdata( $pid ); 
    
       $templateLink = get_template_directory() . get_field("template_ajax_link",$pid);
    
       if(file_exists($templateLink)){
            $file = $template_theme_file;
       }
        echo $file;
       die();
    }
    add_action ( 'wp_ajax_nopriv_load_page', 'load_page' );
    add_action ( 'wp_ajax_load_page', 'load_page' );

    As you can see i am trying to get the a wordpress post to load through a file i have created.

    Structure is like this

    Ajax request is made

    get post ID – done

    get template directory for the file – done

    get the the file contents to return to the ajax request

    The file contents are like

    <main role="main">
        <?php if (have_posts()): while (have_posts()) : the_post(); ?>
        <!-- section -->
            <section>
    
                <h1><?php the_title(); ?></h1>
    
            </section>
        <!-- /section -->
        <?php endwhile; ?>
        <?php endif; ?>
       </main>

    As you can see the post data has to go through to the file so i can get details like the title.

    Thanks

  • The topic ‘Load post/page through ajax WordPress’ is closed to new replies.