【问题标题】:Wordpress Media Count and Next Page NavigationWordpress 媒体计数和下一页导航
【发布时间】:2015-09-19 12:41:57
【问题描述】:

允许用户从前端上传和访问媒体文件。我正在使用以下代码检索媒体文件并在用户前端显示。我的 wordpress 上传中有 40 个媒体文件,但我想展示例如单页有 20 个媒体文件,我想知道是否有人可以帮助编写媒体文件的上一页和下一页导航代码。例如第 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>'; 
}
?>

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以尝试使用标准的 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。

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

    【讨论】:

    • 谢谢,上面的代码限制了每页 20 个媒体/帖子,但下一页的数量没有出现在页面上?
    • 尝试设置每页5个帖子,看看数字是否会出现。同时删除 numberposts 参数。
    • 我进行了更改,但这似乎没有帮助,我删除了 numberposts 参数并设置了每页 5 个帖子。我认为如果我们在循环之后添加“上一个”和“下一个”页码和链接,它会起作用。如果我更正了,你能帮我怎么做吗?
    • 描述更改后的当前效果。它显示了什么。您在导航中看到了什么?
    • 我进行了更改,但这似乎没有帮助,没有任何更改,但我看到了最后 5 个媒体/帖子。结果没有显示页码,也看不到我之前上传的文件。
    【解决方案2】:

    我终于写出了我的解决方案

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

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多