4

I have this piece of code in my template:

<article id="content">
    <section id="mid">
        <jdoc:include type="component" />
    </section>
</article>

which displays the content of my site.

I know there is a way to check if a module exists with $this->countModules('mymodulename') but is it possible to check if content exists?

So I only want to output #content and #mid if actual content exists in the respective article. So far I get the empty html tags displayed If I dont have any content in the article.

  • 1
    Hmm, yes it's possible to check if an article has content but since I am on the road I don't have the time to write an answer. However, what could be the purpose of an empty article? Actually, what do you have in mind and what you would want to display if no content exists? – FFrewin Sep 28 '15 at 9:50
  • I agree with @FFrewin, if you explain what you're trying to do, it's easier to suggest solutions. If articles are empty, why not just unpublish them? – johanpw Sep 29 '15 at 14:20
2

I'd agree with comments, best to maybe un-publish empty articles.

Also there is one possible flaw in this plan, and that is the assumption that the component being called is com_content with a single article. What if the page the menu item points to is not com_content, or if it is a blog view of com_content? In those instances you wont have a single article id.

These thoughts aside, you could do this with the following code...

# get the app object, you may already be doing this in your code
$app = JFactory::getApplication();
# get the article id
$article_id = $app->input->get('id');
# check we have one
if ($article_id) {
    # load the JTableContent object
    $article = JTable::getInstance('content');
    $article->load($article_id);
    # use these for debug
    echo $article->get('title');
    echo $article->get('introtext');
    if ( strlen($article->get('introtext') ) ) {
        echo "we have content :)";
        ?>
        <article id="content">
            <section id="mid">
                <jdoc:include type="component" />
            </section>
        </article>        
        <?php
    } else {
        echo "we have no content :( ";

    }    
}

This uses the Joomla's JTableContent, see https://docs.joomla.org/Creating_content_using_JTableContent

  • This answer is a nice solution but it doesn't work in some situation like on a page that we want to display Login Form. So if we have a Menu item type pointing to: Users » Login Form the solution wouldn't work. – emilushi Apr 10 '16 at 21:15
  • Yes, you're right, as mentioned there is the assumption that the component being called is com_content with a single article. – AndyGaskell May 18 '16 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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