WordPress.org

Ready to get started?Download WordPress

Forums

Different picture on different pages using custom fields? (8 posts)

  1. vincepettit
    Member
    Posted 5 years ago #

    Hi

    I have website using wordpress as a CMS system and I have picture fixed into the template on the left hand side of the page that I assign using a custom field.

    I was wondering, when I use the <!--nextpage--> tag it will create a second page, but obviously the pic on the left is the same as on the first one.

    I was wondering if there was a way around it so I could also assign a second image in my custom fields so when people click on page 2 it switches the picture?

    I know I could embed the pictures in each article as I go along but I want it the way I've described above so the template layout stays exactly the same.

  2. vincepettit
    Member
    Posted 5 years ago #

    Anyone?

  3. vincepettit
    Member
    Posted 5 years ago #

    So NOBODY has any thoughts or even opinions on this?

  4. MichaelH
    Member
    Posted 5 years ago #

    $page = (get_query_var('page')) ? get_query_var('page') : 1;
    if ($page == 1) {
    //do your page 1 image thing
    } elseif ($page == 2) {
    //do your page 2 image thing
    } else {
    //do whatever you need
    }

    Related:
    Pages
    http://justintadlock.com/archives/2007/10/27/wordpress-custom-fields-adding-images-to-posts
    Using Custom Fields
    http://us2.php.net/manual/en/control-structures.elseif.php

  5. vincepettit
    Member
    Posted 5 years ago #

    Thanks for that, sorry for sounding dumb but how do I implement that with the code I use...

    <?php $thereisimage = get_post_meta($post->ID, image, true);
    if($thereisimage){ ?>
    	<img src="<?php echo get_post_meta($post->ID, image, true); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"/>
    <?php }else{ ?>
    	<img src="<?php bloginfo('url'); ?>/default.jpg" alt="Default Image" />
    <?php } ?>
  6. RedElement
    Member
    Posted 5 years ago #

    do you want a different image for every post or every page?

  7. vincepettit
    Member
    Posted 5 years ago #

    different page of the post... for example I have a post that is 5 pages long, I want to use my custom field to put in the image and then have image 1 display on page 1, image 2 on page 2 etc...

  8. RedElement
    Member
    Posted 4 years ago #

    I worked up some quick code so it may be buggy as I haven't tested it. You'll have to use the custom fields to set the images by page number.

    <?php
       $thereisimage = get_post_meta($post->ID, image, true);
       $page = (get_query_var('page')) ? get_query_var('page') : 1;
    
       if ($page == 1) { ?>
          <img src="<?php echo get_post_meta($post->ID, image1, true); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"/>
       <?php } elseif ($page == 2) { ?>
          <img src="<?php echo get_post_meta($post->ID, image2, true); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"/>
       <?php } elseif { ?>
          <img src="<?php echo get_post_meta($post->ID, image3, true); ?>" alt="<?php the_title(); ?>" title="<?php the_title(); ?>"/>
       <?php } else { ?>
          <img src="<?php bloginfo('url'); ?>/default.jpg" alt="Default Image" />
    <?php } ?>

    To explain, you set you're thereisimage variable the the page variable. If the user is on page 1 it displays the image with the image1 key, if page two the image2 key and so on and so forth until it runs out of pages or keys, then it displays the default code.

    Hope this helps.

Topic Closed

This topic has been closed to new replies.

About this Topic