WordPress.org

Support

Support » How-To and Troubleshooting » Slug-dependent PHP

Slug-dependent PHP

  • I’m trying to include an image that will change depending on the page slug. I’d like this to populate across pages, categories, and posts. What I’m trying to say is this:

    if page slug = main, use main.jpg
    if page slug = contact, use contact.jpg
    if category slug = media, use media.jpg
    if category slug = news, use news.jpg
    if none of these is true (i.e. is a post), use main.jpg

    I sadly don’t know the php well enough to improvise. The code I’m currently using works great for pages, but not posts or categories. It looks like this:

    <img src="<?php bloginfo('template_url'); ?>/img/<?php echo $post->post_name; ?>.jpg" id="paper" alt="">

    The page in question is annawaldron.com.

    Please let me know if you have any thoughts. Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Pioneer Web Design
    Participant

    @swansonphotos

    Have a look at Page Templates…you can use them to customize the whole look of the pages/posts/categories (including your custom CSS)…

    http://codex.wordpress.org/Category_Templates

    http://codex.wordpress.org/Pages#Page_Templates

    Many themes provide unique CSS for pages/posts/categories and even unique id’s per post-id…

    It’s not customizing the page titles that’s the problem, it’s calling linking the image source to the page/category/whatever.

    I’ve solved the problem using this:

    ‘<?php if (in_category(‘photos’)) { ?>
    <img src=”<?php bloginfo(‘template_url’); ?>/img/photos.jpg” id=”paper” alt=””>

    <?php } elseif (in_category(‘media’)) { ?>
    <img src=”<?php bloginfo(‘template_url’); ?>/img/media.jpg” id=”paper” alt=””>

    <?php } elseif (in_category(‘press’)) { ?>
    <img src=”<?php bloginfo(‘template_url’); ?>/img/press.jpg” id=”paper” alt=””>

    <?php } elseif (in_category(‘news’)) { ?>
    <img src=”<?php bloginfo(‘template_url’); ?>/img/news.jpg” id=”paper” alt=””>

    <?php } elseif (is_page(‘main’)) { ?>
    <img src=”<?php bloginfo(‘template_url’); ?>/img/main.jpg” id=”paper” alt=””>

    <?php } elseif (is_page(‘resume’)) { ?>
    <img src=”<?php bloginfo(‘template_url’); ?>/img/resume.jpg” id=”paper” alt=””>

    <?php } elseif (is_page(‘contactinfo’)) { ?>
    <img src=”<?php bloginfo(‘template_url’); ?>/img/contactinfo.jpg” id=”paper” alt=””>

    <?php } else { ?>
    <img src=”<?php bloginfo(‘template_url’); ?>/img/main.jpg” id=”paper” alt=””>

    <?php } ?>’

    It’s not elegant, but it gets the job done.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Slug-dependent PHP’ is closed to new replies.