I have been following some online tutorials to create a custom tab in the my-account page from woocommerce. This is all going well, accept for adding a shortcode in the custom tab content.
The code for registering the custom tab is like this:
//add new tab to my account
function bbloomer_add_premium_support_endpoint() {
add_rewrite_endpoint( 'mijn-elearning', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'bbloomer_add_premium_support_endpoint' );
// 2. Add new query var
function bbloomer_premium_support_query_vars( $vars ) {
$vars[] = 'mijn-elearning';
return $vars;
}
add_filter( 'query_vars', 'bbloomer_premium_support_query_vars', 0 );
// ------------------
// 3. Insert the new endpoint into the My Account menu
function bbloomer_add_premium_support_link_my_account( $items ) {
$items['mijn-elearning'] = 'Mijn Elearnings';
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'bbloomer_add_premium_support_link_my_account' );
// ------------------
// 4. Add content to the new tab
function bbloomer_premium_support_content() {
echo do_shortcode('[dashboard_student]');
}
add_action( 'woocommerce_account_mijn-elearning_endpoint', 'bbloomer_premium_support_content' );
the shortcode is defined like this:
function dashboard_function($atts = array(),$html = 'test',$name) {
ob_start();
$query = New WP_QUERY( array ('post_type'=>'programma','posts_per_page'=>-1,'meta_query' => array( array( 'key' => 'level', 'value' => 'programma', 'compare' => 'LIKE'),),),);
while($query->have_posts()){
$html .= '<li><a href="'.get_the_permalink.'">'.get_the_title().'</a></li>';
}
$html .= '</ul>';
wp_reset_postdata();
ob_get_clean();
return $html;
}
add_shortcode('dashboard_student','dashboard_function');
function add_to_init() {
add_shortcode('dashboard_student','dashboard_function');
}
add_action('init','add_to_init');
However, it seems like a query in the custom tab is not possible. It gets stuck and even when i give full access to server memory, i get the fatal error: "Allowed memory size of 536870912 bytes exhausted" (or any higher number).
Any ideas?
CodePudding user response:
You need to add the_post()
function.
/**
* Sets up the current post.
*
* Retrieves the next post, sets up the post, sets the 'in the loop'
* property to true.
*
* @since 1.5.0
*
* @global WP_Post $post Global post object.
*/
public function the_post() {
global $post;
$this->in_the_loop = true;
if ( -1 == $this->current_post ) { // Loop has just started.
/**
* Fires once the loop is started.
*
* @since 2.0.0
*
* @param WP_Query $query The WP_Query instance (passed by reference).
*/
do_action_ref_array( 'loop_start', array( &$this ) );
}
$post = $this->next_post();
$this->setup_postdata( $post );
}
I revised your code. Try the below code.
function dashboard_function($atts = array(),$html = 'test',$name) {
ob_start();
$query = New WP_QUERY(
array(
'post_type' => 'programma',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'level',
'value' => 'programma',
'compare' => 'LIKE'
)
)
)
);
$html = '<ul>';
if( $query->have_posts() ) {
while($query->have_posts()){
$query->the_post();
$html .= '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';
}
wp_reset_postdata();
}
$html .= '</ul>';
ob_get_clean();
return $html;
}
add_shortcode('dashboard_student','dashboard_function');