Support » Fixing WordPress » Displaying custom fields on posts page

  • Resolved mitchrenton

    (@mitchrenton)


    Hey guys, I’m having a problem with displaying a couple of custom fields in a custom theme. I’m displaying 2 custom fields on my page.php template and they are working fine, one spits out some text the an image. It works fine on any page that I create but my posts page for some reason doesn’t display them at all. Does anybody know why this is?

    Thanks

    Mitch

Viewing 15 replies - 1 through 15 (of 32 total)
  • probably you need to edit your:
    index.php or/and single.php templates adding the bit of code you use on your page.php template to ouput the custom fields values.

    I’ve used the exact same code in the exact same position in my index.php that I did in my page.php. But for some reason it isn’t displaying in index.php

    Can you plonk the code from your single.php or index.php (if the single.php doesn’t exist) into a pastebin and post the link..

    You just need to make sure the code to grab the post meta is correctly placed in relation to the post loop.

    I’ll (or Xamataca) take a look if you’d like to whack the code in a bin… 🙂
    http://wordpress.pastebin.com/

    Thanks for that, here’s the code

    http://wordpress.pastebin.com/m7954629

    Ok just taking a peak for you now, and i’ve spotted quite a few errors…

    I’m going to make the code a little more failsafe for you and post it back shortly…

    I’m adding some comments in the code to make it easier to use (if you need it, you can always remove them).

    Will post a link back for you shortly…

    Ok i’ve worked on the upper areas of the file…

    http://wordpress.pastebin.com/m70bef4dd

    I tried to put comments around the main areas i’ve modifed so as to make things obvious..

    Let me know how that goes, and if all is well i’ll work on the bottom bit for you to..

    mitchrenton

    (@mitchrenton)

    Hi t31os_

    Thank you very much for the much improved code there. Looks far more robust. Works great. If you wouldn’t mind taking a look at the “right-sidebar” section I would be most grateful.

    Thanks again,

    Mitch

    Ok, here you go..
    http://wordpress.pastebin.com/m5e127ced

    There is one thing i’d query though, and that’s in relation to the image title custom field, it appears both areas call the same custom field…

    You can set a seperate one for the right image if you want, but right now it uses the same title as the images in the first sidebar.

    Let me know how it goes, we can always add or tweak the code if need be…

    mitchrenton

    (@mitchrenton)

    That code looks solid to me but for some reason none of the info appears in the right-sidebar. No image, image title or pull quote. The posts appear fine.

    The reason I used image title for 2 different areas is because 1 of them is within posts and the other is within a page. Therefore there isn’t a clash. For example, my posts are displayed on the home page, so I’ve used custom fields in the Home Page to display information on the right sidebar, whilst the main content area contains the posts.

    I’m thinking now though that image title could be the problem. What are your thoughts?

    Yes but essentially both are in the same loop, so they look for a custom field from the same post/page…

    There’s nothing in the above code to discern the field from one page to another…

    Did you exclude some code from what was posted?

    Code assumes you have a field called “Right Image”, for the right image to appear, do you have one with that name? If so, does it have a value for this given page?

    EDIT: no thinking about it, this is a loop for page>s<, not singular, so it loops over for each page… you mentioned posts though… but i think the issue relates to whether the field exists and has a value..

    ** Might need to actually plonk the code in a theme to test, i’m just doing this without testing thus far **..

    mitchrenton

    (@mitchrenton)

    Yes there is a key and value for both the right image and the pull quote but both divs are empty. I changed one of the image title fields to right image title but still no joy.

    I really appreciate you helping out.

    Ok, but character case is important here, do the fields match the case, “Right Image” as appose to “right image”, “rightimage” and so on….

    I think it might help if we can see which fields currently exist on your install, so take the code below and place it somewhere in your theme. You’ll want to take out it afterwards, but just plonk it in for a minute and copy the values it prints out..

    For example: In the header.php is fine..

    <?php
    function get_meta_keys() {
    	global $wpdb;
    
    	$ignore1 = '_edit_last';
    	$ignore2 = '_edit_lock';
    	$ignore3 = '_wp_page_template';
    	$ignore4 = '_wp_attached_file';
    	$ignore5 = '_wp_attachment_metadata';
    
    	$keys = $wpdb->get_col("SELECT meta_key FROM $wpdb->postmeta");
    
    	foreach ( $keys as $field ) {
    		if(
    			$field !== $ignore1 &&
    			$field !== $ignore2 &&
    			$field !== $ignore3 &&
    			$field !== $ignore4 &&
    			$field !== $ignore5) {
    				echo $field.'<br />';
    			}
    	}
    }
    get_meta_keys();
    ?>

    I’ve tried to catch the fields we don’t need with the ignore bits to avoid a huge list….

    You should just get a list of text, this is a list of the custom field names that currently exist on your install…

    Alternatively you could just click “Add new” (post) and write down each field name listed in the custom field drop-down box… 🙂 directly from the admin (bit easier)…

    mitchrenton

    (@mitchrenton)

    Here is the output from your code…

    Header Image
    Header Image
    Header Image
    Header Image
    Header Image
    Image
    Image
    Image
    Image
    Image Link
    Image Link
    Image Link
    Image Title
    Image Title
    Image Title
    Image Title
    Image Title
    Image Title
    Image Title
    Image Title
    Image Title
    Pull Quote
    Pull Quote
    Pull Quote
    Pull Quote
    Pull Quote
    Pull Quote
    Right Image
    Right Image
    Right Image
    Right Image
    Right Image
    Right Image
    _links_to
    _links_to
    _links_to
    _links_to_target

    Ok, looks good, field names do match…

    Are you sure the right image field exists for whatever page you’re pulling up?…

    Just took the code posted previously, plonked it in the theme page.php, created a test page with the necssary fields..

    Snippet of the view source result..

    <div id="body_left_content">
    
    	<div id="content" class="narrowcolumn">
    
    	<div class="case-study-img">
    				<a target="_blank" href="aaaa">
    			<img title="bbbb" alt="bbbb" src="http://localhost/wp-content/themes/tweetsheep/js/timthumb.php?src=http://localhost/wp-content/uploads/1.jpg&h=125&w=125&zc=1">
    		</a>
    			</div>
    
    	<div class="case-study-post" id="post-225">
    
    		<h2 class="sifr">testpage</h2>
    		<h5>Blog</h5>
    		<p>testddawdawdwaudkhu hduoawh duiawhduawhd</p>
    	</div>
    	<!-- divPost -->
    
    </div>
    <div id="right-sidebar">
    
    		<div id="right-image">
    		<img title="bbbb" src="http://localhost/wp-content/themes/tweetsheep/js/timthumb.php?src=http://localhost/wp-content/uploads/aa.jpg&h=270&w=184&zc=1" alt="bbbb">
    		<p>bbbb</p>	</div>
    		<!-- divRight-image -->
    
    	<div class="rounded-top"></div>
    	<!-- divRounded-top -->
    
    			<div id="pull-quote"><h4 class="sifr">31231</h4></div>
    
    		<!-- divPull-quote -->
    	</div>
    
    							</div>

    You can see the right sidebar code has been output, so behaviour is as expected…

    I didn’t include this line in my testing however…
    <?php if (in_category('5')) continue; ?>
    … just as a side note…

Viewing 15 replies - 1 through 15 (of 32 total)
  • The topic ‘Displaying custom fields on posts page’ is closed to new replies.