↓ 快轉到主要內容

Wordpress中修改自定义导航菜单标签输出格式

·509 字·2 分鐘
Y Cheung
作者
Y Cheung
Blogger, Programer & Traveler.

wordpress自3.0版本后增加了一个自定义导航菜单的功能,但是默认生成的菜单会被包含在<li></li>列表标签中,对于修改主题来说甚为不便。

从wp_nav_menu()中移除<li>标签,网上给出的解决方案是使用strip_tags()函数:

1    <?php
2    echo strip_tags(wp_nav_menu(
3            array('theme_location' => 'primary',
4                'container_class' => 'user_menu',
5                'echo' => false,
6                'items_wrap' => '%3$s<div class="clear"></div>')
7    ), '<a><div>');
8    ?>

Y.CHEUNG用的是官方给出的办法,用wp_get_nav_menu_items()函数自定义菜单的输出格式,代码如下:

 1    <?php 
 2       $menu_name = 'aboutnav';//菜单位置location,与functions.php文件中的参数一致
 3               if(($locations = get_nav_menu_locations())&&isset($locations[$menu_name])){
 4                   $menu = wp_get_nav_menu_object($locations[$menu_name]);
 5                   $menu_items = wp_get_nav_menu_items($menu->term_id);
 6                   
 7                   foreach ((array)$menu_items as $key => $menu_item){
 8                       $title = $menu_item -> title;
 9                       $url = $menu_item ->url;
10                       $menu_list .= '<tr><td width="26" height="33"><div align="right"><img src="'.get_template_directory_uri().'/images/about_09.jpg" width="9" height="8"></div></td>
11               <td width="203">&nbsp;<a href="'.$url.'" class="black14b">'.$title.'</a></td></tr>';//自定义菜单每条项目item输出的格式
12                   }
13               }else{
14                   $menu_list = '<tr><td width="26" height="33"><div align="right"><img src="'.get_template_directory_uri().'/images/about_09.jpg" width="9" height="8"></div></td>
15               <td width="203">Menu "' . $menu_name . '" not defined.</td></tr>';
16               }
17               echo $menu_list;
18               ?>

以上代码皆按需放置在template.php中相应导航菜单需要输出的位置。

相關文章

Wordpress在循环LOOP中获取 post_date 使用自定义日期时间格式

·146 字·1 分鐘
code示例: 1<?php $args = array('numberposts' => '16','category' => '8'); 2 $recent_posts = wp_get_recent_posts( $args, ARRAY_A ); 3 foreach ($recent_posts as $recent) { 4 echo '<li><a href="'. get_permalink($recent["ID"]).'">'.$recent["post_title"].'</a><span class="list-meta">'. mysql2date('Y-m-d', $recent["post_date"]) .'</span></li>'; 5 } 6?> 原本直接用$recent['post_date']输出的是2014-11-11 14:30:24这样的格式,但现在不需要时间的输出,Y.Cheung 只需要输出日期,找了一下用mysql2date函数就可以。

獲取最新文章並附特色圖片的代碼

·250 字·1 分鐘
幫朋友寫的,順便放在這裡mark一下好了。 以下代碼實現獲取最新10篇日誌及其100×100像素的特色圖片。 1<?php query_posts('posts_per_page=10'); if (have_posts()) : while (have_posts()) : the_post(); ?> 2 3<?php if (has_post_thumbnail()){the_post_thumbnail(array(100,100)); } ?> <?php the_title(); ?> 4 5<?php echo get_the_date(); ?> 6 7<?php endwhile; endif;?> COMMENTS (7) # 老武 · 2012-03-26T10:42:20+08:00

WordPress宕机一日

·616 字·2 分鐘
昨天突然无法访问我的blog了,还以为我又折腾什么出错了,却发现连服务器都进不去了。大件事啦! 联系了hugege(我在他那里租的主机),说是到国内的线路抽风喔。于是,我又定了定神,没事,等它抽完了就好了。果然,晚上的时候就恢复正常了,额,也不算太正常,时不时还是抽一下,不过,能访问就好了。(^U^)ノ~YO

WordPress 301重定向插件 Redirection

·1701 字·4 分鐘
朋友博客换了空间换了域名,但是又不想失去苦苦经营多年的旧博客的流量,于是想说那就设置301重定向吧。 首页和某些特定页面重定向到还好办,直接修改.htaccess就好了,就是那差不多上千篇的文章,如果要一篇篇手动指定是非常痛苦的。小雪一开始就想不然直接用python分别抓取新旧文章链接生成CSV文件再来做重定向好了,但上网搜索了一下发现很多人都推荐用Redirection插件来解决这个问题,于是,have a try咯~

Theme update - 20131126

·1281 字·3 分鐘
小雪在浏览网站的时候看到人家的文章页面的超链接特效很和谐很赞,就忍不住把自己博客的主题也给换上这个效果lol example:这是一个超链接 效果的实现很简单,就是把需要加特效的超链接文字加上一个 <span> 包住,然后在CSS中样式化。 example: