Forums

[resolved] Calling Custom Field values for specific key. (18 posts)

  1. standoutt
    Member
    Posted 2 weeks ago #

    I hope this isn't complicated, but here is what I'm trying to achieve.

    I have a custom field that image addresses are normally entered into. What I want to do is call the 4 latest values and have those four images displayed in a row. Each image needs to link to the post it came from.

  2. MichaelH
    moderator
    Posted 2 weeks ago #

    One option is to use something like

    $your_cusom_field_key = 'image';
    $posts=query_posts('meta_key='.$your_cusom_field_key.'&showposts=4');

    And then look at some examples of using those Custom Fields in this article:

    http://justintadlock.com/archives/2007/10/27/wordpress-custom-fields-adding-images-to-posts

  3. standoutt
    Member
    Posted 1 week ago #

    It doesn't seem to be doing anything. This is what I inserted into the template:

    <?php $your_cusom_field_key = 'MoviePoster';
    $posts=query_posts('meta_key='.$your_cusom_field_key.'&showposts=4'); ?>

    I forgot to mention that this needs to be on a page and not in a post. I'm not sure if that changes anything or if I'm just doing something wrong.

  4. gbaka
    Member
    Posted 1 week ago #

    all you have to do is place that code in a type of word file and rename it to a php file and upload it then go new page put a title and select the php file from the sidebar and the code should work if u can show me the site i might have a idea of whats going wrong.

  5. pboosten
    Member
    Posted 1 week ago #

    Something like this? All the information you see comes from custom fields.

    I've described it here.

    Peter

  6. standoutt
    Member
    Posted 1 week ago #

    @gbaka Here's the page: http://www.srizzil.com/movies/ The code should be on line 170 but doesn't appear at all when looking at the source of the page. There must be code missing.

    @pboosten What you've done is very similar to what I want to achieve but trying to modify your code continually gives me syntax errors.

    I simply want to pull the poster from the 4 latest reviews and display them on the page. The user then should be able to click on the poster to take them through to the review.

    I don't have much experience so it's very likely I'm doing something wrong.

  7. pboosten
    Member
    Posted 1 week ago #

    A collegue of mine told me that you cannot simply copy&paste my code.

    The fault is in the quotes, and the php tags.

    I'll put both on a site for download:

    page-book-overview.php

    page-book-review.php

  8. standoutt
    Member
    Posted 1 week ago #

    Thanks @pboosten. That's solved the syntax errors but now it's doing the same thing as the other code. It's just not there when the page renders. If you look at the source code for the above link your code should be on line 177.

  9. pboosten
    Member
    Posted 1 week ago #

    Hmmm, I gotta feeling you don't understand the concept of php: it's the webserver translating that into something that can be read by the browser.

    The result is never source code, but xhtml.

    I do some queries through the posts, I presume you have some code above mine, right?

    In that case you need to reset the query just before you start 'mine' with wp_reset_query();.

    Peter

  10. standoutt
    Member
    Posted 1 week ago #

    I think I understand that concept, but surely there should be some xhtml on that line from the code. At this point there is nothing.

    I do, here's what I tried to no avail:

    <TD VALIGN="TOP" width="800" BG="#ffffff">
    <?php wp_reset_query(); ?>
    <?php if ( (is_page('Movies')) ){
    
    			query_posts(array('showposts' => 8, 'post_parent' => 949, 'post_type' => 'page'));
    
    		  while (have_posts()) {
    
    				the_post(); // vital
    
    				$thumbPath = get_post_meta($post->ID, 'MoviePoster', true);
    
    				$thumbPath = "" . $thumbPath;
    		?>
    
    				<a href="<?php the_permalink(); ?>" class="grid-book">
    					<img src="<?php echo $thumbPath ?>" alt="" /><br />
    					<span class="grid-title"><?php the_title(); ?></span><br />
    				</a>
    
    			<?php }
    
    		}
    		?>
    
    </td></tr></tbody></table>
  11. pboosten
    Member
    Posted 1 week ago #

    The td tag should be lower case, but that's not the issue.

    Just to be sure: you're searching for the Custom Field 'MoviePoster' in the first 8 pages (not posts), which are subpages of page ID 949 that you encounter?

    Peter

  12. standoutt
    Member
    Posted 1 week ago #

    The reviews are posts. However this is being displayed on a page. I want the 8 latest reviews to have their posters show up and link to that particular post from this page.

  13. pboosten
    Member
    Posted 1 week ago #

    Then you need to change your query:

    query_posts(array('showposts' => 8, 'post_parent' => 949, 'post_type' => 'page'));

    this query searches for 8 sub pages of post 949.

    try this:

    query_posts(array('showposts' => 8, 'post_type' => 'post'));

    Peter

  14. standoutt
    Member
    Posted 1 week ago #

    Yes! Thank-you! Thank-you! Thank-you!

    That's exactly what I need. You're a genius!

  15. pboosten
    Member
    Posted 1 week ago #

    nah, I just wanted to prove that the code in my article worked :-)

    Peter

  16. standoutt
    Member
    Posted 1 week ago #

    All I need now is to limit it to a certain category.

  17. pboosten
    Member
    Posted 1 week ago #

    query_posts(array('showposts' => 8, 'post_type' => 'post', 'cat' => 4));

    query_posts in the codex

    Peter

  18. standoutt
    Member
    Posted 1 week ago #

    Yet again, brilliant!

    Thank-you so much. The page looks great: http://www.srizzil.com/movies/

Reply

You must log in to post.

About this Topic