wordpress获取指定分类文章,调用缩略图,标题,日期,作者,分类,标签,随机排序,分类ID6,7,8
在WordPress中,你可以使用WP_Query
类来获取指定分类下的文章,并调用文章的缩略图、标题、日期、作者、分类、标签等信息。以下是一个示例代码,展示了如何实现这一功能:
方法一:不带样式
<?php
// 指定分类ID
$category_ids = array(6, 7, 8); // 替换为你需要的分类ID数组
// 创建WP_Query对象
$query = new WP_Query(array(
'category__in' => $category_ids, // 指定分类ID数组
'posts_per_page' => -1, // 获取所有文章,或者指定一个数字来获取特定数量的文章
'orderby' => 'date', // 按照日期排序 'rand'随机排序 'date'按照日期排序
'order' => 'DESC' // 降序排序
));
// 检查是否有文章
if ($query->have_posts()) {
// 循环输出文章
while ($query->have_posts()) {
$query->the_post();
// 输出文章缩略图
if (has_post_thumbnail()) {
the_post_thumbnail('thumbnail'); // 你可以指定其他尺寸,如 'medium', 'large', 'full' 或自定义尺寸
}
// 输出文章标题
echo '<h2>' . get_the_title() . '</h2>';
// 输出文章日期
echo '<p>' . get_the_date() . '</p>';
// 输出文章作者
echo '<p>' . get_the_author() . '</p>';
// 输出文章分类
$categories = get_the_category();
if (!empty($categories)) {
echo '<p>分类:';
foreach ($categories as $category) {
echo $category->cat_name . ' ';
}
echo '</p>';
}
// 输出文章标签
$tags = get_the_tags();
if (!empty($tags)) {
echo '<p>标签:';
foreach ($tags as $tag) {
echo $tag->name . ' ';
}
echo '</p>';
}
// 如果你想直接输出分类ID(在这个例子中可能不需要,因为已经输出了分类名称)
// echo '<p>分类ID:' . implode(', ', $category_ids) . '</p>';
}
// 重置文章数据
wp_reset_postdata();
} else {
// 没有找到文章
echo '<p>没有找到指定分类下的文章。</p>';
}
?>
将上面的代码添加到你的WordPress主题文件或者插件中,它将输出分类ID为6、7、8的文章的缩略图、标题、日期、作者、分类和标签。你可以根据需要调整WP_Query
参数的值。
方法二:输出文章缩略图带img 带样式方法
<section class="featured-stories">
<div class="row gutter-parent-10">
<?php
// 指定分类ID
$category_ids = array(6, 7, 8); // 替换为你需要的分类ID数组
// 创建WP_Query对象
$query = new WP_Query(array(
'category__in' => $category_ids, // 指定分类ID数组
'posts_per_page' => -1, // 获取所有文章,或者指定一个数字来获取特定数量的文章
'orderby' => 'date', // 按照日期排序
'order' => 'DESC' // 降序排序
));
// 检查是否有文章
if ($query->have_posts()) {
// 循环输出文章
while ($query->have_posts()) {
$query->the_post();
// 获取文章的永久链接
$permalink = get_permalink();
echo '<div class="col-sm-6 col-lg-3 post-col">'; //我加的
echo '<div class="post-boxed">'; //我加的
echo '<div class="post-img-wrap">'; //我加的
// 输出文章缩略图并添加A标签
if (has_post_thumbnail()) {
echo '<div class="featured-post-img"> <a href="' . $permalink . '">' . get_the_post_thumbnail($post->ID, 'full') . '</a></div>'; // 你可以指定其他尺寸,如 'thumbnail', 'medium', 'large', 'full' 或自定义尺寸
}
// 输出文章分类并添加A标签
$categories = get_the_category();
if (!empty($categories)) {
echo '<div class="entry-meta category-meta"><div class="cat-links">';
foreach ($categories as $category) {
echo '<a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a> ';
}
echo '</div></div>';
}
// 输出文章标签并添加A标签
$tags = get_the_tags();
if (!empty($tags)) {
echo '<div class="entry-meta category-meta"><div class="cat-links">';
foreach ($tags as $tag) {
echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a> ';
}
echo '</div></div>';
}
echo '</div>'; //我加的
echo '<div class="post-content">'; //我加的
// 输出文章标题并添加A标签
echo '<h3 class="entry-title"><a href="' . $permalink . '">' . get_the_title() . '</a></h3>';
echo '<div class="entry-meta">'; //我加的
// 输出文章日期并添加A标签(通常日期不需要链接,但这里按要求添加)
echo '<div class="date"><a href="' . $permalink . '">' . get_the_date() . '</a></div>';
// 输出文章作者并添加A标签(通常作者不需要链接,但这里按要求添加,可以链接到作者页面)
echo '<div class="by-author vcard author"><a href="' . get_author_posts_url(get_the_author_meta('ID')) . '">' . get_the_author() . '</a></div>';
echo '</div>'; //我加的
echo '</div>'; //我加的
echo '</div>'; //我加的
echo '</div>'; //我加的
}
// 重置文章数据
wp_reset_postdata();
} else {
// 没有找到文章
echo '<p>没有找到指定分类下的文章。</p>';
}
?>
</div>
</section>
方法三:输出文章缩略图不带img 带样式方法
<section class="featured-section">
<div class="container">
<section class="featured-stories">
</section>post-boxed
</div>
</section>post-boxed
<div class="row gutter-parent-10">
<?php
// 指定分类ID
$category_ids = array(6, 7, 8); // 替换为你需要的分类ID数组
// 创建WP_Query对象
$query = new WP_Query(array(
'category__in' => $category_ids, // 指定分类ID数组
'posts_per_page' => -1, // 获取所有文章,或者指定一个数字来获取特定数量的文章
'orderby' => 'date', // 按照日期排序
'order' => 'DESC' // 降序排序
));
// 检查是否有文章
if ($query->have_posts()) {
// 循环输出文章
while ($query->have_posts()) {
$query->the_post();
// 获取文章的永久链接
$permalink = get_permalink();
echo '<div class="col-sm-6 col-lg-3 post-col">'; //我加的
echo '<div class="post-boxed">'; //我加的
echo '<div class="post-img-wrap">'; //我加的 post-img-wrap
// 输出文章缩略图并添加A标签
if (has_post_thumbnail()) {
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail_url = wp_get_attachment_image_src($thumbnail_id, 'full');
if ($thumbnail_url) {
echo '<div class="featured-post-img"> <a href="'.$permalink.'" class="post-img" style="background-image:url('.$thumbnail_url[0].');"></a></div>';
}
}
// 输出文章分类并添加A标签
$categories = get_the_category();
if (!empty($categories)) {
echo '<div class="entry-meta category-meta"><div class="cat-links">';
foreach ($categories as $category) {
echo '<a href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a> ';
}
echo '</div></div>';
}
// 输出文章标签并添加A标签
$tags = get_the_tags();
if (!empty($tags)) {
echo '<div class="entry-meta category-meta"><div class="cat-links">';
foreach ($tags as $tag) {
echo '<a href="' . get_tag_link($tag->term_id) . '">' . $tag->name . '</a> ';
}
echo '</div></div>';
}
echo '</div>'; //我加的 post-img-wrap
echo '<div class="post-content">'; //我加的
// 输出文章标题并添加A标签
echo '<h3 class="entry-title"><a href="' . $permalink . '">' . get_the_title() . '</a></h3>';
echo '<div class="entry-meta">'; //我加的
// 输出文章日期并添加A标签(通常日期不需要链接,但这里按要求添加)
echo '<div class="date"><a href="' . $permalink . '">' . get_the_date() . '</a></div>';
// 输出文章作者并添加A标签(通常作者不需要链接,但这里按要求添加,可以链接到作者页面)
echo '<div class="by-author vcard author"><a href="' . get_author_posts_url(get_the_author_meta('ID')) . '">' . get_the_author() . '</a></div>';
echo '</div>'; //我加的
echo '</div>'; //我加的
echo '</div>'; //我加的
echo '</div>'; //我加的
}
// 重置文章数据
wp_reset_postdata();
} else {
// 没有找到文章
echo '<p>没有找到指定分类下的文章。</p>';
}
?>
</div>