开发者

Passing data between Wordpress 'pages'

I have a menu, when the user clicks on any menu item, I want to take them to the same page, but I want to style the page differently depending on the menu item...for ex: change the background color to be differ开发者_运维百科ent. What's the best way to do this? I was thinking that when they click on a menu item, a variable can be set or a session variable can be set.

Is there an easier way to do this? Does Wordpress have anything built into the architecture to make this easy?


Don't use the session for this. Since HTTP is a stateless protocol, you should avoid using session to the greatest lengths possible. Since the user is clicking on a link, it only makes sense making that link convey the information you're after. Just pass any data you want in the URL, e.g. as a a query string parameter:

  1. http://example.com/page_x?bgcolor=1
  2. http://example.com/page_x?bgcolor=2
  3. http://example.com/page_x?bgcolor=3

Then you can just query for this in the page template like so:

<?php
  /*
  Template Name: Varying Background Color Template
  */

  $bgcolor = $_GET['bgcolor'];

  switch ($bgcolor) {
    case 1:
      // Change the background to color 1
      break;

    case 2:
      // Change the background to color 2
      break;

    case 3:
      // Change the background to color 3
      break;
  }
?>

If you think query string parameters are ugly, you can use path info instead, but this requires you to create a special case rewrite rule that makes the path info "invisible" to WordPress so it doesn't think it's the slug of a page. Whether you want or need this depends on how your permalink configuration in WordPress is.


  1. you have to create your page as a wp template ( if you did not created already ) ( see this )
  2. you are right with the session variable. pass the variable to that page and process it there. there is a bit of programming involved...

I do not know of another way for doing what you ask.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜