wordpress调用自定义文章类型文章
时间 : 2024-01-05 19:02:02 声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性
最佳答案
要在WordPress中调用自定义文章类型(Custom Post Type)的文章,您可以使用WP_Query类或get_posts()函数。下面是使用这两种方法的示例:
使用WP_Query类:
```php
$args = array(
'post_type' => 'your_custom_post_type',
'posts_per_page' => 5,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// 在这里输出您自定义文章类型的文章内容
the_title();
the_content();
}
wp_reset_postdata();
} else {
// 如果没有文章,显示一条提示信息
echo "没有找到相关文章";
}
使用get_posts()函数:
```php
$args = array(
'post_type' => 'your_custom_post_type',
'posts_per_page' => 5,
);
$custom_posts = get_posts($args);
if ($custom_posts) {
foreach ($custom_posts as $post) {
setup_postdata($post);
// 在这里输出您自定义文章类型的文章内容
the_title();
the_content();
}
wp_reset_postdata();
} else {
// 如果没有文章,显示一条提示信息
echo "没有找到相关文章";
}
以上代码假设您创建了一个名为"your_custom_post_type"的自定义文章类型,每页显示5篇文章。您可以根据自己的需求修改$args数组中的参数。
请将以上代码插入到您想要显示自定义文章类型的地方,如页面模板文件中的循环部分。
其他答案
要在WordPress中调用自定义文章类型的文章,需要按照以下步骤进行操作:
1. 创建自定义文章类型:首先,在functions.php或任意自定义主题文件中添加以下代码来注册自定义文章类型。
function custom_post_type() {
$labels = array(
'name' => '自定义文章类型',
'singular_name' => '自定义文章',
'add_new' => '添加新文章',
'add_new_item' => '添加新的自定义文章',
'edit_item' => '编辑自定义文章',
'new_item' => '新自定义文章',
'view_item' => '查看自定义文章',
'search_items' => '搜索自定义文章',
'not_found' => '未找到自定义文章',
'not_found_in_trash' => '回收站中未找到自定义文章',
'parent_item_colon' => '',
'menu_name' => '自定义文章类型'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-generic',
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'custom_post', $args );
}
add_action( 'init', 'custom_post_type', 0 );
请注意,上述代码中的 'custom_post_type' 是自定义文章类型的名称,您可以根据需要进行修改。
2. 调用自定义文章类型文章:现在,您可以在您的WordPress网站的任何页面或模板文件中使用以下代码来调用自定义文章类型的文章。
```php
$custom_query = new WP_Query(array(
'post_type' => 'custom_post',
'posts_per_page' => 5 // 调整每页显示的文章数量
));
if($custom_query->have_posts()) {
while($custom_query->have_posts()) {
$custom_query->the_post();
// 根据需要显示文章内容或链接等信息
the_title(); // 显示文章标题
the_content(); // 显示文章内容
}
} else {
echo '没有找到相关文章';
}
wp_reset_postdata(); // 重置查询结果
以上代码将从自定义文章类型('custom_post')中检索最多5篇文章,并在循环中显示每篇文章的标题和内容。您可以根据需要自定义显示内容。
3. 模板文件中调用:如果您希望在某个特定的模板文件(例如单独的页面模板)中调用自定义文章类型的文章,您可以使用以下代码。
```php
/*
Template Name: Custom Post Type Template
*/
get_header();
$custom_query = new WP_Query(array(
'post_type' => 'custom_post',
'posts_per_page' => -1
));
if($custom_query->have_posts()) {
while($custom_query->have_posts()) {
$custom_query->the_post();
// 根据需要显示文章内容或链接等信息
the_title(); // 显示文章标题
the_content(); // 显示文章内容
}
}
wp_reset_postdata(); // 重置查询结果
get_footer();
上述代码将在名为 "Custom Post Type Template" 的模板文件中显示所有自定义文章类型的文章。您可以根据需要更改模板名称和显示的内容。
通过按照以上步骤操作,您就可以在WordPress中成功调用自定义文章类型的文章了。
下一篇
宝塔面板默认安装
https/SSL证书广告优选IDC>>
推荐主题模板更多>>
推荐文章







