WordPress.org

Ready to get started?Download WordPress

Forums

Slug-dependent PHP (4 posts)

  1. walibby
    Member
    Posted 1 year ago #

    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.

  2. Pioneer Valley Web Design
    Member
    Posted 1 year ago #

    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...

  3. walibby
    Member
    Posted 1 year ago #

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

  4. walibby
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic