WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying custom fields on posts page (33 posts)

  1. mitchrenton
    Member
    Posted 5 years ago #

    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

  2. Xamataca
    Member
    Posted 5 years ago #

    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.

  3. mitchrenton
    Member
    Posted 5 years ago #

    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

  4. Mark / t31os
    Moderator
    Posted 5 years ago #

    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/

  5. mitchrenton
    Member
    Posted 5 years ago #

    Thanks for that, here's the code

    http://wordpress.pastebin.com/m7954629

  6. Mark / t31os
    Moderator
    Posted 5 years ago #

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

  7. Mark / t31os
    Moderator
    Posted 5 years ago #

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

  8. mitchrenton
    Member
    Posted 5 years ago #

    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

  9. Mark / t31os
    Moderator
    Posted 5 years ago #

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

  10. mitchrenton
    Member
    Posted 5 years ago #

    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?

  11. Mark / t31os
    Moderator
    Posted 5 years ago #

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

  12. mitchrenton
    Member
    Posted 5 years ago #

    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.

  13. Mark / t31os
    Moderator
    Posted 5 years ago #

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

  14. mitchrenton
    Member
    Posted 5 years ago #

    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

  15. Mark / t31os
    Moderator
    Posted 5 years ago #

    Ok, looks good, field names do match...

    Are you sure the right image field exists for whatever page you're pulling up?...

  16. Mark / t31os
    Moderator
    Posted 5 years ago #

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

  17. mitchrenton
    Member
    Posted 5 years ago #

    There must be something effecting this code somewhere. I tried the code without excluding category 5. Very strange. I'll try and re-create it myself somewhere for you to look at. Thanks again

  18. Mark / t31os
    Moderator
    Posted 5 years ago #

    No problem, i'd love to be more help, but as said, i couldn't re-create the issue you described.

    Let me know how it goes.. :)

  19. mitchrenton
    Member
    Posted 5 years ago #

    Ok, so I re-created the code on a fresh wordpress install and they worked fine. Then I remembered that the page on the original site that had the problems was the posts page. So I changed the home page to static (on my fresh wordpress install) and the posts page to my test page and the custom fields weren't working.

    So I now know why, but I don't actually know why! If that makes sense!?

    Is there any reason that the posts page ignores custom fields?

  20. Mark / t31os
    Moderator
    Posted 5 years ago #

    Where are you placing this code, what file?

    Also do note, that using the static page setting means setting both Front Page and Posts Page settings..

    http://codex.wordpress.org/Settings_Reading_SubPanel

    It's not very well explained and from what i can tell setting a Posts Page alone doesn't work as i'd expect... but then the above page says you need to set both if you choose the Static page option.

    Also, when it refers to pages (ie. the choices in the drop downs) it only refers to regular pages created in the admin (not files in your theme), though that's not to say you can't attach a page template to any of those given pages..

    When you set a static page for posts, you're effectively saying "Use this page as a placeholder/framework" when displaying posts, so any password on that page will just be ignored, since it's now used for handling "post" content and not the actual "page" you created, under "Pages".

    Have a read of the info on the link..

    I'll have to say at this point i don't think the above codex page is very clear on the exact behaviour of these options.

  21. mitchrenton
    Member
    Posted 5 years ago #

    I'm placing the code in the index.php file. At the moment I am hard-coding the pull quote in <p> tags whilst I find a reason/solution for the custom fields not pulling the data. In my page templates the exact same code pulls the data perfectly, just not in the index.php file. I'll have a read through. Thanks for that

  22. mitchrenton
    Member
    Posted 5 years ago #

    Its not the static page that's the problem. Its the posts page (index.php)

  23. Mark / t31os
    Moderator
    Posted 5 years ago #

    So are you using the static page setting or not? Sorry a little confused...

    Let's clear this up a little...

    If you're using the static page option, which pages are you selecting from the drop-down boxes?

    If you're not using the static page option, then are you wanting the index.php to handle all the previously discussed code?

    Could you also give me a list of the template files (anything with a .php extension only) that you have in your theme folder...

    :)

  24. mitchrenton
    Member
    Posted 5 years ago #

    Basically, I am using a static home page which is called "home" which is a template file called home.php. Then I am using case-studies as my posts page. The case-studies page is powered by the index.php file. This is the page that isn't pulling the data.

    So...

    Front page displays: * A static page

    Front page: HOME
    Posts page: CASE STUDIES

    Does that make sense now?

  25. Mark / t31os
    Moderator
    Posted 5 years ago #

    Previous link i gave had this to say in regard to the front page, this applies to you i think..

    # Front page - Select in the drop-down box the actual Page that you want displayed as your front page. If you do not select a choice here, then effectively your blog will show your posts on both the blog's front page and on the Posts page you specify. If you would like to create a static home page template file, do not name it home.php, otherwise you will encounter problems when you try to view the "blog"/"posts" section of your site. To get around this, just name it anything but home.php, for example, myhome.php

    It recommends against using home.php as the front page.

    If case studies is a page, then that's handled by your page.php unless one doesn't exist, in which case you are correct that's index.php...

    http://codex.wordpress.org/File:Template_Hierarchy.png

    I'd say start with changing that home.php to something else and see if there's any improvement...

    If that's still no help, i'll try and replicate your setup and see if i can duplicate the issue...

  26. mitchrenton
    Member
    Posted 5 years ago #

    Apologies for that last message, the file with regards to the home page is called page-home.php.

    The page is definitely powered by the index.php

    I would really appreciate if you could try and figure something out. I'll have a think myself

  27. Mark / t31os
    Moderator
    Posted 5 years ago #

    Yeah no problem, i can try to replicate it...

    Can you provide me the code from those 2 files, so i can test them "as is"...

    Save scrolling up..
    http://wordpress.pastebin.com/

    I can create any additional fields or categories required, i'd like to just ensure i'm using exactly the same code..

    Have to take a shower, have dinner etc... but if you post the code i'll take a look as soon as i sit back down infront of the PC... ;)

  28. mitchrenton
    Member
    Posted 5 years ago #

    Hey, sorry for the delay getting back to you. I've used your code for the index.php file (Way better!) which you can find here: http://wordpress.pastebin.com/m4f6dd9c3

    I've pasted my page.php file too, this has my old code (I havent updated it yet.) That can be found here: http://wordpress.pastebin.com/m7730e227

  29. mitchrenton
    Member
    Posted 5 years ago #

    I've also posted my page-home.php file here:

    http://wordpress.pastebin.com/m418f9cf

  30. Mark / t31os
    Moderator
    Posted 5 years ago #

    I'll let you know how i get on... just wanted to let you know i've not missed your reply...

Topic Closed

This topic has been closed to new replies.

About this Topic