0

I am practically new to Joomla and I need to get the SEF path of an article from an external php script. I feel like I'm close, but something is not working for me. The preliminary code I have as an example is:

require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$mainframe = JFactory::getApplication('site');
$mainframe->initialise();

$articleId = 80;

$url = JRoute::_("index.php?option=com_content&view=article&id=" . $articleId);

With this, JRoute returns the path /component/content/article?Id=80&Itemid=437. After doing some research, I read that I must include the "itemId" of the menu.

I'm not sure if it's the "437" that the JRoute returns to me, or if I should get it separately. Based on my reading, one way to get the menu item id is:

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

However the $Itemid returns empty. I would appreciate if someone could guide me with this. What I need is to build the SEF path of the article, regardless of its menu structure. My final need is to create a link to that article based on its articleID from an external php script.

1
  • Welcome to JSE. Please take our tour. If you discover additional relevant details, please add them to your question as an edit to help volunteers to understand your scenario. If you manage to resolve this yourself, you are encouraged to post an educational answer. Aug 29, 2020 at 2:33

1 Answer 1

1

This only works if the article you are attempting to get the menu item id for exists in a menu. i.e. if your menu only has a link to the category, but not the article, you will find your $Itemid = $menuItem->id; will throw a Trying to get property of non-object because $menuItem is not an object, rather an empty array.

$articleId = 10;
$link = 'index.php?option=com_content&view=article&id=' . $articleId;
$menu = JFactory::getApplication()->getMenu('site');
$menuItem = $menu->getItems('link', $link, true);
$Itemid = $menuItem->id;
echo $Itemid; // will output the menu item ID only if a menu item exists for article id 10.

As I suspect, your Joomla site does not have a menu item for this article, therefore it is not possible to make a SEF url; so you are left with two options:

  1. Create a menu item for this article, set Display in Menu: Hidden, OR, create an entirely new menu and create a menu item for your article there.
  2. Just use JRoute as standard:
echo Route::_('index.php?option=com_content&view=article&id=10');

This will output as:

/?id=10

However, this still works, navigate to the URL and it will still display your article, albeit without a SEF friendly URL - for SEO purposes you should be using canonical URL's anyway.

1
  • Thank you very much Grant. You were absolutely right, I didn't have a menu assigment. I did that and now it works.
    – Andino
    Aug 31, 2020 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.