获取当前登录用户的帖子ID并添加菜单链接 [英] Get post ID of current logged in user and add a link to the menu

查看:140
本文介绍了获取当前登录用户的帖子ID并添加菜单链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个名为wp job manager的插件,该插件会在wordpress帖子中创建"jobs".我将用户只能执行的工作数量限制为一个,并希望在导航菜单中为其工作添加直接链接.

I am using a plugin called wp job manager, which creates "jobs" as wordpress posts. I am limiting the number of jobs a user can have to one and would like to add a direct link to their job in the navigation menu.

我还想添加一个编辑链接,该链接是使用作业的职位ID动态生成的.好像用户只会发表一篇文章,我相信这是可行的.

I would also like to add a edit link, which is dynamically generated using the post id of the job. Seeing as though users will only have one post , I believe this is do-able.

如果我什至可以只获得当前登录用户的单个帖子的链接并将其添加到菜单中,我相信我可以在帖子本身内部添加编辑链接.

这样动态地创建编辑作业网址(循环)

The edit job url is created dynamiclly like this (in a loop)

 <?php foreach ( $jobs as $job ) : ?>
<tr>
   <td class="actions">
      <ul class="job-dashboard-actions">
      <?php
      $actions = array();
      switch ( $job->post_status ) {
      case 'publish' :
      $actions['edit'] = array( 'label' => __( 'Edit', 'wp-job-manager' ), 'nonce' => false );
      break;
      }
      $actions           = apply_filters( 'job_manager_my_job_actions', $actions, $job );
      foreach ( $actions as $action => $value ) {
      $action_url = add_query_arg( array( 'action' => $action, 'job_id' => $job->ID, ) , 'http://www.mywebsite./edit-my-profile/' );
      if ( $value['nonce'] ) {
      $action_url = wp_nonce_url( $action_url, 'job_manager_my_job_actions' );
      }
      echo '
      <li><a  href="' . esc_url( $action_url ) . '" class="job-dashboard-action-' . esc_attr( $action ) . '">' . esc_html( $value['label'] ) . '</a></li>
      ';
      }

生成了这样的链接,例如 http://www .mywebsite.com/edit-my-profile/?action = edit& job_id = ID ,因此,如果尝试获取当前作者的帖子ID并将其附加到此url,然后将其添加到菜单中,但是我似乎找不到一种方法可以在发布循环之外获取当前已登录用户的发布ID.

THis generated a link such as this http://www.mywebsite.com/edit-my-profile/?action=edit&job_id=ID so if have tried getting the post id of the current author and appending it to this url and then adding it to the menu, but I can't seem to find a way to get the post id of current logged in user outside of the post loop.

因此,我改为尝试使用全局$ actionurl变量,并使用此功能将其添加到菜单中,但这只会破坏菜单.

So instead I tried using the global $actionurl variable and adding it to the menu with this function, but it just breaks the menu.

function username_nav($action_url) {
    return $menu = str_replace('editprofile', $action_url, $menu);
}

 add_filter( 'wp_nav_menu', 'username_nav' );

有什么建议吗?预先感谢!

Any suggestions? Thanks in advance!

推荐答案

如果我正确地阅读了您的问题...您希望能够获得当前登录用户创作的所有帖子?就您而言,这只会是一篇文章吗?在这种情况下,您需要这样的东西:

If I'm reading your question right... you want to be able to get all the posts authored by the currently logged in user? In your case, it will only ever be one post? If this is the case, you want something like this:

    global $current_user;    
    $args = array(
        'post_type'      => 'name of job custom post type',
        'author'         => $current_user->ID,
        'status'         => 'publish',
        'posts_per_page' => 1
        );
    $jobs = get_posts( $args );

您可能需要全局$ current_user,但不一定会受到伤害.上面的将返回一个已发布的帖子,当前登录的用户是该帖子的作者.只需输入适当的自定义帖子类型名称,因为我不知道该插件为名称生成了什么.

You may or may not need the global $current_user, but it won't hurt. The above will return one published post of which the currently logged in user is an author of. Just put the appropriate custom post type name in, as I don't know what that plugin generates for a name.

更新

要生成此帖子的链接并将其添加到当前导航的末尾,您将使用类似以下的方法:

To generate a link to this post and add it to the end of your current navigation, you would use something like:

function new_nav_menu_items( $items ) {
    global $current_user;    
    $args = array(
        'post_type'      => 'job_listing',
        'author'         => $current_user->ID,
        'status'         => 'publish',
        'posts_per_page' => 1
        );
    $jobs = get_posts( $args );
    $link = '<li><a href="' . get_permalink( $jobs->ID ) . '">Your Job</a></li>';
    // add link to the end of the menu
    $items = $items . $link;
    return $items;
}
add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

如果您希望菜单内的链接位于特殊位置,则需要使用自定义菜单浏览器.

If you want the link somewhere special inside the menu, you'll need to resort to a custom menu walker.

这篇关于获取当前登录用户的帖子ID并添加菜单链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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