按帖子名称突出显示菜单项

时间:2012-07-19 作者:fightstarr20

在几个用户的帮助下,我的最后两个问题是通过ID突出显示自定义菜单项。

现在,我正在尝试将以下两个函数的代码结合起来,以使其按帖子名工作:

Add highlighting to new Admin Dashboard Menu Item

add_action( \'admin_menu\', \'create_menu\' );
add_action( \'admin_head-post.php\', \'wpse_58567_highlight_menu_item\' );

function create_menu() {
    $settings_page = add_menu_page(
                                   \'Edit_Post_69\', 
                                   \'Edit_Post_69\', 
                                   \'add_users\', 
                                   \'/post.php?post=69&action=edit\', 
                                   \'\', 
                                   get_stylesheet_directory_uri() . \'/editicon.png\', 
                                   2
                                  );
}

function wpse_58567_highlight_menu_item() {
    global $post;

    if( 69 != $post->ID )
        return;
?>
    <script type="text/javascript">
        jQuery(document).ready( function($) {
            $(\'#toplevel_page_post-post-69-action-edit\').removeClass(\'wp-not-current-submenu\').addClass(\'current\');
            $(\'#toplevel_page_post-post-69-action-edit\').find(\'a:last\').addClass(\'current\');
        });     
    </script>
<?php
}
还有。。。。

Convert post name into post ID

$post = get_page_by_title( $post_name, OBJECT, \'post\' );
echo $post->ID;
我现在正在尝试将它们结合起来,这样我就可以指定一个帖子名称而不是帖子ID,有人能给我指出正确的方向吗?

3 个回复
最合适的回答,由SO网友:brasofilo 整理而成

注意使用get_page_by_title 参数。

以及Heredoc PHP 语法。

$the_post_title = \'The Portfolio\';

add_action( \'admin_menu\', \'wpse_59050_add_menu\' );
add_action( \'admin_head-post.php\', \'wpse_59050_highlight_menu_item\' );

function wpse_59050_add_menu() 
{
    global $the_post_title;
    $our_page = get_page_by_title( $the_post_title );

    $settings_page = add_menu_page( \'Edit \'.$our_page->post_title, \'Edit \'.$our_page->post_title, \'add_users\', \'/post.php?post=\'.$our_page->ID.\'&action=edit\', \'\', \'\', 2);
}

function wpse_59050_highlight_menu_item()
{
    global $the_post_title, $post;

    if( !is_object( $post ) )
        return;

    $our_page = get_page_by_title( $the_post_title );

    if( $our_page->ID != $post->ID )
        return;

    echo <<<HTML
        <script type="text/javascript">
            jQuery(document).ready( function($) {
                $(\'#toplevel_page_post-post-{$our_page->ID}-action-edit\').removeClass(\'wp-not-current-submenu\').addClass(\'current\');
                $(\'#toplevel_page_post-post-{$our_page->ID}-action-edit\').find(\'a:last\').addClass(\'current\');
            });     
        </script>
HTML;
}
Result:
enter image description here

SO网友:TheDeadMedic

有一种更容易设置当前菜单上下文的方法parent_file 滤器

add_filter( \'parent_file\', array( \'WPSE_59050\', \'parent_file\' ) );
add_action( \'admin_menu\',  array( \'WPSE_59050\', \'admin_menu\' ) );

class WPSE_59050
{
    /**
     * The title of the post to add our menu item for.
     */
    const POST_TITLE = \'My Post Title\';

    /**
     * Our cached post ID from title.
     * 
     * @var int
     */
    private static $_post_id;

    /**
     * Correctly set the menu context if we\'re editing our post.
     *
     * @return string
     */
    public static function parent_file( $parent_file )
    {
        global $pagenow;

        if ( $pagenow == \'post.php\' && isset( $_REQUEST[\'post\'] ) && $_REQUEST[\'post\'] == self::get_post_id() )
            $parent_file = "post.php?post={$_REQUEST[\'post\']}&action=edit";

        return $parent_file;
    }

    /**
     * Add the menu item if the post can be found.
     */
    public static function admin_menu()
    {
        if ( $post_id = self::get_post_id() )
            add_menu_page( \'Edit \' . self::POST_TITLE, \'Edit \' . self::POST_TITLE, \'add_users\', "/post.php?post=$post_id&action=edit" );
    }

    /**
     * Get our post ID from the title & cache it.
     *
     * @return int
     */
    public static function get_post_id()
    {
        if ( ! isset( self::$_post_id ) ) {
            if ( $post = get_page_by_title( self::POST_TITLE, OBJECT, \'post\' ) )
                self::$_post_id = $post->ID;
            else
                self::$_post_id = null;
        }

        return self::$_post_id;
    }
}
Update: &更改菜单;页面标题使用帖子标题,而不是ID。

SO网友:Everton Ramos
add_filter( \'parent_file\', \'parent_file_hover\' );
function parent_file_hover( $parent_file )   {
    global $pagenow;
    if ( $pagenow == \'post.php\')
        $parent_file = "post.php?post={$_REQUEST[\'post\']}&action=edit";
    elseif($pagenow == \'post-new.php\')
        $parent_file = "post-new.php?post_type={$_REQUEST[\'post_type\']}";
    return $parent_file;
}
结束

相关推荐