Welcome to the MetaMod and Chameleon Support Forums.

Before posting, please check out the FAQs.

helpme

 

Need extra help with your Joomla site? Consider paid Joomla support by the developer of Chameleon and MetaMod.

 

post module if no article present

post module if no article present

Hi I am after a recipe to display a module in a category or section blog layout only if there are no articles present in that category. Is that something that can be coded using MetaMod? Thanks in advance.

adamwclarke
Beginner Modder
ranks
useravatar
Offline
14 Posts
Administrator has disabled public posting

Re: post module if no article present

Sure, that shouldn't be too hard. We'll just modify one of the "Article" recipes on http://www.metamodpro.com/support/recip … e-category

Code:

if ( $option == "com_content" and $view == "category" ) {

  /* category list pages (blog or list style) */
  $category_id = (int)$id;

  $nullDate = $db->Quote( $db->getNullDate() );
  $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
  $jnow  =& JFactory::getDate();
  $now    = $db->Quote( $db->getEscaped( $jnow->toMySQL() ) );
  $query = "SELECT count(*) as c"
      . " FROM #__content WHERE catid = $category_id AND state = 1"
      . " AND ( publish_up = $nullDate "
      . " OR publish_up <= $now )"
      . " AND ( publish_down = $nullDate "
      . " OR publish_down >= $now  )";
  $db->setQuery( $query, 0, 1 );
  $row = $db->loadObject();
  $count = (int)@$row->c;
  if ($count == 0) return XXX; // module id to display when there are no items in this category
}

Tested, and works nicely on my test server.

Cheers,
Stephen

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3314 Posts
User info in posts
Administrator has disabled public posting

Re: post module if no article present

Thanks for this Stephen, your a legend.
Can I trouble you for a slight modification. Is there a way to modify the code so that a module is displayed only when the count of articles that are NOT labelled Frontpage (Joomla article manager) is 0. Currently the module counts all articles Frontpage or not. Could it be more specific. Thanks in advance.

adamwclarke
Beginner Modder
ranks
useravatar
Offline
14 Posts
Administrator has disabled public posting

Re: post module if no article present

I think so - try this:

Code:

if ( $option == "com_content" and $view == "category" ) { 

  /* category list pages (blog or list style) */
  $category_id = (int)$id;

  $nullDate = $db->Quote( $db->getNullDate() );
  $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
  $jnow  =& JFactory::getDate();
  $now    = $db->Quote( $db->getEscaped( $jnow->toMySQL() ) );
  $query = "SELECT count(*) as c"
      . " FROM #__content WHERE catid = $category_id AND state = 1"
      . " AND id not in (select content_id from #__content_frontpage) "
      . " AND ( publish_up = $nullDate "
      . " OR publish_up <= $now )"
      . " AND ( publish_down = $nullDate "
      . " OR publish_down >= $now  )";
  $db->setQuery( $query, 0, 1 );
  $row = $db->loadObject();
  $count = (int)@$row->c;
  if ($count == 0) return XXX; // module id to display when there are no items in this category
}

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3314 Posts
User info in posts
Administrator has disabled public posting

Re: post module if no article present

Hi

This looks very interesting! I have News Show Pro GK5 on my front page. But I get a 1064 error if no article in a specific category is published. So I am trying to display a module if there is no articles published in that category. How can I specify a category and display News Show Pro GK5 if articles are published and a another module if no articles are published? (if else).

Code:

if ( $option == "com_content" and $view == "article" and $id == '19' ) { 

  /* category list pages (blog or list style) */
  $category_id = (int)$id;
  $nullDate = $db->Quote( $db->getNullDate() );
  $my_id = $db->Quote( $db->getEscaped( (int)$id ) );
  $jnow  =& JFactory::getDate();
  $now    = $db->Quote( $db->getEscaped( $jnow->toMySQL() ) );
  $query = "SELECT count(*) as c"
      . " FROM jos_content WHERE catid = $category_id AND state = 1"
      . " AND id not in (select content_id from jos_content_frontpage) "
      . " AND ( publish_up = $nullDate "
      . " OR publish_up <= $now )"
      . " AND ( publish_down = $nullDate "
      . " OR publish_down >= $now  )";
  $db->setQuery( $query, 0, 1 );
  $row = $db->loadObject();
  $count = (int)@$row->c;
  if ($count == 0) return 130; // module id to display when there are no items in a specific category
}

Thanks in advance

Jon

thewizl
Beginner Modder
ranks
useravatar
Offline
1 Posts
Administrator has disabled public posting

Re: post module if no article present

Hi Jon,

can you tell me what version of Joomla you are using? The PHP and SQL query may be a little different in each case.

If you simply want to alter the code above to be an if/else, then change that 2nd-to-last line (with the "if" in it) to this:

if ($count == 0) return 130; // when no items in category
else return 131; // when there are items in category


Thanks,
Stephen

Stephen Brandon
MetaMod / Chameleon developer
If you use MetaMod or Chameleon, please post a rating and a review at the Joomla! Extensions Directory: Chameleon | MetaMod

metamodguy
useravatar
Offline
3314 Posts
User info in posts
Administrator has disabled public posting

Board Info

Board Stats:
 
Total Topics:
1691
Total Polls:
6
Total Posts:
5912
Total Posts Today:
6
User Info:
 
Total Users:
11515
Newest User:
velis415
Members Online:
0
Guests Online:
270

Online: 
There are no members online

Forum Legend:

 Topic
 New
 Locked
 Sticky
 Active
 New/Active
 New/Locked
 New Sticky
 Locked/Active
 Active/Sticky
 Sticky/Locked
 Sticky/Active/Locked