定制single.php创建动态about us 页面

1)在wp-admin中,创建 about 类别,创建需要显示的关于的介绍文章

2) 查看需要首先显示的文章,复制网址

3)在定制菜单中,添加关于菜单和网址

4)复制single.php到single-about.php,并定制此页面。

5)在function.php中添加下列代码:

define(SINGLE_PATH,get_stylesheet_directory());

/**
* Filter the single_template with our custom function
*/
add_filter('single_template', 'my_single_template');
 
/**
* Single template function which will choose our template
*/
function my_single_template($single) {
global $wp_query, $post;
 
/**
* Checks for single template by category
* Check by category slug and ID
*/
foreach((array)get_the_category() as $cat) :
 
if(file_exists(SINGLE_PATH . '/single-' . $cat->slug . '.php'))
    return SINGLE_PATH . '/single-' . $cat->slug . '.php';
 
elseif(file_exists(SINGLE_PATH . '/single-' . $cat->term_id . '.php'))
   return SINGLE_PATH . '/single-' . $cat->term_id . '.php';
else
return SINGLE_PATH.'/single.php';
endforeach;

}


这是一种建立动态"关于“或“企业介绍“的参考思路。想了好几天,肯定有更好的方法,只是接触wordpress时间不长,

不知道罢了。






猜你喜欢

转载自blog.csdn.net/xtjie/article/details/80223210