I have a page with three tabs. I am using nav-tab-wrapper class and php to to check which tab is active. My third tab is a custom post types for logging. I am trying to load the custom post type under the tab and not redirect to another page. I have not figured out how to do this yet.
I tried using a href="page" but it redirects to the post types page. I tried to use href= page&tab, then include the page on that tab. This causes an error.
<?php $active_tab = isset($_GET['tab']) ? $_GET['tab'] : 'entries'; // end if ?>
<h2 class="nav-tab-wrapper">
<a href="?page=test-app&tab=entries" class="nav-tab <?php echo $active_tab == 'entries' ? 'nav-tab-active' : ''; ?>">Entries</a>
<a href="?page=test-app&tab=maps" class="nav-tab <?php echo $active_tab == 'maps' ? 'nav-tab-active' : ''; ?>">Maps</a>
<a href="?page=test-app&tab=logs" class="nav-tab <?php echo $active_tab == 'logs' ? 'nav-tab-active' : ''; ?>">Logs</a>
</h2>
<form method="post" action="options.php">
<?php
if ($active_tab == 'entries') {
include 'entries.php';
} else if ($active_tab == 'maps') {
include 'maps.php';
} else if ($active_tab == 'logs'){
// THIS IS WHERE I NEED TO LOAD THE POST TYPE TABLE. I WANT TO LOAD THE PAGE UNDER THE TAB.
}
Expected: logs (post_type) table loads under logs tab
Actual Results: I can redirect to the page but I do not know how to load under the actual tab.
The thing you have to do is doing a custom query for that post type to fetch the posts of that type inside that condition ( $active_tab == 'logs' ). You can do it by WP_Query . To know more about WP_Query, please check this link: https://www.billerickson.net/code/wp_query-arguments/
Here you will find the info regarding how to use this function with all the possible parameters.
Here in your code, this would be something like the code below.
else if ($active_tab == 'logs'){
// THIS IS WHERE I NEED TO LOAD THE POST TYPE TABLE. I WANT TO LOAD THE PAGE UNDER THE TAB.
$arg = array(
'post_type' => {POST TYPE NAME},
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Do Stuff, here you print anything regarding your post, i have printed the title for the current post as an example
the_title()
} // end while
} // endif
// Reset Post Data
wp_reset_postdata();
}