在WordPress的菜单中选择创建一个动态的网站地址(Create a Dynamic Site

2019-09-26 20:20发布

我做的是基于WordPress的主题现有站点的一些工作,但使用约15的插件(包括BuddyPress的)。 其中特别是WP滑动登录|仪表板插件,它有一个链接到用户的活动饲料。 我发现,会在WP-滑动登录-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/">Activity Feed</a></li>';
                    }
                    ?>

我想用这个代码给用户发送到同一位置,但用在主页顶部的链接。 不幸的是,主页链接使用Wordpress菜单,据我所知道的,只允许使用连接到现有的网页静态链接的所有已创建。

难道我创建一个虚拟页面链接到只有退出执行上面的代码? 是否可能? 想象一个五十岁想读莎士比亚,和你有我作为一个编码器能力的想法,所以随意搞我,这样的 - 如果你说,“哦,只要创建一个作用域的功能,而不是创建一个全球性的,即功能”,我会盯着你流口水和困惑。

为了清晰图像:滑动登录菜单(WP-滑动登录|仪表板插件),显示在状态栏www.ferrignofit.com/members/FerrignoFit/activity/目标URL(登录用户当前是FerrignoFit): HTTP ://i.imgur.com/NPvmCXU.jpg

基于WordPress的页主菜单,这是我想要去到上述网址,但目前打算www.ferrignofit.com/activity/,不同的页面: http://i.imgur.com/dIiFpDC.jpg

Answer 1:

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.



文章来源: Create a Dynamic Site Address in a Wordpress Menu