在Wordpress菜单中创建动态站点地址 [英] Create a Dynamic Site Address in a Wordpress Menu

查看:178
本文介绍了在Wordpress菜单中创建动态站点地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基于WordPress主题的现有网站上做了一些工作,但使用了大约15个插件(包括Buddypress)。 WP滑动登录|仪表板插件特别是具有指向用户活动Feed的链接。我发现在wp-sliding-login-dashboard.php文件中创建该链接的代码:

 <?php 
if(is_user_logged_in()){
global $ current_user;
$ username = $ current_user-> user_login;
echo'< li>< a href =http://www.mywebsitename.com/members/'。$ username。'/ activity />活动Feed< / a>< / li> ;';
}
?>

我想使用此代码将用户发送到同一位置,但使用一个链接在主页的顶部。不幸的是,主页链接都是使用Wordpress菜单创建的,据我所知,只有允许使用附加到现有页面的静态链接。



我创建一个虚拟页面链接到那个退出只执行上面的代码?甚至有可能吗图片一个五岁的老人想读莎士比亚,你知道我作为一个编码人的能力,所以我可以自由地让我这样做 - 即如果你说哦,只是创建一个范围的功能,而不是创建一个全球功能,我会盯着你流口水和困惑。



图片清晰:滑动登录菜单(WP-Sliding Login | Dashboard Plugin),显示目标网址状态栏如www.ferrignofit.com/members/FerrignoFit/activity/(当前登录的用户是FerrignoFit):
http://i.imgur.com/NPvmCXU.jpg



主页基于Wordpress的菜单,我想去到上面的URL,但目前正在www.ferrignofit.com/activity/,另一个页面:
http://i.imgur.com/dIiFpDC.jpg

解决方案

所以这里是一个jQuery解决方案这个具体问题。从您看到的图像,您想要做的是在动态WordPress菜单中定位一个特定的锚点。



如您所知,您可以创建自定义链接WordPress菜单功能...它只是让您设置一个标签和一个URL。你应该创建这样的项目,并给它一个哈希的URL现在。



现在为这个特定的菜单项设置一个类,所以你可以有一个很好的处理jQuery (否则您可以使用WordPress为每个特定菜单项创建的动态类)。



您有一个类设置或您知道您需要什么目标,那么您可以在菜单之前添加此代码块。

 <?php if(is_user_logged_in()){?> ; 
< script>
$(document).ready(function(e){
var targetNav = $('li.customClassName a');
var userName ='<?php $ current_user = wp_get_current_user ); echo $ current_user-> display_name;?>';
var userUrl ='http://www.mywebsitename.com/members/'+ userName +'/ activity /';
targetNav .attr('href',userUrl);
});
< / script>
<?php} else {?>
< script>
$(document).ready(function(e){
var targetNav = $('li.customClassName a');
targetNav.attr('href','http:// www.stackoverflow.com');
});
< / script>
<?php}?>

请不要以为我在使用PHP获取当前用户名在WordPress,我将其存储在 userName 变量中,我在 userUrl 中使用它来设置我想要的路径。 / p>

另一方面,我使用 is_user_logged_in(),所以你可以选择使链接另外实际上用户没有登录,所以用这个如果语句,两个代码块之一将由PHP输出。



一旦我设置了两个变量,我只需使用jQuery定位我的菜单项,然后修改 href 属性来替换散列与路径和动态用户名



尽管混合JS和PHP并不是很好的做法,我想在这里学习一些其他解决方案的人可以建议,所以我发布这个一个解决您的非常具体问题的解决方案。



我用自己的WordPress网站测试了这个代码nd它的工作,只要记住,这个代码需要在一个PHP文件中,否则PHP中使用的< script> 标签不会意味着任何服务器,如果它是在 .js 文件中。


I'm doing some work on an existing site that is based on the Wordpress theme, but uses about 15 plugins (Including Buddypress). One in particular is the WP Sliding Login|Dashboard plugin, which has a link to the user's activity feed. I found the code that creates that link in the wp-sliding-login-dashboard.php file:

  <?php
                if ( is_user_logged_in() ) {
                global $current_user;
                $username = $current_user->user_login;
                echo ' <li><a href="http://www.mywebsitename.com/members/' . $username . '/activity/">Activity Feed</a></li>';
                    }
                    ?>

I want to use this code to send the user to the same location, but using the a link at the top of the home page. Unfortunately, the home page links are all created using Wordpress menus, which as far as I can tell, only allow for the use of static links attached to existing pages.

Do I create a dummy page to link to that exits only to execute the above code? Is that even possible? Picture a five-year-old trying to read Shakespeare, and you have an idea of my ability as a coder, so feel free to engage me as such - i.e. if you say, "oh just create a scoping function instead of creating a global function", i would stare at you drooling and confused.

Images for clarity: The sliding login menu (WP-Sliding Login|Dashboard Plugin), showing the target URL in the status bar as www.ferrignofit.com/members/FerrignoFit/activity/ (the current logged in user is FerrignoFit): http://i.imgur.com/NPvmCXU.jpg

The main page Wordpress-based menu, which i want to go to the above URL, but is currently going to www.ferrignofit.com/activity/, a different page: http://i.imgur.com/dIiFpDC.jpg

解决方案

So here's a jQuery solution for this specific issue. From seeing the images you have, what you want to do is target a specific anchor in your dynamic WordPress menu.

As you may be aware, you can create custom links for the WordPress menu function... it simply lets you set a label and a URL. You should create such item and just give it a hash for the URL for now.

Now set a class for that specific menu item so you can have a nice handle for jQuery to target it (otherwise you can use the dynamic class that WordPress creates for each specific menu-item).

One you have a class set or you know what you need to target then you can add this block of code before your menu.

<?php if (is_user_logged_in()){ ?>
  <script>
    $(document).ready(function(e) {    
      var targetNav = $('li.customClassName a');
      var userName = '<?php $current_user = wp_get_current_user(); echo $current_user->display_name;?>';
      var userUrl = 'http://www.mywebsitename.com/members/'+ userName +'/activity/';
      targetNav.attr('href',userUrl);
    });
  </script>
<?php } else { ?>
  <script>
    $(document).ready(function(e) {    
      var targetNav = $('li.customClassName a');
      targetNav.attr('href','http://www.stackoverflow.com');
    });
  </script>
<?php } ?>

Please not that I am using PHP to get the current username in WordPress, after I get the username and I store it in the userName variable I use it in the userUrl to set it with the path that I want.

On another note, I'm using is_user_logged_in() so you have the option of making the link something else if the user is in fact not logged in. So with this if statement one of the two blocks of code will be output by PHP.

As soon as my two variables are set, I simply target my menu item with jQuery and modify the href attribute to replace the hash with the path and dynamic username.

Though this is not very good practice to mix JS and PHP, I'd like to learn myself here what other solutions someone can suggest so I'm posting this as a solution for your very specific issue.

I tested this code with my own WordPress site and it works, just remember that this code NEEDS to be in a PHP file otherwise the PHP used in the <script> tags won't mean anything to the server if it's in a .js file.

这篇关于在Wordpress菜单中创建动态站点地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆