如何在自定义帖子类型页面上列出自定义帖子类型?

时间:2016-10-29 作者:Abstractic

我想知道为什么我的自定义帖子类型页面不接受分页页面?

在普通页面上,我可以列出自定义帖子类型,但在自定义帖子类型页面上,这似乎是不可能的。我想制作自定义列表页面,根据各种查询列出自定义帖子类型。

一、 e.如果我想在带有分页的页面上列出所有自定义帖子类型,URL或永久链接结构将如下所示:http://www.url.com/taxonomy/document/page/2/, 但我根本无法访问该页面,它只是将其他页面(如2、3、4等)重定向到根页面http://www.url.com/taxonomy/document/.

我尝试了我在网上找到的各种奇怪而奇妙的解决方案,但似乎没有任何解决方案?

我不想将标准页面与自定义模板一起使用的原因是,它需要特定的设置来设置搜索查询,而普通页面不应该这样做。

这是代码片段,它在自定义页面模板(template list.php)上完全可以正常工作,但在自定义post类型页面上则不行。

$entry = new ThemeOptions\\PropertyListing();

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

$args = array(

    \'post_type\'         => \'property\',
    \'posts_per_page\'    => $max,
    \'paged\'             => $paged,
    \'tax_query\'         => \'\',
    \'meta_query\'        => \'\',
    \'post_status\'       => \'publish\',
    \'orderby\'           => \'\',
    \'order\'             => \'DESC\'
);

$query = new WP_Query($args);

if ($query->have_posts()) :

    while($query->have_posts()) : $query->the_post();

        echo $entry->display($post);

    endwhile;

    $wp_query = $query;

    get_template_part(\'/includes/layout/pagination\', \'\');

else:

    echo "No posts found!! <br>";

endif;
这是我想列出其他帖子类型的自定义帖子类型,并且分页或分页页面失败。

[slug] => listing
[label] => Listing
[labels] => Array
    (
        [name] => Listings
        [singular_name] => Listing
        [menu_name] => Listings
        [all_items] => Listings
        [add_new] => Add New
        [add_new_item] => Add New Listing
        [edit] => Edit
        [edit_item] => Edit Listing
        [new_item] => New Listing
        [view] => View Listing
        [view_item] => View Listing
        [search_items] => Search Listings
        [not_found] => No Listings found
        [not_found_in_trash] => No Listings found in Trash
        [parent_item_colon] => Parent Listing
    )

[description] => Listing page is a custom page type to store and display a search listing page.
[public] => 1
[exclude_from_search] => 1
[publicly_queryable] => 1
[show_ui] => 1
[show_in_nav_menus] => 1
[show_in_menu] => 1
[show_in_admin_bar] => 1
[menu_position] => 21
[menu_icon] => dashicons-admin-page
[capability_type] => listing
[capabilities] => Array
    (
        [read] => read
        [edit_post] => edit_listing
        [edit_posts] => edit_listings
        [edit_others_posts] => edit_others_listings
        [edit_private_posts] => edit_private_listings
        [edit_published_posts] => edit_published_listings
        [publish_posts] => publish_listings
        [read_post] => read_listing
        [read_private_posts] => read_private_listings
        [delete_post] => delete_listing
        [delete_posts] => delete_listings
        [delete_others_posts] => delete_others_listings
        [delete_private_posts] => delete_private_listings
        [delete_published_posts] => delete_published_listings
    )

[map_meta_cap] => 1
[hierarchical] => 
[register_meta_box_cb] => 
[has_archive] => 
[rewrite] => Array
    (
        [slug] => listing
    )

[query_var] => 1
[can_export] => 1
[supports] => Array
    (
        [0] => title
        [1] => editor
        [2] => author
        [3] => thumbnail
        [4] => revisions
        [5] => page-attributes
    )
我不确定是否缺少允许分页的内容

1 个回复
SO网友:Abstractic

似乎需要覆盖自定义帖子类型上的规范重定向才能使分页正常工作。它与上面使用和描述的自定义查询没有任何关系,因为这在普通页面和现在的自定义帖子类型上都可以很好地工作。

我发现我可以在函数文件中使用以下内容:

/**
* Prevents custom post type pages from being redirected on pagination
*
* @param $url   - root URL
*/

if (! function_exists(\'disable_redirect_canonical\') ) :

    function disable_redirect_canonical($url) {

        $accepted = array(\'listing\');

        if (is_paged() && is_singular() && in_array(get_post_type(), $accepted)) { $url = false; }

    return $url; 
}
endif;
add_filter(\'redirect_canonical\', \'disable_redirect_canonical\');

相关推荐

Increment paged on WP_Query

在WP\\U查询调用下增加“paged”的正确方法是什么?以下是我的情况: $custom_query = new WP_Query([ \'post_type\' => \'employee\', \'post_status\' => \'any\', \'posts_per_page\' => 10, \'paged\' => 1, ]);