vendredi 8 janvier 2016

Get post ID of current logged in user and add a link to the menu

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.

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://ift.tt/1MXMOAr' );
      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>
      ';
      }

THis generated a link such as this http://ift.tt/1S8ZQTIID 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.

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!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire