用PHP模拟CSS?

So, I want to make a side menu in WordPress.

It should display:
1) all the first-level links
2) the full "path" to the link that is currently selected

I found out that thanks to the WordPress CSS menu class structure I can emulate the behavior I want to get with this code:

ul#menu-test ul {display:none;}
ul#menu-test li.current-menu-ancestor > ul {display:block;}
ul#menu-test li.current-menu-item > ul {display:block;}
ul#menu-test li.current-menu-item > a {background-color:yellow;}

But I don't want to repeat the whole menu again and just trim it with the CSS, I just want to physically display only the items I really need.

Can I somehow emulate this behavior in PHP? I can pre-fetch the full menu in text format, if that matters.

If i understand correctly the second thing you want to display is something called "breadcrumbs". Here is example solution for that: PHP Simple dynamic breadcrumb Also you can google many of other solutions for breadcrumbs if you need.

You can echo your menu with php and then operate on that section with php but still to format it visually you still need css as this is purpose of css. For example:

echo('<ul>');
  echo('<li><a href="index.php">Home</a></li>');
  echo('<li><a href="news.php">News</a></li>');
  echo('<li><a href="contact.php">Contact</a></li>');
  echo('<li><a href="about.php">About</a></li>');
echo('</ul>');

Let this be just a hint for you how to achieve what you expect.