james
james

Reputation: 67

How to fetch the title of a Joomla article?

I have designed a Joomla template. I want to bring my articles on my template, I have written code that is bring article body text on my template but neither image nor title is coming up. Can anyone help please ?

$query = "select * from jh7na_content order by id desc limit 2";
$sql = mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_array($sql)){
    echo "<div id='div1' style='border='2px';'>".html_entity_decode($row['introtext'])."</div>";             
}

Upvotes: 0

Views: 679

Answers (2)

Lodder
Lodder

Reputation: 19733

A few things you should know. Firstly, if you want to show an article, why not create a menu item, and assign an article to it in the Joomla backend? Secondly, your query is only showing the article text because you have told it to and are not calling the Title. Thirdly, you should be using Joomla coding standards, especially when making database queries, like so:

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*')
 ->from('#__content')
 ->order('id DESC');
$db->setQuery($query,0,2);
$rows = $db->loadObjectList();

foreach($rows as $row){
    echo "<div class='title'>" . $row['title']. "</div>"; 
    echo "<div class='introtext'>" . $row['introtext']. "</div>"; 
}

However, as stated before, you should assign an article to a menu item.

Hope this helps

Upvotes: 2

Elin
Elin

Reputation: 6755

Well since you are only echoing the intro text and not the title or the image, that would explain why the introtext and not the title or image is showing. Look at the core layouts to see how to render each part of an article.

However, if you mean you have written an actual template, which in Joomla is used for design purposes, you should not be putting any specific code like this there, you would just be using a jdoc statement to include your article.

Do not reinvent the wheel. Joomla is a content management system, use it for content management. It will automatically load your content for you. That's what it is totally designed to do -- take care of that part while you worry about content and design. If you want to make a different appearing page than the core you can do a layout override (search for an answer on how to do that). But I suggest first that you learn a bit about how Joomla works. Open up the core code and study it, then you will know how to work with it.

Upvotes: 0

Related Questions