• Resolved jamiedustuk

    (@jamiedustuk)


    Hi, I am not sure if this is an issue with the plug in or just the custom fields but seeing as how im using the plug in, I though I better post it in here…

    basically I have a custom field to add content to a right hand column, the code used to display this in my templates is:

    <?php $right_images = get_post_meta($post->ID, 'right', true); ?>
    <?php echo $right_images; ?>

    when i change the content in this field for other pages within the same category they dont change, it seems to be picking up content from just one of my posts and showing that, whatever…

    any ideas?

    thanks

Viewing 15 replies - 1 through 15 (of 15 total)
  • Since you are using the $post->ID variable, you should have some kind of post loop in operation, correct?

    If necessary, copy and past the code from your template into a pastebin and report the link back here. Maybe someone can spot a problem…

    Thread Starter jamiedustuk

    (@jamiedustuk)

    i havent touched the post loop so it wil just be the standard wordpress loop, dont know what a ‘pastebin’ is but here is my code…

    thanks.

    <div id="page">
    	<?php if (have_posts()) : ?>
    		<?php while (have_posts()) : the_post(); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    <h2><?php the_title(); ?></h2>
    
    	<table id="contenttable"><tr><td>
    
    <div class="entry">
        <?php the_content('Read the rest of this entry &raquo;'); ?>
    </div>
    
    <small><?php the_time('F jS, Y') ?>  by <?php the_author() ?></small>							
    
    		<?php endwhile; ?>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    
     <?php endif; ?>	
    
    </td><td class="sidebarcell">
    
    <ul id="postlist">
     <?php
     global $post;
     $myposts = get_posts('numberposts=5&amp;offset=0&amp;category=11');
     foreach($myposts as $post) :
     ?>
    <li><span style="font-size:0.9em;font-weight:bold;">&raquo;&nbsp;&nbsp;</span><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
     <?php endforeach; ?>
     </ul>
    
    <?php $right_images = get_post_meta($post->ID, 'right', true); ?>
    <?php echo $right_images; ?>
    </td></tr></table><!--end sidebar-->
    </div>

    Two lines above this code:

    <?php $right_images = get_post_meta($post->ID, 'right', true); ?>

    you have a endforeach that ends your 2nd loop.

    Wouldn’t you want that get_post_meta INSIDE one of your two loops?

    Please note I deleted the other thread you started with this same topic.

    Thread Starter jamiedustuk

    (@jamiedustuk)

    aahhh, thanks…

    it seems whats happening is that when my ‘for each’ loop is ended the custom field breaks and only shows the one field for all pages, it works when it comes before my ‘for each’ loop starts but then its in the wrong place in the page, i cant put it in the for each loop because i then get all custom fields showing…

    any idea how i can make it work, after the for each, do i need to start a new loop? you can probably tell im not a php programmer… the only other way to do it is to arrange the elements on the page with css, i will have a play for now, thanks

    You have three loops, and if you want the custom fields for each post in each of those loops, then you will need the code,

    <?php $right_images = get_post_meta($post->ID, 'right', true); ?>
    <?php echo $right_images; ?>

    within each of those loops.

    Thread Starter jamiedustuk

    (@jamiedustuk)

    ok i have the loops sorted, the problem seems to be, im using this code:

    <?php
     global $post;
     $myposts = get_posts('numberposts=50&amp;offset=0&amp;category=13');
     foreach($myposts as $post) :
     ?>
    <li><span style="font-size:0.9em;font-weight:bold;">&raquo;&nbsp;&nbsp;</span><a href="<?php 
    
    the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    to output all posts in my category and when this comes ‘before’ my calling for custom field it

    breaks the custom field, if it comes after its ok, so it must be something in this…

    any ideas?

    thanks

    I’d delete the $global post statement

    and you need a setup_postdata($post); after the foreach

    Review:
    http://www.daobydesign.com/blog/2007/07/modifying-wordpress-front-page/

    Thread Starter jamiedustuk

    (@jamiedustuk)

    hmmm…. i did that, didnt make any difference, i then tried changing the category=13 in my post list to another category number and my custom field now outputs the fields from that category! so its definately being effected by this, my code now reads:

    it works when my custom field output is ‘before’ the postlist but not after

    `<?php
    $myposts = get_posts(‘numberposts=50&offset=0&category=13’);
    foreach($myposts as $post) :
    ?>

    • <span style=”font-size:0.9em;font-weight:bold;”>»  </span>“><?php the_title(); ?>
    • <?php endforeach;?>
      </div>

      <?php setup_postdata($post);
      $right_images = get_post_meta($post->ID, ‘right’, true); ?>
      <?php echo $right_images; ?>`

    Should be:

    foreach($myposts as $post) :
      setup_postdata....
      ///your other stuff
      $right_images = get_post_meta....
      echo $right_images
    endforeach

    Thread Starter jamiedustuk

    (@jamiedustuk)

    no it doesnt work if i put my custom field (right_images) within the ‘foreach’ loop as it outputs all the custom fields for posts in that category, all i want is on each single post page, the custom field for that post is displayed underneath a list displaying all the posts in that category (and links)

    all i want is on each single post page, the custom field for that post is displayed underneath a list displaying all the posts in that category (and links)

    For which post do you want to display the custom fields?

    In your loop, put an assignment

    $mypostid=$post->ID;

    Then in your custom field ‘get’, this:

    $right_images = get_post_meta($mypostid, ‘right’, true); ?>

    Thread Starter jamiedustuk

    (@jamiedustuk)

    Nah, its not happening mate, it already had $post->ID in anyway doesnt make any difference.

    there must be some straight forward reason why calling for and outputting all posts in a category breaks anything else that comes after it, none of the names used are the same and ive tried it within the while and if loops and out of the loop, ive tried it in the foreach loop also

    i think its time to give up unless anyone has any more ideas.

    is there a better way to output a list of all the posts in the current category, complete with links?

    is there a better way to output a list of all the posts in the current category, complete with links?

    Some Related Posts plugins do that.

    Also will submit this to display posts belonging to the categories of the current post without duplicating the present post:

    <?php
    global $post;
    $cat_ID=array();
    $categories = get_the_category(); //get all categories for this post
      foreach($categories as $category) {
        array_push($cat_ID,$category->cat_ID);
      }
      $args = array(
      'orderby' => 'date',
      'order' => 'DESC',
    	'post_type' => 'post',
    	'numberposts' => 8,
    	'post__not_in' => array($post->ID),
    	'category__in' => $cat_ID
      ); // post__not_in will exclude the post we are displaying
        $cat_posts = get_posts($args);
        $out='';
          foreach($cat_posts as $cat_post) {
              $out .= '<li>';
              $out .=  '<a href="'.get_permalink($cat_post->ID).'" title="'.wptexturize($cat_post->post_title).'">'.wptexturize($cat_post->post_title).'</a></li>';
          }
        $out = '<ul class="cat_post">' . $out . '</ul>';
        echo $out;
    ?>

    Thread Starter jamiedustuk

    (@jamiedustuk)

    thanks, that seems to do the trick, id actually just solved it myself, i looked up the ‘List category posts’ plug in wich almost does what i wanted and found this snippet wich works aswell, thanks for your help

    <?php
    $result = "<ul>";
    $catposts = get_posts('category=13');
    foreach($catposts as $single):
    	$result .= '<li><span style="font-size:0.9em;font-weight:bold;">&raquo;&nbsp;&nbsp;</span><a href=';
    	$result.=get_permalink($single->ID).'>'.$single->post_title.'</a><br />';
    	//This displays the title for the post and an end of line (br tag)
    	$result .= $single->the_excerpt."\n".'</li>';
    	//This displays the first 55 words of the post's content [the_excerpt() function]
    endforeach;
    $result .= "</ul>";
    echo $result;
     ?>
Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Custom Fields all posts show same content’ is closed to new replies.