
1
2
3
4
5
6
7
$args = array(
'ignore_sticky_posts' => 1,//忽略sticky_posts,即不置顶
);
$query = new WP_Query( $args );
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); >
<a href="<php the_permalink() >"><php the_title(); ></a>
<php endwhile;endif; >
在循环中你可以使用WordPress的调用函数都是可以的,根据需要修改即可。
你的代码太长太复杂,用我这个比较简单。 上面是调取指定分类的代码,你可以自行美化这个列表。在这之后,你可以添加分页代码,网上有相关的插件,也有纯代码完成的。 也就是说你不必写太多的函数来完成你想要的效果。
用法:
参数:$cat_name 分类名称 默认值为"General"。类型为字符型可选
返回的值:出错时返回0,成功则返回分类ID号,类型为整数
示例:
<php
$category_id = get_cat_id('Category Name');
query_posts('cat='$category_id);
if (have_posts()) : while (have_posts()) : the_post();
the_content();
endwhile; endif;
>
最简单的方法,获取分类id
<php
// 将以下cat_ID改成你的分类ID即可
echo get_category(cat_ID)->count;
>
函数the_category 或者 get_the_category_list 就可以在循环内获取所属分类。具体位置位于:
/wp-includes/ -> category-templatephp
如果你想按照自己的想法输出内容,还可以借鉴默认模板,如twentyfifteen中的做法,示例:
$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma', 'twentyfifteen' ) );if ( $categories_list && twentyfifteen_categorized_blog() ) {
printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
_x( 'Categories', 'Used before category names', 'twentyfifteen' ),
$categories_list
);
}
第一步,将下面的代码添加到主题functions模板文件中:
function setTitle(){
$term = get_term_by('slug',get_query_var('term'),get_query_var('taxonomy'));
echo $title = $term->name;
}
第二步,用:
<php setTitle(); >
替换主题头部模板header类似这句:
<php wp_title('',true); >
(美设之家提供)
方法有很多,介绍一个方法给你
首先将如下代码放到主题文件functionsphp中:
function get_current_category_id() {
$current_category = single_cat_title('', false);
//获得当前分类目录名称
return get_cat_ID($current_category);
//获得当前分类目录ID
}
function get_current_tag_id() {
$current_tag = single_tag_title('', false);
//获得当前TAG标签名称
$tags = get_tags();
//获得所有TAG标签信息的数组
foreach($tags as $tag) {
if($tag->name == $current_tag) return $tag->term_id;
//获得当前TAG标签ID,其中term_id就是tag ID
}
}
下面这一步是为了获得指定分类目录ID或指定TAG标签ID,之后就可以在需要的地方使用如下代码来调用functions文件里的函数就可以实现想要的功能了:
<php
if (is_category())
{
get_most_viewed_category(get_current_category_id());
}
elseif (is_tag())
{
get_most_viewed_tag(get_current_tag_id());
}
else {
get_most_viewed();
}
>
补充说明:按照以上的方法来进行推理,只要将上述代码中的most替换为least即可实现冷门文章的调用。
热门,冷门文章都可以中这个方法来实现,看你怎么 *** 作了。
不懂的可以百度‘Pladys’,第一个即为我的博客,欢迎给位新手前来提问
你好:
wordpress如何在首页对每个分类进行 *** 作?我们可以用”get_categories()”函数,它返回与查询参数匹配的类别objects的数组
下面的代码就是得到所有的分类,并显示出每个分类下最新的10篇文章:
<php
// 得到所有分类列表 $categories = get_categories();
// 循环所有分类 foreach ($categories as $cat) {
// 得到分类ID $catid = $cat->cat_ID;
// 得到分类下10篇最新文章 query_posts(“showposts=10&cat=$catid”); >
<!– 输出分类标题及链接 –> <h4><a href=”<php echo get_category_link($catid);>” title=”<php echo strip_tags(category_description($catid)); >”> <php single_cat_title(); > </a></h4>
<!– 输出10篇最新文章的标题及链接 –> <ul> <php while (have_posts()) : the_post(); > <li> <a href=”<php the_permalink() >” title=”<php the_title(); >”> <php the_title(); ></a> <php the_time(‘m-d’); > </li> <php endwhile; > </ul> <php } >
以上就是关于如何让WordPress一个页面显示全部分类的文章全部的内容,包括:如何让WordPress一个页面显示全部分类的文章、WordPress新建页面怎么调用分类文章、wordpress怎么获取文章所属分类的名称等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)