Search code examples
phpmodx

modx php get current page name


In ModX, I'm trying to make a simple redirect based on which page the user is on. On my site, I have a chunk with various links to pages such as "About Us", "Careers", etc. This chuck is used on all pages on the site.

My issue starts here - within the Careers page, there is a link to a new page which contains all available job postings. I would like to make it so that when the user is on the Careers page, clicking the link to the Careers page on the chunk will take them directly to the job posting page, rather than looping them right back to the Careers page.

The url to the careers page is in this format:

www.example.com/index.php?q=careers.html

So, my code in the chunk for if the user is on the Careers page, redirect to job listings:

<?php
    if ($_SERVER['PHP_SELF'] == "index.php?q=careers.html") {
      echo '<p><a href="www.joblistsite.com">Careers</a></p>';
    } 
    else {
      echo '<p><a href="index.php?q=careers.html">Careers</a></p>';
    }
?>

But this doesn't work. It continues to link back to the Careers page instead of the job listings page. I've tried using the page's ID, but that doesn't either. What should I use for the if statement to get the current page in ModX? What am I doing wrong?


Solution

  • I don't have experience with modx, but the best option to determin this is to debug the $_SERVER variable and check the contents of the various 'keys' inside it;

    echo '<pre>'; print_r($_SERVER); echo '</pre>';