【问题标题】:Adding custom code for social icons to a Wordpress primary menu将社交图标的自定义代码添加到 Wordpress 主菜单
【发布时间】:2020-07-23 13:06:01
【问题描述】:

你好 StackOverflow 社区!

我的问题有点棘手,但我们在这里:

  • 目标:在 wordpress(Divi 主题)的主菜单中添加自定义代码以显示社交图标
  • 预期结果:

为什么是主菜单?因为我希望社交图标以响应模式保留在汉堡菜单中,并且不要留在汉堡菜单之外。

到目前为止,我编写了我要查找的内容,但是...我不知道如何将其添加到 wordpress。我的意思是:我很好地为我的菜单打开了一个自定义链接窗口,包括 url、导航标签和 css 类,但是......我不知道如何让它工作。我应该使用简码还是什么?

提前,非常感谢!

代码:

<script type="text/javascript">
            (function($){
            var isMenuActive = false;

            var timeout;

            $('.navbar .menu > ul > li').on('mouseover', function(e){

                var $this = $(this).find('.sub-menu');


                if ($this.length==0 && $(this).is('li')) {
                    var $this = $('.navbar .sub-menu.active');
                    $this.trigger('mouseleave');
                    return;
                }

                $this = $this.add('.sub-menu-shadow');

                if (!$this.hasClass('appear')) {
                    
                    $this.addClass('appear');
                    
                    timeout = setTimeout(function() {
                        $this.addClass('active').removeClass('appear');
                    }, 500);
                }

            });
            $('.sub-menu').on('mouseleave', function(e){

                console.log('leave')

                clearTimeout(timeout);

                var $this = $(this);

                $this = $this.add('.sub-menu-shadow');

                $this.addClass('disapear')

                timeout = setTimeout(function(){
                    $this.removeClass('active').removeClass('disapear').removeClass('appear');
                },500);
                
            });

        })(jQuery);
</script> 
<style>
ul,
ol {
    list-style: none
}

p,
ul,
ol {
    margin: 0;
    padding: 0
}
.top-navigation .fb {
    background-image: url(facebook-gray.svg)
}

.top-navigation .fb:hover {
    background-image: url(facebook-black.svg)
}

.top-navigation .ig {
    background-image: url(instagram-gray.svg)
}

.top-navigation .ig:hover {
    background-image: url(instagram-black.svg)
}
.top-navigation .in {
    background-image: url(linkedin-gray.svg)
}

.top-navigation .in:hover {
    background-image: url(linkedin-black.svg)
}

.top-navigation .pi {
    background-image: url(Pinterest-gray.svg)
}

.top-navigation .pi:hover {
    background-image: url(Pinterest-black.svg)
}

.navbar {
    position: relative;
    width: 100%;
    padding: 24px 0;
    z-index: 2;
    display: none;
    font-size: 2em;
    line-height: 1.2;
    z-index: 9;
    color: #898989
}

.navbar .menu>ul>li,
.navbar__menu>ul>li {
    margin: 0 0 16px;
    display: block
}

.navbar .menu>ul>li>a,
.navbar__menu>ul>li>a {
    position: relative
}

.navbar .menu>ul>li>a:after,
.navbar__menu>ul>li>a:after {
    transition: .3s height;
    -webkit-transition: .3s height;
    -moz-transition: .3s height;
    -ms-transition: .3s height;
    -o-transition: .3s height;
    content: "";
    position: absolute;
    left: 0;
    bottom: -50px;
    width: 100%;
    background-color: #f0c796;
    height: 0
}

@media screen and (min-width:1024px) {
    .navbar .menu>ul>li>a:not(.appear-not):hover:after,
    .navbar__menu>ul>li>a:not(.appear-not):hover:after {
        height: 7px
    }
}

@media screen and (min-width:1024px) {
    .navbar .menu>ul>li.current-menu-item>a:not(.appear-not):after,
    .navbar .menu>ul>li:hover>a:not(.appear-not):after,
    .navbar__menu>ul>li.current-menu-item>a:not(.appear-not):after,
    .navbar__menu>ul>li:hover>a:not(.appear-not):after {
        height: 7px
    }
}

.navbar .sub-menu {
    font-size: 22px
}

.navbar .sub-menu>li>a>div {
    display: none
}

.navbar .sub-menu .img--center:after {
    padding-bottom: 53%
}

.navbar .sub-menu .img--center>img {
    width: 105%;
    -webkit-transition: max-width .2s;
    transition: max-width .2s
}

.navbar .sub-menu .img--center>img:hover {
    max-width: 105%
}

@media screen and (min-width:1024px) {
    .navbar {
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        display: grid;
        display: -ms-grid;
        grid-template-columns: 1fr auto 1fr;
        -ms-grid-columns: 1fr auto 1fr;
        font-size: .8em;
        padding: 0
    }
    .navbar>* {
        -ms-flex-item-align: center;
        align-self: center
    }
    .navbar .menu,
    .navbar .logo {
        vertical-align: middle
    }
    .navbar .menu:nth-child(2),
    .navbar .logo:nth-child(2) {
        text-align: center
    }
    .navbar .menu:last-child,
    .navbar .logo:last-child {
        text-align: right
    }
    .navbar .menu>ul>li,
    .navbar__menu>ul>li {
        display: inline-block;
        margin: 0 6px
    }
    .navbar .sub-menu {
        position: absolute;
        left: -24px;
        right: -24px;
        top: 85px;
        background: #fef8f0;
        padding: 0;
        display: none;
        opacity: 0;
        height: 265px;
        transition: opacity .5s;
        -webkit-transition: opacity .5s;
        -moz-transition: opacity .5s;
        -ms-transition: opacity .5s;
        -o-transition: opacity .5s
    }
    .navbar .sub-menu>li {
        display: inline-block;
        margin-left: 32px;
        margin-bottom: 26px;
        text-align: center
    }
    .navbar .sub-menu>li>a>div {
        width: 262px;
        padding-top: 32px;
        margin: 0 auto 24px;
        display: block
    }
    .navbar .sub-menu>li>a {
        font-size: 34px;
        line-height: 42px;
        color: #000;
        font-family: Timeout B, Caveat
    }
    .navbar .sub-menu.active {
        display: block;
        opacity: 1
    }
    .navbar .sub-menu.disapear {
        opacity: 0
    }
    .navbar .sub-menu.appear {
        display: block
    }
}

@media screen and (min-width:1024px) {
    .navbar .menu>ul>li,
    .navbar__menu>ul>li {
        margin: 0 12px
    }
}
.nav-active {
    overflow: hidden
}

@media screen and (min-width:1024px) {
    .nav-active {
        overflow: auto
    }
}

.nav-active .top-navigation {
    position: fixed;
    top: 0;
    left: 80px;
    right: 0;
    bottom: 0;
    height: 100%;
    width: calc(100% - 80px);
    background-color: #fff
}

.nav-active .top-navigation:after {
    content: "";
    z-index: -1;
    background: rgba(0, 0, 0, .35);
    position: absolute;
    left: -80px;
    width: 80px;
    top: 0;
    height: 100%
}

@media screen and (min-width:1024px) {
    .nav-active .top-navigation {
        position: relative;
        left: 0;
        width: 100%
    }
    .nav-active .top-navigation:after {
        content: none
    }
}

.nav-active .navbar {
    display: block;
    margin-top: 0;
    text-align: center
}

@media screen and (min-width:1024px) {
    .nav-active .navbar {
        text-align: left;
        height: auto;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex
    }
}

@media screen and (max-width:1023px) {
    .nav-active .navbar .menu>ul>li {
        border-bottom: 1px solid
    }
    .nav-active .navbar .menu>.social-icons--header>li {
        border: none;
        padding: 0
    }
    .nav-active .navbar .menu>.social-icons--header {
        margin-top: 12px
    }
    .nav-active .navbar .inline-block-menu {
        display: block
    }
    .nav-active .navbar ul>li {
        text-align: left;
        font-size: 12px;
        line-height: 16px;
        margin-left: -24px;
        margin-right: -24px;
        margin-bottom: 0
    }
    .nav-active .navbar ul>li>a {
        padding: 8px 12px;
        display: block
    }
    .nav-active .navbar .sub-menu>li>a {
        padding-left: 24px
    }
}
.social-icons,
.social-icons--header{
    display: block;
    vertical-align: middle
}

@media screen and (min-width:1024px) {
    .social-icons,
    .social-icons--header{
        display: inline-block;
        position: relative
    }
}

.social-icons>ul>li,
.social-icons--header>ul>li {
    width: 0
}

.social-icons li a,
.social-icons--header li a,
.social-icons li,
.social-icons--header li {
    display: inline-block
}

.social-icons li a,
.social-icons--header li a {
    width: 23px;
    height: 23px;
    margin-right: 21px;
    background-repeat: no-repeat;
    background-size: 100%;
    background-position: center
}

.social-icons--header{
    margin-left: 10px
}

.social-icons--header li a {
    width: 21px;
    height: 21px;
    margin-right: 14px;
    margin-top: 4px
}

.social-icons--header ul>li>a {
    padding: 0 !important
}

@media screen and (min-width:1024px) {
    .social-icons--header {
        width: 28px;
        height: 30px
    }
    .social-icons--header ul>li
    .social-icons--header li a {
        display: block;
        margin-left: auto;
        margin-right: auto
    }
    .social-icons--header ul>li {
        width: 26px
    }
    .social-icons--header li a {
        margin-top: 3px;
        max-width: 16px;
        background-position: center
    }
    .social-icons--header>ul {
        background-size: 100%;
        position: absolute;
        top: 0;
        width: 100%;
        height: 128px;
        max-height: 30px;
        overflow: hidden;
        -webkit-transition: .3s max-height;
        transition: .3s max-height
    }
    .social-icons--header>ul:hover {
        max-height: 128px;
        background-image: none;
        border-color: silver
    }
}
.navbar>.menu>ul.social-icons li,
.navbar>.menu>ul.social-icons--header li {
    margin: 0;
    display: inline-block
}
</style>
<div id="header-nav" class="top-navigation">
    <div class="navbar">
        <div class="social-icons--header">
            <ul>
                <li> 
                    <a href="https://www.facebook.com" target="_blank" class="fb appear-not"></a>
                </li>
                <li> 
                    <a href="https://www.instagram.com/" target="_blank" class="ig appear-not"></a>
                </li>
                <li> 
                    <a href="https://www.linkedin.com/" target="_blank" class="in appear-not"></a>
                </li>
                <li> 
                    <a href="https://www.pinterest.fr/" target="_blank" class="pi appear-not"></a>
                </li>
            </ul>
        </div>
    </div>
</div>

【问题讨论】:

    标签: jquery css wordpress icons


    【解决方案1】:

    您可以在管理菜单中创建自定义链接并添加他们的类,如下所示:

    你可以添加css:

    .fb {
        background-image: url('image_link');
        color: transparent;
        height: 50px;
        width: 50px;
        display: inline-block;
        background-position: center;
        background-size: cover;
    }
    

    或者你可以像这样在链接文本中添加html标签(img或div宽度类名:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2022-11-12
      • 1970-01-01
      相关资源
      最近更新 更多