How to load content of custom page template with ajax?

I have a working theme that loads the content from each page with ajax.

function fetch_modal_content() {
if ( isset($_REQUEST) ) {
$page_name = $_REQUEST[‘pageName’];
$page = get_page_by_title( $page_name[0] );
$content = apply_filters(‘the_content’, $page->post_content);
echo $content;

Now I want to make a custom page template which is also loaded via ajax.
The problem is now, when I make a new page which uses this template the post_content is empty, so the template isn’t loaded with ajax.

My idea was to “convert” the template into a shortcode which then can be used.
But the shortcode seems not to work with ajax too, just without.

How can I accomplish that my page template or shortcode is loaded with ajax like all the other pages?

This is the content I want to load with ajax.

div id=”content”>
<div class=”entry-content-page”>
<?php $args = array(
‘numberposts’ => -1,
‘category_name’ => “pictures”

$postslist = get_posts( $args );

foreach ($postslist as $post) : setup_postdata($post);
<h2><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
<?php echo get_the_post_thumbnail( $post->ID, ‘thumbnail’); ?>
<?php endforeach; ?>
<div id=”menuCloseBtn”>
<i class=”far fa-caret-square-left fa-2x”></i>

Read more here:: How to load content of custom page template with ajax?

Leave a Reply

Your email address will not be published. Required fields are marked *