# Wrong link to appendix because of redefining appendix command

I adopted @Mico 's solution in here to determine the singular or plural form of Appendix in the ToC based on the number of sections in the appendix part. It works well for this purpose.

However, I find that the solution has a side effect that the hyperlink to appendix is wrong and also bookmarks in the PDF.

Please see the following MWE. Appendix 1.A is wrongly referred to section 1.1. The same problem happens in Appendix 2.A which is linked to section 2.1. But the reference to Appendix 2.B is right.

If there are other ways to change the singular or plural form of Appendix in the ToC, I am happy to accept it as well.

\documentclass[11pt, a4paper, twoside]{book}
\RequirePackage[title,titletoc]{appendix}
\usepackage{lipsum}

% solution in https://tex.stackexchange.com/a/47011/87806
\usepackage{ifthen}
\makeatletter
%% For source of "@seccntformat" command, see book "The LaTeX Companion"
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
{\csname the#1\endcsname\ \ }%  default
{\csname #1@cntformat\endcsname}}%    enable individual control
%% Individual control: '\section@cntformat'
\newcommand{\section@cntformat}{\thechapter.\thesection\ \ }

% Macros to redefine numbering of appendix sections
\newcommand{\appname}{} % dummy definition
\newcommand{\appsecnumbering}[1][0]{%
\ifthenelse{#1=1}{\renewcommand\appname{Appendix}}
{\renewcommand\appname{Appendices}}
\setcounter{section}{0}
\renewcommand\thesection{\thechapter.\Alph{section}}
\renewcommand{\section@cntformat}{\appendixname~\thesection\ \ }
\newcommand\normalsecnumbering{%
\renewcommand{\thesection}{\thechapter.\arabic{section}}
\renewcommand{\section@cntformat}{\thesection\ \ }}
\let\origchapter\chapter
\renewcommand\chapter{\normalsecnumbering % first, reset numbering style
\origchapter} % second, execute the original \chapter command
\makeatother

\usepackage[ pdftex, plainpages = false, pdfpagelabels,
pdfpagelayout = useoutlines,
bookmarks,
bookmarksopen = true,
bookmarksnumbered = true,
%pagebackref,
urlcolor  = blue,
citecolor = blue,
anchorcolor = green,
hyperindex = true,
pdfpagelabels,
hyperfigures
]{hyperref}

\usepackage{bookmark}

\begin{document}
\tableofcontents

\chapter{First chapter}
\section{A sections}
\lipsum[1]
\subsection{A subsection}

See Appendix \ref{app:first}. % Wrongly referred to section 1.1

\lipsum[2-4]

% \begin{subappendices}
\appsecnumbering[1]
\section{First appendix} \label{app:first}
\lipsum[2]
% \end{subappendices}

\chapter{Second chapter}
\section{A section}
See Appendix \ref{app:first2}. % Wrongly referred to section 2.1

See Appendix \ref{app:second}. % Correct

\lipsum[2-4]

% \begin{subappendices}
\appsecnumbering[2]
\section{First appendix} \label{app:first2}
\lipsum[2-4]
\section{Second appendix} \label{app:second}
\lipsum[2-4]
% \end{subappendices}

\end{document}