You didn't give an example of why the accepted answer you linked does not work for you. To my experience, the reason might be:
- You are not using the standard classes
article, book etc. In your class there might be no \@startsection or \@xsect so the patches do not work.
- The style of
\section, \subsection, etc. have been modified by package like titlesec.
A quick note: the command \section is more sophisticated than you thought: it detects whether it has a star follows and there is usually also an optional argument for assigning the short title for substituting the actually title in the hyperlinks, and then it reads in the main argument, i.e. your section title. Thus you cannot expect something simple as \renewcommand*{\section}{\nolinenumbers\section\linenumbers} to work.
In my own packages or classes, I usually have the following:
\newif\ifLNturnsON
\def\LocallyStopLineNumbers{\LNturnsONfalse%
\ifLineNumbers\LNturnsONtrue\fi\nolinenumbers}
\def\ResumeLineNumbers{\ifLNturnsON\linenumbers\fi}
\LocallyStopLineNumbers and \ResumeLineNumbers are better than just \nolinenumbers and \linenumbers in that they detect if the line numbering has been turned on, so after disabling the line numbers \ResumeLineNumbers won't enable it if the user hasn't enabled the line numbering at the time of \LocallyStopLineNumbers.
Now for the solution, either way, you can use titlesec and add \LocallyStopLineNumbers to the beginning of the format, and [\LocallyStopLineNumbers] to the end. In the following example, I included a complete set of formats that mimic the default styles (the code is copied from documentation of titlesec, "8.2. Standard Classes"). You can of course modify your own style in the similar way.
\documentclass{article}
\usepackage{lineno}
\usepackage{lipsum}
\usepackage{titlesec}
\newif\ifLNturnsON
\def\LocallyStopLineNumbers{\LNturnsONfalse%
\ifLineNumbers\LNturnsONtrue\fi\nolinenumbers}
\def\ResumeLineNumbers{\ifLNturnsON\linenumbers\fi}
\makeatletter
\titleformat{\chapter}[display]
{\LocallyStopLineNumbers\normalfont\huge\bfseries}{\chaptertitlename\ \thechapter}{20pt}{\Huge}[\ResumeLineNumbers]
\titleformat{\section}
{\LocallyStopLineNumbers\normalfont\Large\bfseries}{\thesection}{1em}{}[\ResumeLineNumbers]
\titleformat{\subsection}
{\LocallyStopLineNumbers\normalfont\large\bfseries}{\thesubsection}{1em}{}[\ResumeLineNumbers]
\titleformat{\subsubsection}
{\LocallyStopLineNumbers\normalfont\normalsize\bfseries}{\thesubsubsection}{1em}{}[\ResumeLineNumbers]
\titleformat{\paragraph}[runin]
{\LocallyStopLineNumbers\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}[\ResumeLineNumbers]
\titleformat{\subparagraph}[runin]
{\LocallyStopLineNumbers\normalfont\normalsize\bfseries}{\thesubparagraph}{1em}{}[\ResumeLineNumbers]
\titlespacing{\chapter} {0pt}{50pt}{40pt}
\titlespacing{\section} {0pt}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\titlespacing{\subsection} {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing{\subsubsection}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\titlespacing{\paragraph} {0pt}{3.25ex plus 1ex minus .2ex}{1em}
\titlespacing{\subparagraph} {\parindent}{3.25ex plus 1ex minus .2ex}{1em}
\makeatother
\linenumbers
\begin{document}
\section{A section} \lipsum[2]
\subsection{Another section} \lipsum[2]
\subsubsection{Final section} \lipsum[2]
\end{document}

\documentclass{scrartcl} ... \usepackage{scrletter}in my preamble (for a letter but with section capabilities).