我使用以下代码在自定义静态页面上显示媒体文件。文件显示正确,但分页不正确not 工作我如何解决这个问题?
以下代码显示媒体文件:
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$args = array(
\'post_type\' => \'attachment\',
\'posts_per_page\' => \'10\',
\'paged\' => $paged,
/* \'numberposts\' => -1, */
\'post_status\' => null,
\'author\' => $current_user->ID,
\'post_parent\' => $post->ID,
/* \'caller_get_posts\' => 1, */
);
// The Query
$query = new WP_Query( $args );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo \'<tr><td><a href="\' . wp_get_attachment_url( $attachment->ID ) .
\'" rel="shadowbox" title="\' . $attachment->post_excerpt . \'">\';
echo ($attachment->_wp_attached_file);
echo \'</a>
</td>\';
</tr>\';
}
}
将以下内容添加到
functions.php
:
if ( ! function_exists( \'my_pagination\' ) ) :
function my_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'current\' => max( 1, get_query_var( \'paged\' ) ),
\'total\' => $wp_query->max_num_pages,
\'prev_next\' => True,
\'prev_text\' => __( \'« Previous\' ),
\'next_text\' => __( \'Next »\' ),
\'type\' => \'plain\',
\'add_args\' => False,
\'add_fragment\' => \'\',
\'before_page_number\' => \'\',
\'after_page_number\' => \'\'
) );
}
endif;
页码或分页不起作用,在静态页面中调用函数,如下所示:
echo my_pagination( $args );
SO网友:Ubaid Rehman
亲爱的,我在这个问题上浪费了一周的时间,基本上,当你在设置->阅读wordpress中选择静态页面作为你的主页时,列出内容的整个行为都会改变,基本上静态页面永远不会用于分页。事实上,当你调用$paged变量时,即使你将其值定义为1,它也会始终返回零。分页只会刷新你的页面,因为它是一页而不是一页。php有效文件页已经是索引中调用的分类函数。主wordpress的php无论你做什么,函数都会返回相同的东西,
这种变通方法可以生成索引。php文件将静态页面中的所有内容复制到它,然后用索引替换它。php并在设置->阅读wordpress中选择最新帖子
现在,如果您真的不想弄乱它,但仍然想进行分页,那么您需要为分页编写一个小代码
基本上是家。com/page/2只能在家里工作。com/?page=2将始终有效,但更改permalink结构将改变它的每一个方面,因此我们将创建一个分页函数,该函数将使用home调用下一页。com/?页面=2结构url
/* ------------------------------------------------------------------*/
/* PAGINATION */
/* ------------------------------------------------------------------*/
//paste this where the pagination must appear
global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 ) {
// get the current page
if ( !$current_page = get_query_var(\'paged\') )
$current_page = 1;
// structure of "format" depends on whether we\'re using pretty permalinks
if( get_option(\'permalink_structure\') ) {
$format = \'?paged=%#%\';
}
echo paginate_links(array(
\'base\' => get_pagenum_link(1) . \'%_%\',
\'format\' => $format,
\'current\' => $current_page,
\'total\' => $total,
\'mid_size\' => 4,
\'type\' => \'list\'
));
}
如果将此代码包含在当前分页的位置,则会看到它正在工作:)
SO网友:satinder ghai
//它在主页上的工作设置了“paged”参数(如果查询位于静态首页,则使用“page”)
<?php
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : \'1\';
$args = array (
\'nopaging\' => false,
\'paged\' => $paged,
\'posts_per_page\' => \'1\',
\'post_type\' => \'post\',
\'category__not_in\' => array(97),
);
// The Query
$query = new WP_Query( $args );
print_r($query);
$total_pages = $query->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var(\'paged\'));
echo \'<div class="navigation">\';
echo paginate_links(array(
\'base\' => get_pagenum_link(1) . \'%_%\',
\'format\' => \'?paged=%#%\',
\'current\' => $current_page,
\'total\' => $total_pages,
\'type\' => \'list\',
\'prev_text\' => __(\'Previous Posts\'),
\'next_text\' => __(\'More Posts\'),
));
echo \'</div>\';
}