[英]$_GET and paginate_links() in WordPress persist in returning me to 1
我正在尝试使用WordPress中的paginate_links()开发用户目录。 以下内容似乎正确设置了正确构建的页面链接的URL(例如,链接3给我https://www.example.com/private-messaging/?pmaction=directory&p=3 )。 但是,$ _ GET ['p']没有捕获页码。 当我用$ _SERVER ['QUERY_STRING']检查它时,我得到pmaction = directory。 当我用print_r($ _ GET)检查它时,无论URL如何,我都会返回1。 当我从$ _GET中提取键和值时,将返回以下内容:键= pmaction,值=目录。 我不确定我缺少什么。 $ _GET使用类似的方法在应用程序中的其他地方都可以正常工作,但在这里不行。 我可能做错了什么?
如果我在浏览器中手动发送此网址: https : //www.example.com/private-messaging/?pmaction=directory&p=3
它剥去结尾并仅显示以下内容: https : //www.example.com/private-messaging/?pmaction=directory
我处于流程的第一步。 如果我手动设置$ page = 3; 我将看到第三步(只有第三步)。
function dispDirectory()
{
global $user_ID;
// Disallow directory if there is a messaging administrator and this person is not it.
if ($this->admin_user_id && $this->admin_user_id != $user_ID) {
return '';
}
$count_args = array(
'role' => 'Client',
'fields' => 'all_with_meta',
'number' => 999999
);
$user_count_query = new WP_User_Query($count_args);
$user_count = $user_count_query->get_results();
$total_users = $user_count ? count($user_count) : 1;
$page = isset($_GET['p']) ? $_GET['p'] : 1;
$users_per_page = 15;
$total_pages = 1;
$offset = $users_per_page * ($page - 1);
$total_pages = ceil($total_users / $users_per_page);
$directory = "<form name='form' method='post'><label for='clientname'>Client Name: </label>
<input name='clientname' id='clientname' type='text' style='width: 181px;'
value=''/><input type='submit'></form>";
$search_string = $_POST["clientname"];
if (!empty($search_string)) {
$args = array (
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_string,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_string,
'compare' => 'LIKE'
)
)
);
}else{
$args = array (
'meta_key' => 'last_name',
// return all fields
'fields' => 'all_with_meta',
'number' => $users_per_page,
'offset' => $offset // skip the number of users that we have per page
);
}
$wp_user_query = new WP_User_Query($args);
$wp_user_query->query_orderby = str_replace( 'user_login', 'wp_usermeta.meta_value', $wp_user_query->query_orderby );
$wp_user_query->query();
$users = $wp_user_query->get_results();
if (!empty($users)) {
foreach($users as $u)
{
$firstName = get_user_meta($u->ID, 'first_name', true);
$lastName = get_user_meta($u->ID, 'last_name', true);
$directory .= '<p><strong>'.$lastName.', '.$firstName.'</strong> - <a href="'.$this->actionURL.'newmessage&to='.$u->ID.'">'.__('Send Message', 'cartpaujpm').'</a> <a href="'.$this->actionURL.'oldmessages&to='.$u->ID.'">'.__('Old Messages', 'cartpaujpm').'</a></p>';
}
} else {
$directory .= '<p><strong>No Clients Found</strong></p>';
}
$directory .= paginate_links( array(
'base' => get_pagenum_link(1) . '%_%', // the base URL, including query arg
'format' => '&p=%#%', // this defines the query parameter that will be used, in this case "p"
'prev_text' => __('« Previous'), // text for previous page
'next_text' => __('Next »'), // text for next page
'total' => $total_pages, // the total number of pages we have
'current' => $page, // the current page
'end_size' => 1,
'mid_size' => 5,
));
return $directory;
}
你在尝试这个吗?
function dispDirectory()
{
global $user_ID;
// Disallow directory if there is a messaging administrator and this person is not it.
if ($this->admin_user_id && $this->admin_user_id != $user_ID) {
return '';
}
$count_args = array(
// 'role' => 'Client',
'fields' => 'all_with_meta',
'number' => 999999
);
$user_count_query = new WP_User_Query($count_args );
$user_count = $user_count_query->get_results();
$total_users = $user_count ? count($user_count) : 1;
$page = isset($_GET['pagenum']) ? $_GET['pagenum'] : 1;
$limit = 2;
$offset = $limit * ($page - 1);
$total_pages = ceil($total_users / $limit);
$directory .= "<form name='form' method='post'><label for='clientname'>Client Name: </label>
<input name='clientname' id='clientname' type='text' style='width: 181px;'
value=''/><input type='submit'></form>";
$search_string = $_POST["clientname"];
if (!empty($search_string)) {
$args = array (
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_string,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_string,
'compare' => 'LIKE'
)
)
);
}else{
$args = array (
'meta_key' => 'last_name',
// return all fields
'fields' => 'all_with_meta',
'number' => $limit,
'offset' => $offset // skip the number of users that we have per page
);
}
$wp_user_query = new WP_User_Query($args);
$wp_user_query->query_orderby = str_replace( 'user_login', 'wp_usermeta.meta_value', $wp_user_query->query_orderby );
$wp_user_query->query();
$users = $wp_user_query->get_results();
if (!empty($users)) {
foreach($users as $u)
{
$firstName = get_user_meta($u->ID, 'first_name', true);
$lastName = get_user_meta($u->ID, 'last_name', true);
$directory .= '<p><strong>'.$lastName.', '.$firstName.'</strong> - <a href="newmessage&to='.$u->ID.'">'.__('Send Message', 'cartpaujpm').'</a> <a href="oldmessages&to='.$u->ID.'">'.__('Old Messages', 'cartpaujpm').'</a></p>';
}
} else {
$directory .= '<p><strong>No Clients Found</strong></p>';
}
echo $directory;
/*$directory = paginate_links( array(
'base' => get_pagenum_link(1) . '%_%', // the base URL, including query arg
'format' => '&p=%#%', // this defines the query parameter that will be used, in this case "p"
'prev_text' => __('« Previous'), // text for previous page
'next_text' => __('Next »'), // text for next page
'total' => $total_pages, // the total number of pages we have
'current' => $page, // the current page
'end_size' => 1,
'mid_size' => 5,
));*/
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'text-domain' ),
'next_text' => __( '»', 'text-domain' ),
'total' => $total_pages,
'current' => $page
) );
if ( $page_links ) {
echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.