Use content component route helper. It will find Itemid for you.
$articleLink = ContentHelperRoute::getArticleRoute($row['aid'], $row['cid']);
// SEF url
$articleLinkSEF = JRoute::_($articleLink);
It considers that you have already created a menu item - your current category list menu item- which becomes an "authoritative" itemid for the content you are linking.
It is also possible that you are going to have further similar issues with any article of your Main top category, as they might use the itemid of your home page now, instead of the category's ...
There is a relevant discussion here, but in short it just concludes that using the jdatabase insertid method is just as rubbish as mysqli_insert_id.
Joomla DB Insert, how to get last inserted id?
The trick is to do a select using fields that you already have from the insert query, eg. get max id & match the userid, or any other fields that you might ...
You can do this using Regular Lab's Advanced Module Manager.
Install it and activate it, and select the following for the the module settings:
For Assigments use Joomla! Content, include pagetype, selection category Blog.
This will give you the module only on the category blog page, but not on the seperate article-view pages.
This can be resolved at the template level (which is the cleanest way of doing this) as described here.
Essentially, you will need to modify your template's index.php file to include a condition to check if the current view is not an article view, and then add the module tag if that's the case. Here's an example condition (note: the condition in the blog ...
Joomla 3.8.2 does not add any solution to your issue.
One solution you didn't mention would be to use CSS to hide the unwanted modules on article layout pages.
Pros: It's easy to implement and, depending on how your template sets its class variables, highly configurable.
Cons: The hidden modules are still having to be build/rendered by joomla and added to ...
If you install Advanced Module Manager (free extension), you'll get a lot more control over your modules and where they will be displayed/hidden. The extension has many filters and conditions, and I'm sure you'll be able to find a setting that hides the module, although I can't tell you exactly what setting will work in your specific case.
Hope that helps a ...
Do you have (hidden) menuitems linked directly to these subcats as well? Because else Joomla doesn't know where to route to, other than the current Itemid.
So you will need an extra (hidden) menuitem pointing to that subcat (blog-layout).
You can do this by creating a hidden field in RS Form and in its default value field insert a code as below:
$jinput = JFactory::getApplication()->input;
return ($jinput->get('id', 'No K2 Item', 'INT'));
Your hidden field will be sending the K2 Item's id, assuming you are only using it inside K2 items, with each ...