当前位置: 首页 > 知识库问答 >
问题:

自定义post类型存档分页

黄逸清
2023-03-14

我有一个自定义的文章类型“author”,我已经成为了归档作者。php,这里是我使用wp pagenavi插件时遇到的问题,我在这个自定义帖子类型中有15篇帖子,我已经使用了$paged,'posts\u per\u page'=

这是我的网站链接,我想这个问题可能来自我的自定义帖子类型。

这是我的archive-author.php密码

<div class="writer-crit-box col-md-9 col-sm-8 col-xs-12">

                    <div class="col-md-11 col-sm-11 col-xs-12">
                        <?php
                        $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
                         $args = array( 'post_type' => 'Author','paged' => $paged,'posts_per_page' =>5); 
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); ?>
                    <a href="<?php the_permalink(); ?>" class="writer-link col-md-12 col-sm-12 col-xs-12">

                        <div class="writer-row1 col-md-12 col-sm-12 col-xs-12 hvr-glow">
                                <div class="col-md-2 col-sm-3 col-xs-12 image-right">
                                    <?php the_post_thumbnail('post-thumbnail',array('class' => 'img-responsive')); ?>
                                </div>
                                <div class="col-md-10 col-xs-12 col-sm-9 col-xs-12 pull-right writer-content">
                                    <h3><?php the_title(); ?></h3>
                                        <h4><?php the_field('auth-trans'); ?></h4>  
                                    <?php if ( get_field('writer-bio') ) { 
                                        echo '<p>'.get_field('writer-bio').'</p>';} ?>

                                    <span>...</span>
                                </div>
                        </div>
                    </a>

                    <?php endwhile; // End of the loop. ?>          

                    <div class="wp-pagenavi row">
                        <div id="wp_page_numbers text-center col-sm-6 center-margin">
                            <ul>
                                <li class="active_page text-center"><?php if(function_exists('wp_pagenavi')) { wp_pagenavi(array( 'query' => $loop )); } ?></li>
                            </ul>
                        </div>
                    </div>              
                </div>

            </div>

这是我的分类学代码

    <?php

function p2p2_register_author(){
    $labels = array(
        'name'               => _x( 'همکاران', 'Author' ),
        'singular_name'      => _x('همکار','Author'),
        'add_new'            => _x( 'افزودن همکار', '' ),
        'add_new_item'       => __( 'افزودن همکاران جدید' ),
        'edit_item'          => __( 'ویرایش همکار' ),
        'new_item'           => __( 'همکار جدید' ),
        'all_items'          => __( 'همه همکاران' ),
        'search_items'       => __( 'جست و جو همکار' ),
        'not_found'          => __( 'همکاری یافت نشد' ),
        'not_found_in_trash' => __( 'همکار در زباله دان یافت نشد' ),
        'parent_item_colon'  => '',
        'menu_name'          => 'همکار'
    );

    register_post_type(
        'Author',
        array (
            'labels'             => $labels,
            'description'   => 'ذخیره اطلاعات مربوط به همکاران',
            'public'             => true,
            'publicly_queryable' => true,
            'show_ui'            => true,
            'show_in_menu'       => true,
            'menu_position'      => 2,
            'query_var'          => true,
            'rewrite'            => array( 'slug' => 'author' ),
            'capability_type'    => 'post',
            'has_archive'        => true,
            'hierarchical'       => false,
            'supports'           => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
        )
    );
}
        add_action('init', 'p2p2_register_author');
?>

我希望你们能帮助我。


共有2个答案

农明辉
2023-03-14

使用get_next_posts_link()get_previous_posts_link()将打破您的错误,并对页面错误保持冷静。

<div class="writer-crit-box col-md-9 col-sm-8 col-xs-12">
<div class="col-md-11 col-sm-11 col-xs-12">
<?php
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array( 'post_type' => 'Author','paged' => $paged,'posts_per_page' =>5); 
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<a href="<?php the_permalink(); ?>" class="writer-link col-md-12 col-sm-12 col-xs-12">
    <div class="writer-row1 col-md-12 col-sm-12 col-xs-12 hvr-glow">
                <div class="col-md-2 col-sm-3 col-xs-12 image-right">
                    <?php the_post_thumbnail('post-thumbnail',array('class' => 'img-responsive')); ?>
                </div>
                <div class="col-md-10 col-xs-12 col-sm-9 col-xs-12 pull-right writer-content">
                    <h3><?php the_title(); ?></h3>
                        <h4><?php the_field('auth-trans'); ?></h4>  
                    <?php if ( get_field('writer-bio') ) { 
                        echo '<p>'.get_field('writer-bio').'</p>';} ?>
                    <span>...</span>
                </div>
        </div>
    </a>
    <?php endwhile; // End of the loop. ?>          
    <div class="wp-pagenavi row">
        <div id="wp_page_numbers text-center col-sm-6 center-margin">
            <ul>
                <li><?php echo get_next_posts_link( 'Next Page', $loop->max_num_pages ); ?></li>
                <li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
            </ul>
        </div>
    </div>              
</div>
</div>
太叔英卫
2023-03-14
<div class="writer-crit-box col-md-9 col-sm-8 col-xs-12">

                <div class="col-md-11 col-sm-11 col-xs-12">
                    <?php
                   $posts = query_posts( 'post_type=author&orderby=title&order=asc&posts_per_page=12' );
                while ( have_posts() ) : the_post(); ?>
                <a href="<?php the_permalink(); ?>" class="writer-link col-md-12 col-sm-12 col-xs-12">

                    <div class="writer-row1 col-md-12 col-sm-12 col-xs-12 hvr-glow">
                            <div class="col-md-2 col-sm-3 col-xs-12 image-right">
                                <?php the_post_thumbnail('post-thumbnail',array('class' => 'img-responsive')); ?>
                            </div>
                            <div class="col-md-10 col-xs-12 col-sm-9 col-xs-12 pull-right writer-content">
                                <h3><?php the_title(); ?></h3>
                                    <h4><?php the_field('auth-trans'); ?></h4>  
                                <?php if ( get_field('writer-bio') ) { 
                                    echo '<p>'.get_field('writer-bio').'</p>';} ?>

                                <span>...</span>
                            </div>
                    </div>
                </a>

                <?php endwhile; // End of the loop. ?>          

                <div class="wp-pagenavi row">
                    <div id="wp_page_numbers text-center col-sm-6 center-margin">
                        <?php pagenavi(); ?>
                    </div>
                </div>              
            </div>

        </div>





    // Add this function to your functions.php file


/* Function that Rounds To The Nearest Value.
 Needed for the pagenavi() function */
 function round_num($num, $to_nearest) {
 /*Round fractions down (http://php.net/manual/en/function.floor.php)*/
 return floor($num/$to_nearest)*$to_nearest;
}

/* Function that performs a Boxed Style Numbered Pagination (also called  Page Navigation).
Function is largely based on Version 2.4 of the WP-PageNavi plugin */
function pagenavi($before = '', $after = '') {
global $wpdb, $wp_query, $post;

$pagenavi_options = array();
$pagenavi_options['pages_text'] = __('Page %CURRENT_PAGE% of  %TOTAL_PAGES%:','viewr');
$pagenavi_options['current_text'] = '%PAGE_NUMBER%';
$pagenavi_options['page_text'] = '%PAGE_NUMBER%';
$pagenavi_options['first_text'] = __('First Page','viewr');
$pagenavi_options['last_text'] = __('Last Page','viewr');
$pagenavi_options['next_text'] = __('Next &raquo;','viewr');
$pagenavi_options['prev_text'] = __('&laquo; Previous','viewr');
$pagenavi_options['dotright_text'] = '...';
$pagenavi_options['dotleft_text'] = '...';
$pagenavi_options['num_pages'] = 3; //continuous block of page numbers
$pagenavi_options['always_show'] = 0;
$pagenavi_options['num_larger_page_numbers'] = 0;
$pagenavi_options['larger_page_numbers_multiple'] = 5;

//If NOT a single Post is being displayed
/*http://codex.wordpress.org/Function_Reference/is_single)*/
if (!is_single()) {

    $request = $wp_query->request;
    //intval — Get the integer value of a variable
    /*http://php.net/manual/en/function.intval.php*/
    $posts_per_page = intval(get_query_var('posts_per_page'));

    //Retrieve variable in the WP_Query class.
    /*http://codex.wordpress.org/Function_Reference/get_query_var*/
    $paged = intval(get_query_var('paged'));
    $numposts = $wp_query->found_posts;

    $max_page = $wp_query->max_num_pages;
    if(is_home()){
        $max_page = $wp_query->max_num_pages - 1;
    }else{
        $max_page = $wp_query->max_num_pages;
    }
    //echo '<script>alert("'.$max_page.'")</script>';
    //empty — Determine whether a variable is empty
    /*http://php.net/manual/en/function.empty.php*/
    if(empty($paged) || $paged == 0) {
        $paged = 1;
    }

    $pages_to_show = intval($pagenavi_options['num_pages']);

    $larger_page_to_show = intval($pagenavi_options['num_larger_page_numbers']);
    $larger_page_multiple = intval($pagenavi_options['larger_page_numbers_multiple']);
    $pages_to_show_minus_1 = $pages_to_show - 1;
    $half_page_start = floor($pages_to_show_minus_1/2);
    //ceil — Round fractions up (http://us2.php.net/manual/en/function.ceil.php)
    $half_page_end = ceil($pages_to_show_minus_1/2);
    $start_page = $paged - $half_page_start;
    if($start_page <= 0) {
        $start_page = 1;
    }

    $end_page = $paged + $half_page_end;
    if(($end_page - $start_page) != $pages_to_show_minus_1) {
        $end_page = $start_page + $pages_to_show_minus_1;
    }
    if($end_page > $max_page) {
        $start_page = $max_page - $pages_to_show_minus_1;
        $end_page = $max_page;
    }
    if($start_page <= 0) {
        $start_page = 1;
    }

    $larger_per_page = $larger_page_to_show*$larger_page_multiple;
    //round_num() custom function - Rounds To The Nearest Value.
    $larger_start_page_start = (round_num($start_page, 10) + $larger_page_multiple) - $larger_per_page;
    $larger_start_page_end = round_num($start_page, 10) + $larger_page_multiple;
    $larger_end_page_start = round_num($end_page, 10) + $larger_page_multiple;
    $larger_end_page_end = round_num($end_page, 10) + ($larger_per_page);

    if($larger_start_page_end - $larger_page_multiple == $start_page) {
        $larger_start_page_start = $larger_start_page_start - $larger_page_multiple;
        $larger_start_page_end = $larger_start_page_end - $larger_page_multiple;
    }
    if($larger_start_page_start <= 0) {
        $larger_start_page_start = $larger_page_multiple;
    }
    if($larger_start_page_end > $max_page) {
        $larger_start_page_end = $max_page;
    }
    if($larger_end_page_end > $max_page) {
        $larger_end_page_end = $max_page;
    }
    if($max_page > 1 || intval($pagenavi_options['always_show']) == 1) {
        /*http://php.net/manual/en/function.str-replace.php */
        /*number_format_i18n(): Converts integer number to format based on locale (wp-includes/functions.php*/
        $pages_text = str_replace("%CURRENT_PAGE%", number_format_i18n($paged), $pagenavi_options['pages_text']);
        $pages_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pages_text);
        echo $before.'<div class="pagenavi">'."\n";

        if(!empty($pages_text)) {
            echo '<span class="pages">'.$pages_text.'</span>';
        }
        //Displays a link to the previous post which exists in chronological order from the current post.
        /*http://codex.wordpress.org/Function_Reference/previous_post_link*/
        echo '<span class="pnlink">';
        previous_posts_link($pagenavi_options['prev_text']);
        echo '</span>';

        if ($start_page >= 2 && $pages_to_show < $max_page) {
            $first_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['first_text']);
            //esc_url(): Encodes < > & " ' (less than, greater than, ampersand, double quote, single quote).
            /*http://codex.wordpress.org/Data_Validation*/
            //get_pagenum_link():(wp-includes/link-template.php)-Retrieve get links for page numbers.
            echo '<a href="'.esc_url(get_pagenum_link()).'" class="first" title="'.$first_page_text.'">1</a>';
            if(!empty($pagenavi_options['dotleft_text'])) {
                echo '<span class="expand">'.$pagenavi_options['dotleft_text'].'</span>';
            }
        }

        if($larger_page_to_show > 0 && $larger_start_page_start > 0 && $larger_start_page_end <= $max_page) {
            for($i = $larger_start_page_start; $i < $larger_start_page_end; $i+=$larger_page_multiple) {
                $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="single_page p1" title="'.$page_text.'">'.$page_text.'</a>';
            }
        }

        for($i = $start_page; $i  <= $end_page; $i++) {
            if($i == $paged) {
                $current_page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['current_text']);
                echo '<span class="current">'.$current_page_text.'</span>';
            } else {
                $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="single_page p2" title="'.$page_text.'">'.$page_text.'</a>';
            }
        }

        if ($end_page < $max_page) {
            if(!empty($pagenavi_options['dotright_text'])) {
                echo '<span class="expand">'.$pagenavi_options['dotright_text'].'</span>';
            }
            $last_page_text = str_replace("%TOTAL_PAGES%", number_format_i18n($max_page), $pagenavi_options['last_text']);
            echo '<a href="'.esc_url(get_pagenum_link($max_page)).'" class="last" title="'.$last_page_text.'">'.$max_page.'</a>';
        }
        echo '<span class="pnlink">';
        next_posts_link($pagenavi_options['next_text'], $max_page);
        echo '</span>';

        if($larger_page_to_show > 0 && $larger_end_page_start < $max_page) {
            for($i = $larger_end_page_start; $i <= $larger_end_page_end; $i+=$larger_page_multiple) {
                $page_text = str_replace("%PAGE_NUMBER%", number_format_i18n($i), $pagenavi_options['page_text']);
                echo '<a href="'.esc_url(get_pagenum_link($i)).'" class="single_page p3" title="'.$page_text.'">'.$page_text.'</a>';
            }
        }
        echo '</div>'.$after."\n";
    }
  }
}
 类似资料:
  • 我的股票与分页我尝试了很多解决方案,但没有工作!我使用WordPress5.4。我的网站是一个单页网站,当然我想在索引中使用它。php 这是我的密码: 很抱歉,如果这是一个高度重复的问题——我发现的其他修复方法都不适用于我。请有人告诉我问题在哪里? 谢谢! 编辑1我尝试了这个,它也不工作!

  • 我有一个自定义的帖子类型设置,有许多类别和子类别。 我试图做的是创建一个页面,显示特定类别中的所有帖子,并使用一个菜单列出所有类别子类别,以便可以过滤帖子。 我已尝试复制存档模板并将其重命名为分类法-(我的自定义分类法)。php,如果我转到slug,它会显示某些帖子,并使用

  • 我最近开发了一个网站, 我创建了一个带有slug'job_post‘的自定义post类型,然后客户端也添加了许多post, 我现在需要将slug更改为“/jobs”, 有没有一种方法可以做到这一点而不删除所有的帖子?我担心我会把网站搞乱, 非常感谢,

  • 我创建了一个自定义超文本标记语言归档博客页面,我想在其中放置WordPress帖子。自定义页面有几个不同的布局,因此在帖子中循环只是重新生成我试图避免的整个页面。我想做的是循环通过每个帖子,然后将该帖子放在相关的档案中。 因此,第1列进入第1列第1列,第2列进入第2列第1列,第3列进入第2列第2列,第4列进入第2列第3列,以此类推。。。 如果这是不可能的,有没有一种方法,我可以有一个代码块,例如,

  • 问题内容: 我在Wordpress网站上有一个自定义帖子类型“新闻”。我正在使用“高级自定义字段”插件将元数据添加到每个帖子。 我想创建一系列新闻条目作为存档: 我设法使使用此工作: 我需要能够使用和链接到特定的年份和月份。 由于这涉及自定义帖子类型“新闻”,因此我不确定如何实现此目的? 问题答案: 为了执行您需要的操作,您需要修改Wordpress重写以能够捕获自定义帖子类型的年/月/等帖子数据

  • 我创建了一个归档页面,我的'事件'自定义帖子猿archive-event.php.我使用的内容从标准archive.php模板,它列出了我的4个事件的顺序,他们被张贴。 下一步是按自定义字段event_date更改订单。我这样做没有问题。然后我不想显示任何事件的日期已经过去。下面的代码完美地实现了这一点。 我现在的问题是我的分页都搞砸了。我的默认阅读设置为每页2篇文章,我有一个“加载更多”按钮来显