‘POSTS_PER_PAGE’=>‘10’不显示任何帖子

时间:2019-06-20 作者:Dario B.

每个人

我已经创建了一个自定义的\\u post\\u类型;

function create_post_type_veranstaltungen()
{
register_post_type(\'veranstaltungen\', array(
        \'label\' => __(\'Veranstaltungen\'),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,
        \'menu_icon\' => \'dashicons-admin-site-alt\',
        \'supports\' => array(\'thumbnail\', \'title\', \'editor\', \'author\', 
        \'excerpt\', \'comments\'),
      )
  );
}
add_action(\'init\', \'create_post_type_veranstaltungen\');
有自己的分类部分

function tr_create_my_taxonomy()
{

register_taxonomy(
    \'veranstaltungen-category\',
    \'veranstaltungen\',
    array(
        \'label\' => __(\'Category\'),
        \'rewrite\' => array(\'slug\' => \'veranstaltungen-category\'),
        \'hierarchical\' => true,
    )
);
}

add_action(\'init\', \'tr_create_my_taxonomy\');
然后我创建了4个post,并实现了相应的循环

<?php

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

            $custom_args = array(
                \'post_type\' => \'veranstaltungen\',
                \'orderby\' => \'date\',
                \'order\' => \'DESC\',
                \'paged\' => $paged,
                \'posts_per_page\' => \'10\'

            );
            $wp_query = new WP_Query($custom_args);

            if (have_posts()) :
                while (have_posts()) : the_post();


             ?>

            <div class="pagination">
                <?php
                    $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
                       ));
                 ?>
            </div>
如果income posts\\u per\\u page=>“-1”有效,如果我输入posts\\u per\\u page=>“1”也有效,但如果我输入posts\\u per\\u page=>“10”根本不显示任何内容。

有人知道会发生什么吗?

谢谢

3 个回复
SO网友:Dario B.

经过几天的研究,我发现错误仅仅在于分页。通过更改分页代码,我使其工作正常。

<?php

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

                $custom_args = array(
                    \'post_type\' => \'veranstaltungen\',
                    \'orderby\' => \'date\',
                    \'order\' => \'DESC\',
                    \'paged\' => $paged,
                    \'posts_per_page\' => \'10\'
                );
                $wp_query = new WP_Query($custom_args);

                if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();

                    ?>

                    <div class="accordion">
                        <div class="columns is-mobile is-flexend ">
                            <div class="column is-half">
                                <div class="columns is-mobile is-vcentered">
                                    <div class="column is-one-fifth">
                                        <i class="angle fa fa-angle-down fa-2x" aria-hidden="true"></i>
                                    </div>
                                    <div class="column is-four-fifths">
                                        <?php
                                        $categories = get_the_terms($custom_args->ID, \'veranstaltungen-category\');
                                        $cats = \'\'; // set empty string as  value

                                        foreach ($categories as $i => $category) {
                                            if ($i) echo \', \';
                                            echo \'<span>\' . $category->name . \'</span>\';
                                        }
                                        ?>

                                        <div class="title">
                                            <?php the_title(); ?>
                                        </div>
                                    </div>
                                </div>

                            </div>
                            <div class="column is-half">
                                <div class="postdate">
                                    <p><?php the_field(\'tag\') ?></p>
                                    <p><?php the_field(\'datum\') ?></p>

                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="thepanel">
                        <div class="theimage">
                            <?php the_post_thumbnail(); ?>
                        </div>
                        <?php the_content(); ?>
                    </div>
                <?php endwhile; ?>
                    <div class="columns is-mobile">
                        <div class="column is-3 is-offset-9">
                            <div class="pagination">
                                <?php
                                $nav = get_the_posts_pagination(array(
                                    \'mid_size\' => 2,
                                    \'prev_text\' => \'Zurück\',
                                    \'next_text\' => \'Vor\',
                                    \'screen_reader_text\' => \'A\'
                                ));
                                $nav = str_replace(\'<h2 class="screen-reader-text">A</h2>\', \'\', $nav);
                                echo $nav;
                                ?>
                            </div>
                        </div>
                    </div>
                <?php endif;
                wp_reset_query();
                ?>
完美的阿霍拉功能。格雷西亚斯·托多

SO网友:Bhupen

您错过了要添加到循环中的$wp\\u query->have\\u posts()这样的$wp\\u查询。请参见代码:

$wp_query = new WP_Query($custom_args); 
if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : $wp_query->the_post();

SO网友:Chetan Vaghela

请尝试以下代码:

$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
        $custom_args = array(
                \'post_type\' => \'veranstaltungen\',
                \'orderby\' => \'date\',
                \'order\' => \'DESC\',
                \'paged\' => $paged,
                \'posts_per_page\' => \'10\'
            );
            $wp_query = new WP_Query($custom_args);

        while ( $wp_query->have_posts() ) {
            $wp_query->the_post();

                 echo \'<lable>Post Name : \' . get_the_title() .\'</lable><br/>\';
        }
        wp_reset_query();
           ?>
            <div class="pagination">
                <?php
                    $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
                       ));
                 ?>
            </div>

相关推荐

Ajax load more posts button

我正在尝试使用此教程,但它不起作用:https://artisansweb.net/load-wordpress-post-ajax/正确的顺序是:4号岗位、1号岗位、3号岗位、2号岗位。前2篇帖子是可以的(单击之前),但当我单击按钮时,结果是错误的(第2篇,第1篇),而不是第3篇,第2篇。这是我的代码:页面模板:<?php /* Template Name: Ajax */ get_header(); ?> <div class=\"entry-content