Wordpress媒体计数和下一页导航

时间:2015-09-19 12:41:58

标签: php wordpress

允许用户从前端上传和访问媒体文件。我正在使用以下代码检索媒体文件并在用户前端显示。我的wordpress上传中有40个媒体文件,但我想显示例如单页上有20个媒体文件,我想知道是否有人可以帮忙写上一页&下一页媒体文件的导航代码。例如Page 1 2 3 ... 10

<?php 
$args = array(
'post_type' => 'attachment',
/* 'posts_per_page' => '2', */
'numberposts' => -1,
'post_status' => null,
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
);
    $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>'; 
}
?>

2 个答案:

答案 0 :(得分:0)

您可以尝试使用标准的wordpress导航。

尝试更改你的$ args:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
  'posts_per_page' => 20,
  'paged' => $paged
);

更新:

<?php 
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'attachment',
'posts_per_page' => 20,
'paged' => $paged,
'numberposts' => -1,
'post_status' => null,
'author' => $current_user->ID,
'post_parent' => $post->ID,
'caller_get_posts'=> 1,
);
    $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>'; 
}
?>

更新2

您还需要使用此功能

在帖子下面的某处发布

$big = 999999999; // need an unlikely integer
$navArgs = array(
    'base'         => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format'       => '?page=%#%',
    'total'        => 1,
    'current'      => max( 1, get_query_var('paged') ),
    'show_all'     => False,
    'end_size'     => 1,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »'),
    'type'         => 'plain',
    'add_args'     => False,
    'add_fragment' => '',
    'before_page_number' => '',
    'after_page_number' => ''
); 

echo paginate_links( $navArgs );

如您所见,您可以按自己喜欢的方式设置navArgs。

您需要获取帖子的总数并设置“总计”参数。

答案 1 :(得分:0)

最后我写了我的解决方案

<?php 

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

$args = array(                                                          
    'post_type'     => 'attachment',
    'posts_per_page'=> '10',
    'paged'     => $page,
    'numberposts'=> -1,
    'post_status'   => 'any',
    'author'        => $current_user->ID,
    'post_parent'   => $post->ID,
    'caller_get_posts'=> 1,
    'number'        =>  $display_count,


);

// Custom query, display posts by args defined users
$file_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>'; 
    }    
}

?>

在您的代码中调用查询功能

<?php previous_posts_link( '<< Previous Page', $file_query->max_num_pages ); ?>&nbsp;|&nbsp;<?php next_posts_link( 'Next Page >>', $file_query->max_num_pages ); ?>
相关问题