1.wordpress判断当前分类有没有子分类
wordpress获取当前分类下的子分类
1.将此函数放在你模版的函数中 (wordpress3.8.1是functions.php这个文件)
functionget_category_root_id($cat)
{
$this_category = get_category($cat); // 取得当前分类
while($this_category->category_parent) // 若当前分类有上级分类时,循环
{
$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)
}
return$this_category->term_id; // 返回根分类的id号
}
2.页面调用 在模板下sidebar.php里修改
?>
2.如何让wordpress的文章只显示在子分类而不显示在父分类
WordPress
先判断下是否登录,然后获取当前用户对象,然后获取当前用户对象的信息,需要哪些用哪些:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if(is_user_logged_in()){
$current_user = wp_get_current_user();
/**
* @example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
echo 'Username: ' . $current_user->user_login . '
';
echo 'User email: ' . $current_user->user_email . '
';
echo 'User first name: ' . $current_user->user_firstname . '
';
echo 'User last name: ' . $current_user->user_lastname . '
';
echo 'User display name: ' . $current_user->display_name . '
';
echo 'User ID: ' . $current_user->ID . '
';
}
3.WordPress 如何实现分类以及分类下的子分类,共用一个模板
创建独立ID或别名的样式文件:WordPress默认的分类是查找对应ID的主题文件,找不到就会指向archive.php文件,如果archive.php文件不存在,就会默认使用index.php文件。
到博客后台查看一下要设计样式的分类的ID,假设分类ID为7,之后把主题文件archive.php复制一下,把文件改名为:category-7.php,这样当你浏览分类ID为7的分类时,就会自动调用category-7.php主题文件,就实现了自定义显示分类为7的样式。创建的category-7.php或category-Internet.php的样式要区别于archive.php,要不然,就称不上“不同分类使用不同列表样式”了。
4.怎么判断wordpress 文章是否含有图片,并以此分类排文章
借助preg_match_all函数进行输出调试
<?php
$szPostContent = $post->post_content;
$szSearchPattern = '~<img [^\>]*\ />;~'; // 搜索所有符合的图片
preg_match_all( $szSearchPattern, $szPostContent, $aPics );
$iNumberOfPics = count($aPics[0]); // 检查一下至少有一张图片
?>
<?php if ( $iNumberOfPics > 0 ) { ?>
. 。后面不写了,仅供参考
希望对你有所帮助
5.wordpress 如何让父分类显示某一个子分类内容
首先,先通过is_category()来判断当前是不是分类页面,如果是获取分类ID;在得到分类ID后,根据你的设定选择显示哪个分类,这里就要求你先手动设置个对照关系,举例:$routers[18] = 20; 然后在这个$routers里遍历到要显示的ID. 有了这个ID后,向pre_get_posts添加自定义函数,就可以实现你的需求。
大概代码手写一下给你参考: if ( is_category() { $cid = get_queried_object_id(); if ( isset($routers[$cid]) && (int)$routers[$cid] ){ add_action('pre_get_posts', 'show_posts_by_custom_category_ID'); } function show_posts_by_custom_category_ID( $query ) { $query->set( 'cat', $routers[$cid] ); return; }}。
6.如何让wordpress的文章只显示在子分类而不显示在父分类
1234567891011121314if(is_user_logged_in()){$current_user = wp_get_current_user(); /** * @example Safe usage: $current_user = wp_get_current_user(); * if ( !($current_user instanceof WP_User) ) * return; */ echo 'Username: ' . $current_user->user_login . ''; echo 'User email: ' . $current_user->user_email . ''; echo 'User first name: ' . $current_user->user_firstname . ''; echo 'User last name: ' . $current_user->user_lastname . ''; echo 'User display name: ' . $current_user->display_name . ''; echo 'User ID: ' . $current_user->ID . ''; }。
转载请注明出处51数据库 » wordpress判断是否是子分类