2013-12-03 16:33
浏览 36


My understanding of PHP is pretty basic so please forgive me if I don't use the correct terms, please ask for clarification if I do.

My ultimate goal is to create a slider that loads different sets of images depending on the URL, but some of the pages share the same images as others so I'd like to be able to share some images across multiple pages, using PHP to pull in the photos would save a lot of time editing each page seperatly.

I know I can use <?php if (is_page_template('homepage.php')) { ?> in Wordpress to specify an action on a particular page as long as it has a template. But needing lots of template files gets messy and leaves me with my original problem.

So is there something along the lines of <?php if (is_url('example.com/stuff')) { ?> that I could use instead?

And if so, is it possible to make it an array of url's? eg have more than one URL listed per command? I'm not quite sure on the syntax that would be required to do that.

I'll make an example to help you see where I'm going, but the syntax is probably way off.


<div id="products">
 <div class="slides_container">
      <?php if (is_url('example.com/stuff') || ('example.com/other-stuff')) { ?>
      <img src="images/picture.jpg" alt="Pretty Picture" />
      <?php } ?>

Help is greatly appreciated.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drqefnd544707688 2013-12-03 17:10

    I found the solution, on Google with the search "If Post ID"

    <?php if(is_page( array('about-me', 'About Me And Joe' ) ) { ?> <img src="picture.jpg" /> <?php } ?>

    Should do the trick, thank you for your help guys.

    点赞 打赏 评论
  • dsg41888 2013-12-03 16:38

    You can use the $_SERVER variable given to you by PHP itself to determine the current URL, this doesn't taken into account permalinks though (so if you change permalink structure, you should change your 'image select function' as well).

    Other options is to use the function given by wordpress: get_permalink($post->ID). That will suit your case the best I believe. Do something like:

    $current_url = get_permalink($post->ID); // $post should hold the current post/page
    if($current_url == 'my/special/page') {
        // your image select magic
    点赞 打赏 评论

相关推荐 更多相似问题