wordpress获取自定义文章列表
时间 : 2024-01-12 16:25:03 声明: : 文章内容来自网络,不保证准确性,请自行甄别信息有效性
最佳答案
要获取WordPress中的自定义文章列表,你可以使用WP_Query函数。以下是一个基本的示例代码:
```php
<?php
$args = array(
'post_type' => 'your_custom_post_type',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// 在这里输出文章信息
the_title(); // 输出文章标题
the_content(); // 输出文章内容
}
}
wp_reset_postdata();
?>
在这个示例中,`post_type`参数应该替换为你的自定义文章类型的名称,比如`product`或`portfolio`等。
你可以根据你的需求修改`$args`数组中的其他参数,比如`posts_per_page`来设置每页显示的文章数量,`order`和`orderby`来排序文章等。
将这段代码插入到你的WordPress主题的相应位置,就能够获取到自定义文章列表,并根据你的需求输出相应的内容了。
其他答案
要获取 WordPress 中的自定义文章列表,您可以使用 WP_Query 类。下面是一个获取自定义文章列表的简单示例代码:
```php
<?php
$args = array(
'post_type' => 'your_custom_post_type', // 替换为您的自定义文章类型
'posts_per_page' => -1, // 显示所有文章,您可以设置显示的数量
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<div class="entry-content">
<?php the_excerpt(); ?>
</div>
<?php
}
wp_reset_postdata(); // 重置文章数据
} else {
// 没有文章的情况下的处理方法
echo '没有自定义文章';
}
?>
在上面的示例代码中,您需要将 `your_custom_post_type` 替换为您的实际自定义文章类型名称。您还可以根据需要修改其他参数,例如 `posts_per_page` 用于设置每页显示的文章数量。
将上述代码添加到您的 WordPress 主题的页面模板文件或自定义模板中,即可获取并显示自定义文章列表。
上一篇
dedecms根目录搬到子目录
下一篇
正常运行的主机装宝塔面板
https/SSL证书广告优选IDC>>
推荐主题模板更多>>
推荐文章







