I am using this code to show a module inside blog.php:

 <?php echo JHtml::_('content.prepare', '{loadposition myModule}'); ?>

How can I show it only on the first page of the category blog? I want to combine the above code with the code below:

<?php if (!JRequest::getInt('start') < 1) { ?>
<?php } ?>

but for some reason I am not doing it right. Any help? Thank you.

  • Are you passing a variable (via hyperlink or form submission) that equates to start=0 or start=1 etc.? Your conditional logic is a little twisted. I think you should pull that ! out of the expression (or remove < 1) -- you don't want to flip the evaluated boolean value that is generated. Please confirm that you are receiving a value from JRequest. What is your current output? Any errors/warnings/notices being generated? – mickmackusa Oct 16 '18 at 5:56

You have an error in logic check. Now it runs when start is >= 1. You can remove the ! operator or the comparison (< 1).

Some other thoughts:

JRequest is deprecated. JInput should be used instead of it. But in this case it's best to use values from state.

You can render the module using document's renderer instead of plugin shortcode. This will prevent content plugins from running on generated module content, which may or may not be desirable.

<?php if (!$this->state->get('list.start')) : ?>
    <?php echo JFactory::getDocument()->loadRenderer('modules')->render('myModule'); ?>
<?php endif; ?>

To show only on 2nd page, compare list.start value to list.limit value. Remember, this isn't 100% reliable because users can manipulate this by changing the URL.

<?php if ($this->state->get('list.start') == $this->state->get('list.limit')) : ?>
    <?php echo JFactory::getDocument()->loadRenderer('modules')->render('myModule'); ?>
<?php endif; ?>
  • thank you, it worked perfect. just a tip: how is the code if I want (i.e.) to be only in the 2nd page? – geoplous Oct 16 '18 at 6:01
  • If start=0 is the first page and start=1 is the second page, you use <?php if (JFactory::getApplication()->input->getInt('start') == 1) { echo JFactory::getDocument()->loadRenderer('modules')->render('myModule'); } ?> – mickmackusa Oct 16 '18 at 21:52
  • It's tricky because there are no defined page numbers in list views. Instead there are start and limit parameters, both of which can be manipulated manually. Suppose, you have 5 item limit per page. Users can just append start=3 to URL and end up on a page that is neither 1st (doesn't start at 0) nor 2nd (doesn't start at 5). But if that doesn't matter, use list.limit and list.start values from state. See updated answer. – Sharky Oct 17 '18 at 5:57

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.