且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取当前登录用户的帖子ID并添加菜单链接

更新时间:2023-11-30 17:50:16

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

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.