简体   繁体   English

如何将字体真棒图标添加到Joomla菜单

[英]How to add Font Awesome Icons to Joomla Menu

This is a simple method to add font awesome icons to joomla menu. 这是一种将真棒字体图标添加到joomla菜单的简单方法。

Open modules/mod_menu/tmpl/default.php 打开模块/mod_menu/tmpl/default.php

Find

foreach ($list as $i => &$item) :

After the foreach add this function. 在foreach之后添加此功能。

if(strpos($item->title,"fa-") !== false){
    $titleArray = explode("fa-",$item->title);        
    $item->title = "";
    for($i=0;$i<count($titleArray);$i++){
        if($i){
            $item->title .= "<i class=\"fa fa-".$titleArray[$i]."\"></i>";
        }else{
            $item->title .= $titleArray[$i];
        }
    }       
}

When you go to joomla menu manager put font awesome class after the menu title. 当您转到joomla菜单管理器时,在菜单标题后放置字体很棒的类。

Like this. 像这样。

Home fa-coffee 家用咖啡

Try Below: 尝试以下:

      if(strpos($item->title,"|") !== false){
                $titleArray = explode("|",$item->title);        
                $item->title = "";
                for($i=0;$i<count($titleArray);$i++){
                    if($i){
                        $item->title .= html_entity_decode("<i class=\"fa ".$titleArray[$i]."></i>");
                    }else{
                        $item->title .= $titleArray[$i];
                    }
                }       
            }

Had a similar problem on a website I'm working on at the moment. 我目前正在处理的网站上出现了类似的问题。 Joomla 3 has the ability to add a 'link css style' which if filled in with a font awsome style (ie 'fa fa-home') will add the font awsome class to the 'a' tag It works but it does not follow the FA guidelines of putting in in a 'i' tag. Joomla 3具有添加“链接CSS样式”的功能,如果使用字体令人敬畏的样式(即“ fa fa-home”)填充,则会将字体可怕的类添加到“ a”标签中。放入“ i”标签的FA准则。 If you want to do it property in joomla 3, here is my solution. 如果要在joomla 3中执行此操作,这是我的解决方案。
First we need to create a module override for the menu 首先,我们需要为菜单创建一个模块替代
Go to your template directory if not exisits, create a new directory called HTML 如果不存在,请转到模板目录,创建一个名为HTML的新目录
inside HTML create a new directory mod_menu 在HTML内创建一个新目录mod_menu
copy the following files from the /modules/mod_menu/tmpl directory and paste them in the templates/yourtemplate/HTML/mod_menu directory:- 从/ modules / mod_menu / tmpl目录复制以下文件,并将其粘贴到templates / yourtemplate / HTML / mod_menu目录中:-
default.php default.php
default_url.php default_url.php
default_component.php default_component.php
Joomla only lets you override the default.php file so to get around this rename Joomla只允许您覆盖default.php文件,以便绕开此重命名
default_url.php and default_component.php default_url.php和default_component.php
to
default_url2.php and default_component2.php default_url2.php和default_component2.php
(Make sure you are in the newly created module override directory as it's easy to edit the original and not the copy. (请确保您位于新创建的模块覆盖目录中,因为编辑原始文件而不是副本很容易。
open default.php 打开default.php
at or around line 75 在第75行或附近
change both instances of default_url to default_url2 将两个default_url实例都更改为default_url2
change default_component to default_component2 将default_component更改为default_component2

// Render the menu item.
switch ($item->type) :
    case 'separator':
    case 'url':
        require JModuleHelper::getLayoutPath('mod_menu', 'default_url2');
        break;
    case 'component':
        require JModuleHelper::getLayoutPath('mod_menu', 'default_component2');
        break;
    case 'heading':
        require JModuleHelper::getLayoutPath('mod_menu', 'default_' . $item->type);
        break;

    default:
        require JModuleHelper::getLayoutPath('mod_menu', 'default_url2');
        break;
endswitch;

Now we have control over default, default_url2 and default_component2 现在我们可以控制default,default_url2和default_component2

Open default_component2.php Basically we need to see whether there is a font awsome style in the 'link css style' If there is, save the style name in a variable then delete it from the link css style. 打开default_component2.php基本上,我们需要查看“链接CSS样式”中是否存在字体太酷的样式,如果存在,请将样式名称保存在变量中,然后从链接CSS样式中删除它。 Then we can add the 'i' tag with the saved font awsome style. 然后,我们可以添加带有已保存字体惊人样式的'i'标签。 here is the code at or around line 13 replace 这是第13行或附近的代码替换

$class = $item->anchor_css ? 'class="' . $item->anchor_css . '" ' : '';

with

$anchor_css = $item->anchor_css;
if (preg_match_all("/(fa-[-a-z0-9]+)/", $anchor_css, $fa)){ //look for a font awsome class name beginning with fa- if found the lass name is stored in $out[0][0]
    $anchor_css = preg_replace("/(fa-[-a-z0-9]+)/", "", $anchor_css);   //remove the font awsome class name starting with fa-
    $anchor_css = preg_replace("/fa/", "", $anchor_css);    //remove the fa prefix from the class
    $anchor_css = preg_replace('!\s+!', ' ', $anchor_css);  //remove multiple spaces and replace with a single space
    $anchor_css = trim($anchor_css);    //remove leading/trailing spaces
}
$fa_icon = $fa[0][0] ? '<i class="fa '.implode(' ',$fa[0]).'" ></i>' :'';   //if a font awsome class name exists, create an <i></i> tag
$class = $anchor_css ? 'class="' . $anchor_css . '" ' : '';

Now we need to add the icon into the code at or around line 34 add echo $fa_icon; 现在,我们需要在第34行或周围的代码中将图标添加到代码中。 to where you want the font awsome icon to appear. 到您希望字体可怕图标出现的位置。 I want mine before the 'a' tag so here is my new switch 我要在'a'标签之前添加我的标签,所以这是我的新开关

switch ($item->browserNav)
{
    default:
    case 0:
        echo $fa_icon;
        ?>
        <a <?php echo $class; ?>href="<?php echo $item->flink; ?>" <?php echo $title; ?>><?php echo $linktype; ?></a>
        <?php
        break;
    case 1:
        // _blank
        echo $fa_icon;
        ?>
        <a <?php echo $class; ?>href="<?php echo $item->flink; ?>" target="_blank" <?php echo $title; ?>><?php echo $linktype; ?></a>
        <?php
        break;
    case 2:
    // Use JavaScript "window.open"
        echo $fa_icon;
        ?>
        <a <?php echo $class; ?>href="<?php echo $item->flink; ?>" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');return false;" <?php echo $title; ?>><?php echo $linktype; ?></a>
        <?php
        break;
}

if you want the font awsome icon to be within the 'a' tag just simply move the 如果您希望字体的可怕图标位于'a'标签内,只需将

echo $fa_icon;

ie

switch ($item->browserNav)
{
    default:
    case 0:
        ?>
        <a <?php echo $class; ?>href="<?php echo $item->flink; ?>" <?php echo $fa_icon . $title; ?>><?php echo $linktype; ?></a>
        <?php
        break;
    case 1:
        // _blank
        ?>
        <a <?php echo $class; ?>href="<?php echo $item->flink; ?>" target="_blank" <?php echo $fa_icon . $title; ?>><?php echo $linktype; ?></a>
        <?php
        break;
    case 2:
    // Use JavaScript "window.open"
        ?>
        <a <?php echo $class; ?>href="<?php echo $item->flink; ?>" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes');return false;" <?php echo $fa_icon . $title; ?>><?php echo $linktype; ?></a>
        <?php
        break;
}

Repeat the above for default_url2.php the first part of the code is exactly the same however the switch in the default_url is different to the default_component 对default_url2.php重复上述步骤,代码的第一部分完全相同,但是default_url中的开关与default_component不同

switch ($item->browserNav) :
    default:
    case 0:
        echo $fa_icon;
        ?>
        <a <?php echo $class; ?>href="<?php echo $flink; ?>" <?php echo $title; ?>><?php echo $linktype; ?></a><?php
        break;
    case 1:
        // _blank
        echo $fa_icon;
        ?>
        <a <?php echo $class; ?>href="<?php echo $flink; ?>" target="_blank" <?php echo $title; ?>><?php echo $linktype; ?></a><?php
        break;
    case 2:
        // Use JavaScript "window.open"
        $options = 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,' . $params->get('window_open');
        echo $fa_icon;
        ?>
        <a <?php echo $class; ?>href="<?php echo $flink; ?>" onclick="window.open(this.href,'targetWindow','<?php echo $options;?>');return false;" <?php echo $title; ?>><?php echo $linktype; ?></a><?php
        break;
endswitch;

Save all your files and that's it To add a font icon to your menu item just go to the joomla menu manager, open up a menu item, click on the 'Link Type' tag and add your fa style to the link css style field in the form of 'fa fa-home' Click save, open the fron end, refresh and BINGO! 保存所有文件,仅此即可向菜单项添加字体图标,只需转到joomla菜单管理器,打开一个菜单项,单击“链接类型”标签,然后将fa样式添加到“链接样式”字段中。 “ fa fa-home”的形式单击“保存”,打开前端,刷新并添加BINGO!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM