Joomla module, get menu id (ItemID) from article ID

10,540

You can get menu id of an article using the below code-

$link = 'index.php?option=com_content&view=article&id='.(int)$articleId ;           
$menu = JSite::getMenu();
$menuItem = $menu->getItems( 'link', $link, true );
$Itemid = $menuItem->id;

Hope this will help.

Share:
10,540
sMyles
Author by

sMyles

My name is Myles McNamara and I have an obsession with technology, innovation, creation, and reverse-engineering. From taking apart my parents alarm clocks and VHS players to finding jtag on a device. Technology has become a passion that has become my life. I built my first computer before I could even drive a car. You can normally find me writing code until 4am, desk covered in soda cans, just as it has been ever since I was in high school writing code in VB on a P166. Nothing has changed, except now I have a beaglebone, arduino, and a few other PCBs next to all the cans. I have a passion for development which started with VB, progressed to HTML, ended up on PHP, and just recently started expanding to jQuery, Python, and Javascript. I'm always looking to learn more and thanks to SO finding help or providing it has become that much easier! "When the power of love overcomes the love of power, the world will know peace."

Updated on June 24, 2022

Comments

  • sMyles
    sMyles about 2 years

    So i've been searching and testing for a couple days and can't seem to figure this out.

    I'm using the K2 content module and I need to add "?Itemid=111" to the end of the URL's it generates for the "Read More" link on pages.

    This is not done by default and i've found where I can add it, but i'm having problems getting the Itemid (menu id) from the article id.

    All the examples i've found use

    $app   = JFactory::getApplication();
    $menu   = $app->getMenu();
    $activeId = $menu->getActive()->id;
    

    And that works, whenever you're on that page, and it does add the correct ID. But for the articles I display on the homepage it does not work correctly as it adds the Itemid (menu id) of the homepage, and not the menu id of that specific article.

    Before when using Joomla 1.5 I would use the built-in function

    getItemid($articleid)
    

    That doesn't seem to work for me anymore.

    Does anybody have any suggestions or can anybody point me in the right direction on how I could solve this and "Get menu id (Itemid) from the article id"?

    Thanks!!!