Is there a way of loading a template file without having a post? I’m loading set of data via an API. Set up my index page but having a brain freeze with single template files as these posts are not in the WP registry/database.

// Template files
events-index.php
events-single.php

Events index:

 <?php $events = $api_data // Data retrieved from API

   if ( $upcoming_events->have_posts() ) :
     while ( $upcoming_events->have_posts() ) : $upcoming_events->the_post();

     $custom_link = sanitize_title( get_the_title() );
     $custom_link = rtrim($custom_link, "https://wordpress.stackexchange.com/");
     $custom_link .= '?' . get_the_ID(); ?>

     <a href="https://wordpress.stackexchange.com/questions/338042/<?php echo $custom_link; ?>">
     // This link to a custom template file
 <?php } ?>

2 Answers
2

You could setup a custom rewrite rule using add_rewrite_rule and send these requests to a custom page.

Your URL structure could be /index-page/single-event-slug The rule would point all of the single events to a separate page where you can then set your template events-single.php.

Another option is to pass the events in as a query /index-page?event=single-event-slug or /index-page?event=123. You’d need to register this parameter with add_query_arg and then change your index template to use the event template via the template_include hook.

Leave a Reply

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