Products
GG网络技术分享 2025-03-18 16:12 0
在前面的章节中,我们已经完成了wordpress主题trans的前端页面的动态模板的创建,我们创建了公共模板:头部模板、侧边栏模板、底部模板,我们完成了首页模板、列表页模板、文章详情页模板。但是,有一些细节问题,我们还没有处理好,如:logo图片如何修改?底部的一些信息如何修改?网站的关键词与描述在哪添加?当然,这些东西,我们可以手动添加到模板代码中,但是,在后期操作中就比较麻烦,难道每次修改时,我们都要去修改模板代码吗?如果用户是一个不懂代码的人,该如何操作呢?这时,如果在trans主题的后台有一个界面可以设置这些东西就好了,那样,不管你懂不懂代码,都可以操作。所以,我们将带着在大家来给trans主题添加一个主题设置页面。
在trans主题的functions.php文件中添加如下代码:
//在后台“外观”菜单中添加 【主题设置】 这个子菜单function Themes_Set(){ add_theme_page( 'title标题', '主题设置', 'administrator', 'ashu_slug','ssmay_set'); }add_action('admin_menu', 'Themes_Set'); function ssmay_set(){ //主题设置函数 include("theme_set.php");//这里是我人创建的一个php文件,用来设置选项内容 }
这里我用到了wordpress的几个函数:
add_theme_page():给“外观”导航创建子菜单;参数解说:参数1-----标题的内容参数2-----显示在后台左边菜单的标题参数3-----访问这个页面需要的权限参数4-----别名,需要独一无二哦参数5-----执行的函数(我们自定义的函数)
add_action():这是一个添加勾子的函数,这里将“Themes_Set”函数添加到“admin_menu”后台菜单的勾子中。
这时,我们还不能测试,因为theme_set.php我们还没有创建,会报错的。
在主题文件夹里创建一个新文件——theme_set.php,也就是主题设置内容界面文件。在这个文件中添加如下测试代码:
<h2>我是trans主题设置</h2>
这时,我们打开后台的外观,可以看到一个“主题设置”的子菜单,点击“主题设置”,右侧内容栏会显示“我是trans主题设置”,就表示我们添加设置功能成功了,如下图:
接下来,我们就可以在后台“主题设置”的右侧内容栏中添加我们想要的设置表单内容了。在theme_set.php添加如下代码:
< style>.theme_set{ width:98%; }.theme_set h2{ font-size:20px; }.theme_set dl{ margin-top:20px; }.theme_set dd{ margin:5px 0; }.theme_set dd input[type=text]{ width:50%; }.theme_set dd textarea{ width:50%; }.theme_set dd img{ margin-bottom:-30px; }</style>
< div class="theme_set">< form action="" method="post" enctype="multipart/form-data">< h2>主题设置</h2> < dl>< dt>网站Logo:</dt>< dd> < input type="file" name="logo"> </dd></dl>< dl><dt>网站备案号:</dt><dd><input type="text" name="beian" value=""></dd></dl><dl>< dt>网站地图链接:</dt>< dd><input type="text" name="map" value=""> </dd></dl>< dl>< dt>网站关键词:</dt>< dd><input type="text" name="keywords" value=""></dd></dl>< dl>< dt>网站描述:</dt>< dd>< textarea name="description" ></textarea></dd></dl>< dl>< dt>分享代码:</dt>< dd>< textarea name="share" ></textarea></dd></dl>< dl>< dt>文章页广告代码:</dt>< dd>< textarea name="ad_single" ></textarea></dd></dl>< dl>< dt></dt>< dd><input type="submit" name="theme_set" value="提交"></dd></dl></form></div>
这时,主题设置界面的展示效果如下图,还只是一个静态界面,没有任何动态交互功能:
在theme_set.php页面的顶部添加表单提交后的处理代码,如下:
< ?phpif($_POST['theme_set']){ $attachment_id = media_handle_upload( 'logo', 0 ); //上传图片,返回的是 附件的ID $logo_url = wp_get_attachment_url($attachment_id); //获取 图片的地址if($logo_url){update_option("logo_img",$logo_url); //如果图片地址在在,就将图片的地址写入到数据库}update_option("beian",$_POST["beian"]); //更新数据表中的备案字段的值update_option("map",$_POST["map"]); update_option("keywords",$_POST["keywords"]);update_option("description",$_POST["description"]);update_option("share",stripslashes($_POST["share"]));update_option("ad_single",stripslashes($_POST["ad_single"]));
}
$logo_img = get_option("logo_img"); ?>
这里,我们又用到了wordpress的几个函数:
wp_get_attachment_url():获取附件的路径地址,参数是附件的ID。media_handle_upload():上传文件的函数,返回上传附件的ID。参数1:< input name="logo">的name值;参数2:文章ID,如果是0,表示不是文章。update_option():更新wp_options数据表中的选项。参数1:数据表中的字段名;参数2:字段的值。
完成了这一步,我们就可以在后台的主题设置界面上传logo图片和添加相关信息了。
通过上面的步骤,我们只可以添加数据到数据库,但在下一次进入到这个主题设置界面时,我们并不知道有没有添加数据,因为,表单仍然是空的,它没有从数据库中调用相应的数据,这样就不友好了。所以,这里,我们要让表单中显示我们已添加到数据库中的数据。把表单的部分代码修改成如下代码:
< dl>< dt>网站Logo:</dt>< dd> < input type="file" name="logo"> < img src="<?php echo $logo_img; ?>" height=50 ></dd></dl>< dl>< dt>网站备案号:</dt>< dd><input type="text" name="beian" value="<?php echo get_option("beian"); ?>"></dd></dl>< dl>< dt>网站地图链接:</dt>< dd>< input type="text" name="map" value="<?php echo get_option("map"); ?>"> </dd></dl>< dl>< dt>网站关键词:</dt>< dd>< input type="text" name="keywords" value="<?php echo get_option("keywords"); ?>"></dd></dl>< dl>< dt>网站描述:</dt>< dd>< textarea name="description" ><?php echo get_option("description"); ?></textarea></dd></dl>< dl>< dt>分享代码:</dt>< dd>< textarea name="share" ><?php echo get_option("share"); ?></textarea></dd></dl>< dl>< dt>文章页广告代码:</dt>< dd>< textarea name="ad_single" ><?php echo get_option("ad_single"); ?></textarea></dd></dl>
这样,我们在后期进入这个主题设置界面后,在表单中就会显示已添加的数据和已上传的图片了。如下图所示:
通过上面的五大步骤,我们成功地为wordpress主题trans的后台添加了主题设置功能。在下一节中,我们再来介绍,如何在前端的代码中调用这些设置页面所设置的数据。本节我们就介绍到这里,如果喜欢我的课程,别忘了帮我“点赞、评论、收藏”哦。谢谢参阅。
文章主要介绍了WordPress开发中wp_title()函数的用法,wp_title可以用来显示文章标题和分类名称等,需要的朋友可以参考下 。
wp_title 函数在 WordPress 中是用来显示文章、页面、分类等等等等标题的一个函数,但在首页索引,该函数将不显示任何的东西。该函数在 WordPress 官方主题中一直被使用,但目前很多定制的主题中这个函数总是为忽视。
函数意义详解
wp_title 函数用来显示页面的标题,如在文章页面,则显示文章标题;在分类页面,则显示分类名称,但在首页索引,该函数将不显示任何的东西。有点像 WordPress 中的 get_the_title 和 single_cat_title()这两个函数的自适应用法(自动判断是页面、文章还是分类、归档、标签)。
函数声明
有点长,希望您能耐心看一遍,哪怕只有那么一遍。
/*** Display or retrieve page title for all areas of blog.
*
* By default, the page title will display the separator before the page title,
* so that the blog title will be before the page title. This is not good for
* title display, since the blog title shows up on most tabs and not what is
* important, which is the page that the user is looking at.
*
* There are also SEO benefits to having the blog title after or to the \'right\'
* or the page title. However, it is mostly common sense to have the blog title
* to the right with most browsers supporting tabs. You can achieve this by
* using the seplocation parameter and setting the value to \'right\'. This change
* was introduced around 2.5.0, in case backwards compatibility of themes is
* important.
*
* @since 1.0.0
*
* @param string $sep Optional, default is \'»\'. How to separate the various items within the page title.
* @param bool $display Optional, default is true. Whether to display or retrieve title.
* @param string $seplocation Optional. Direction to display title, \'right\'.
* @return string|null String on retrieve, null when displaying.
*/
function wp_title($sep = \'»\', $display = true, $seplocation = \'\') {
global $wpdb, $wp_locale;
$m = get_query_var(\'m\');
$year = get_query_var(\'year\');
$monthnum = get_query_var(\'monthnum\');
$day = get_query_var(\'day\');
$search = get_query_var(\'s\');
$title = \'\';
$t_sep = \'%WP_TITILE_SEP%\'; // Temporary separator, for accurate flipping, if necessary
// If there is a post
if ( is_single() || ( is_home() && !is_front_page() ) || ( is_page() && !is_front_page() ) ) {
$title = single_post_title( \'\', false );
}
// If there\'s a category or tag
if ( is_category() || is_tag() ) {
$title = single_term_title( \'\', false );
}
// If there\'s a taxonomy
if ( is_tax() ) {
$term = get_queried_object();
$tax = get_taxonomy( $term->taxonomy );
$title = single_term_title( $tax->labels->name . $t_sep, false );
}
// If there\'s an author
if ( is_author() ) {
$author = get_queried_object();
$title = $author->display_name;
}
// If there\'s a post type archive
if ( is_post_type_archive() )
$title = post_type_archive_title( \'\', false );
// If there\'s a month
if ( is_archive() && !empty($m) ) {
$my_year = substr($m, 0, 4);
$my_month = $wp_locale->get_month(substr($m, 4, 2));
$my_day = intval(substr($m, 6, 2));
$title = $my_year . ( $my_month ? $t_sep . $my_month : \'\' ) . ( $my_day ? $t_sep . $my_day : \'\' );
}
// If there\'s a year
if ( is_archive() && !empty($year) ) {
$title = $year;
if ( !empty($monthnum) )
$title .= $t_sep . $wp_locale->get_month($monthnum);
if ( !empty($day) )
$title .= $t_sep . zeroise($day, 2);
}
// If it\'s a search
if ( is_search() ) {
/* translators: 1: separator, 2: search phrase */
$title = sprintf(__(\'Search Results %1$s %2$s\'), $t_sep, strip_tags($search));
}
// If it\'s a 404 page
if ( is_404() ) {
$title = __(\'Page not found\');
}
$prefix = \'\';
if ( !empty($title) )
$prefix = \" $sep \";
// Determines position of the separator and direction of the breadcrumb
if ( \'right\' == $seplocation ) { // sep on right, so reverse the order
$title_array = explode( $t_sep, $title );
$title_array = array_reverse( $title_array );
$title = implode( \" $sep \", $title_array ) . $prefix;
} else {
$title_array = explode( $t_sep, $title );
$title = $prefix . implode( \" $sep \", $title_array );
}
$title = apply_filters(\'wp_title\', $title, $sep, $seplocation);
// Send it out
if ( $display )
echo $title;
else
return $title;
}
用法
<?php wp_title( $sep, $echo, $seplocation ); ?>
参数详解
总结
WordPress 中相同功能的函数有很多,都是从基层到高级不断的经过封装最后到达使用层的,当然如果我们需要一些灵活用法的话,我们可以直接用中间那层的函数,如果我们懒的话我们可以直接使用最高级的那层函数,诸如本函数 wp_title ,其实这个函数我们从源代码来看, wp 替我们针对 分类、标签、文章、归档、作者、页面等多种类型的页面进行了判断,并根据不同页面调用不同的标题函数来达到目的。
如果有时间,您可以对下面几个函数进行深入研究一下,一遍更灵活的进行seo:
Demand feedback