Wooccommerce-自定义选项卡中的快捷键自定义查询超时-为什么



我一直在学习一些在线教程,以便从wooccommerce在我的帐户页面中创建一个自定义选项卡。一切顺利,接受在自定义选项卡内容中添加快捷代码。

注册自定义选项卡的代码如下:

//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' );

短代码定义如下:

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');

但是,似乎无法在自定义选项卡中进行查询。它被卡住了,即使我完全访问服务器内存,我也会得到致命的错误:";536870912字节的允许存储器大小耗尽";(或任何更高的数字(。

有什么想法吗?

您需要添加the_post()函数。

/**
* 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 );
}

我修改了你的代码。请尝试以下代码。

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');