0

Follow up from this: Apply a specific class/id to current page on menu (PHP)

Here's the code I'm using:

        <div id="bottoni" style="float:right;margin-top:30px;margin-right:30px">

            <?php
            // funzione per ottenere la class 'current' per la pagina che si sta visitando
            function get_current($name) {
                if (strpos($_SERVER['REQUEST_URI'], $name) !== false)
                    echo 'id="current"';
            }
            ?>

            <a <?php get_current('biografia') ?> href="<?php echo esc_url( home_url( '/' ) ); ?>biografia/"><img style="width:120px;margin-right:25px" src="http://robertocavosi.com/beta/wp-content/themes/robertocavosi/images/bottone_robertocavosi.jpg"></a>
            <a <?php get_current('attore') ?> <?php get_current('regista') ?> <?php get_current('autore') ?> <?php get_current('riconoscimenti') ?> <?php get_current('pubblicazioni') ?> <?php get_current('insegnamento') ?> id="tooltipclick"><img style="width:120px;margin-right:25px;cursor:pointer" src="http://robertocavosi.com/beta/wp-content/themes/robertocavosi/images/bottone_curriculum.jpg"></a>
            <a <?php get_current('gallery') ?> href="<?php echo esc_url( home_url( '/' ) ); ?>gallery/"><img style="width:120px;margin-right:25px;border:0" src="http://robertocavosi.com/beta/wp-content/themes/robertocavosi/images/bottone_gallery.jpg"></a>
            <a <?php get_current('itinera') ?> href="<?php echo esc_url( home_url( '/' ) ); ?>itinera/"><img style="width:120px;margin-right:25px;border:0" src="http://robertocavosi.com/beta/wp-content/themes/robertocavosi/images/bottone_itinera.jpg"></a>
            <a <?php get_current('contatti') ?> href="<?php echo esc_url( home_url( '/' ) ); ?>contatti/"><img style="width:120px;border:0" src="http://robertocavosi.com/beta/wp-content/themes/robertocavosi/images/bottone_contatti.jpg"></a>
        </div>

I'd like to specify in one "call" all these:

 <?php get_current('attore') ?> <?php get_current('regista') ?> <?php get_current('autore') ?> <?php get_current('riconoscimenti') ?> <?php get_current('pubblicazioni') ?> <?php get_current('insegnamento') ?>

I tried with:

 <?php get_current('attore', 'regista', 'autore', 'riconoscimenti', 'pubblicazioni', 'insegnamento') ?>

But it doesn't work...

3 Answers 3

1

Use func_get_args() like this:

<?php
function get_current() {
  foreach (func_get_args() as $arg) {
    if (strpos($_SERVER['REQUEST_URI'], $arg) !== false)
      echo 'id="current"';
  }
}

Then you can call the function from HTML:

<a <?php get_current('attore', 'regista', 'autore') ?> id="tooltipclick"><img src="http://robertocavosi.com/beta/wp-content/themes/robertocavosi/images/bottone_curriculum.jpg"></a>
Sign up to request clarification or add additional context in comments.

Comments

1

You can't expect to just pass a function multiple arguments and have it work if the function is designed to accept multiple arguments.

Instead, put your arguments in an array and call the function in a loop:

<?php
$args = array('attore', 'regista', 'autore', 'riconoscimenti', 'pubblicazioni', 'insegnamento');
foreach ($args as $arg) { get_current($arg); }
?>

Comments

1

You can do this with func_get_args:

<?php
// funzione per ottenere la class 'current' per la pagina che si sta visitando
function get_current() {
    foreach(func_get_args() as $arg) {
        if (strpos($_SERVER['REQUEST_URI'], $arg) !== false) {
            echo 'id="current"';
            break;
        }
    }
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.