Get the article ID of an article in a blog view

In this case, I needed to find the id of an article that was in a blog view. To do this in blog.php, you'll find that there is already the following code:

            $this->item = &$item;

which contains an array of all the article parameters and content. As a result if you put:

            echo $this->item->id;

immediately after it, you get the id number of the article.

PHP code for use in a Joomla template to set the background image to be the full_text image from the article or the image for the category

 

<!-- bectin -->
<?php
// This code sets the background images of the body tag to be the full_text image from the article.
// If no article is assigned, it chooses the image from the first article in the category of the selected article.
// If the first article in the category doesn't have an image then it chooses the default.
    $app = JFactory::getApplication();

    if($app->input->getCmd('option') == "com_content" && $app->input->getCmd('view') == "article" ) {
        $article_id = $app->input->getCmd('id');
    
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select($db->quoteName('catid'));
        $query->from($db->quoteName('#__content'));
        $query->where($db->quoteName('id') . ' = '. $db->quote($article_id));
        $db->setQuery($query);
        $cat_id = $db->loadResult();
    
    
    // if the current page has an assigned background photo
        $article = JTable::getInstance("content");
        $article->load(JRequest::getInt("id")); // Get Article ID
        
        $article_images = $article->get("images"); // Get image parameters
        $pictures = json_decode($article_images); // Split the parameters apart
        // Print the image
        if ($pictures->{'image_fulltext'}!='') // there is an image in the image_fulltext field
        {
        echo "<style> body {background: url('../" . $pictures->{'image_fulltext'} . $pictures->{'image_fulltext_alt'} . "');background-attachment: fixed; color: #444;}</style>";
        }
        
        else
        {
        //If the current article does not have an assigned background photo choose category image
    
            $query="SELECT params FROM #__categories WHERE id=$cat_id";
            $db->setQuery($query);
            $db->query();
            $article_images= $db->loadAssocList();
            $pictures = json_decode($article_images[0][params]); // Split the parameters apart
            if ($pictures->{'image'}!='') // there is an image in the image field
            {
            echo "<style> body {background: url('../" . $pictures->{'image'} . "');background-attachment: fixed; color: #444;}</style>";
            }
            else
            {
                echo "<style> body {background: url('/images/backgrounds/bg2000.jpg');background-attachment: fixed; color: #444;}</style>";            
            }
        }
    }
// set background for contacts
    $option   = JRequest::getCmd('option');
    $view   = JRequest::getCmd('view');        
    if ($view=='contact') echo "<style> body {background: url('/images/backgrounds/run.jpg');background-attachment: fixed; color: #444;}</style>";
?>
<!-- end bectin -->

To retrieve the URL of the intro image in a Joomla article

This code is taken from: http://www.eighttwentydesign.com/blog/all/149-how-to-get-article-s-intro-image-in-joomla-using-php

They provide a more detailed explanation of how to use this code. Note, I had to make one change from their original code to include a / character after the img src=' text.

<?php

// Article Image
$article = JTable::getInstance("content");
$article->load(JRequest::getInt("id")); // Get Article ID

$article_images = $article->get("images"); // Get image parameters
$pictures = json_decode($article_images); // Split the parameters apart
// Print the image
echo "<img src='/" . $pictures->{'image_intro'} . "' alt='" . $pictures->{'image_intro_alt'} . "'>";

?>

Using the Full Text image as a Background image

I've often had customers that wanted to be able to configure a unique page for the background of each page on a site. Whether this is a good idea or not is irrelevant.

Here's how I do it.

Add the following text to the index.php file for the tempalte right before the </head> tag. .

$article = JTable::getInstance("content");
$article->load(JRequest::getInt("id")); // Get Article ID
$article_images = $article->get("images"); // Get image parameters
$pictures = json_decode($article_images); // Split the parameters apart
echo "<style> body {background: url('../" . $pictures->{'image_fulltext'} . $pictures->{'image_fulltext_alt'} . "');}</style>";

You may want to set a default background in the regular css file for the site so that no page will have a blank background.

Problem with accented characters not showing up using PHP and MySQL

If your data is loosing its accented characters when you retrieve and store it in your database, try using the following command:

SET NAMES 'utf8'