我正在尝试使用php循环加载我的自定义帖子,并且它仅加载5个帖子。我正在使用CPT UI Wordpress插件代码注册帖子:
function cptui_register_my_cpts_homepage_brands() {
/**
* Post Type: Homepage Brands.
*/
$labels = array(
"name" => __( "Homepage Brands", "astra" ),
"singular_name" => __( "Homepage Brands", "astra" ),
);
$args = array(
"label" => __( "Homepage Brands", "astra" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "homepage_brands", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail" ),
);
register_post_type( "homepage_brands", $args );
}
add_action( 'init', 'cptui_register_my_cpts_homepage_brands' );
并且我使用以下代码创建循环并从循环中加载帖子中的图像:
<div class="your-class">
<?php
$args = array(
'post_type' => 'homepage_brands',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$brand = get_posts($args);
foreach ($brand as $post) {
setup_postdata($post);
$thumbnail = get_the_post_thumbnail_url($post->ID, 'full');
if (!$thumbnail)
continue;
?>
<div><img src="<?php echo $thumbnail; ?>"></div>
<?php
}
wp_reset_postdata();
?>
</div>
是的,我检查了Wordpress帖子设置,并且帖子数量设置为数百。而且,奇怪的是,我使用相同的确切方法来循环另一个自定义帖子,并且它起作用了。
我认为我注册帖子类型的方式有些问题,但我不确定。 我只是在不同的服务器上尝试了相同的方式/代码,所以一切运行正常。
答案 0 :(得分:0)
此问题是由错误的“ Post Migrate” wordpress插件引起的。删除它并创建新的帖子类型可以解决此问题。
答案 1 :(得分:0)
function cptui_register_my_cpts_homepage_brands() {
/**
* Post Type: Homepage Brands.
*/
$labels = array(
'name' => _x( 'Homepage Brands', 'astra' ),
'singular_name' => _x( 'Homepage Brands', 'astra' ),
'menu_name' => _x( 'Homepage Brands', 'astra' ),
'name_admin_bar' => _x( 'Homepage Brands', 'astra' ),
'add_new' => __( 'Add New Brands', 'astra' ),
'add_new_item' => __( 'Add New Brands', 'astra' ),
'new_item' => __( 'New Brands', 'astra' ),
'edit_item' => __( 'Edit Brands', 'astra' ),
'view_item' => __( 'View Brands', 'astra' ),
'all_items' => __( 'All Brands', 'astra' ),
'search_items' => __( 'Search Brands', 'astra' ),
'parent_item_colon' => __( 'Parent Brands:', 'astra' ),
'not_found' => __( 'No Brands found.', 'astra' ),
'not_found_in_trash' => __( 'No Brands found in Trash.', 'astra' ),
'featured_image' => _x( 'Brands Image', 'astra' ),
'set_featured_image' => _x( 'Set Brands image', 'astra' ),
'remove_featured_image' => _x( 'Remove Brands image', 'astra' ),
'use_featured_image' => _x( 'Use as Brands image', 'astra' ),
'archives' => _x( 'Project archives', 'astra' ),
'insert_into_item' => _x( 'Insert into Brands', 'astra' ),
'uploaded_to_this_item' => _x( 'Uploaded to this Brands', 'astra' ),
'filter_items_list' => _x( 'Filter Brands list', 'astra' ),
'items_list_navigation' => _x( 'Brands list navigation', 'astra' ),
'items_list' => _x( 'Brands list', 'astra' ),
);
$args = array(
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => true,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => false,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "homepage_brands", "with_front" => true ),
"query_var" => true,
"supports" => array( "title", "editor", "thumbnail" ),
);
register_post_type( "homepage_brands", $args );
}
add_action( 'init', 'cptui_register_my_cpts_homepage_brands' );
<div class="your-class">
<?php
$args = array(
'post_type' => 'homepage_brands',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
);
$brand = new WP_Query($args);
if ( $brand->have_posts() ) {
while ( $brand->have_posts() ) {
$brand->the_post();
$thumbnail = get_the_post_thumbnail_url(get_the_ID(), 'full');
if (!$thumbnail)
continue;
?>
<div><img src="<?php echo $thumbnail; ?>"></div>
<?php
}
}
wp_reset_postdata();
?>
</div>