0

I created a custom field called 'Page Description' which I use to create a custom title for each static page of my wordpress site. I use the code:

<?php echo get_post_meta(get_the_ID(),'Page Description',true); ?>

in my header.php.

it displays in all of my pages except for my blog page. Can someone please help me figure this out? thanks.

you can view the website here if it helps. www.tenspeedgreens.com

  • 1
    In what template file (edit: header.php) did you add this code, and where in that template file (and in relation to the Loop) did you add this code? Also: what debugging information do you have. What error messages, if any, are you getting? – Chip Bennett Mar 25 '13 at 16:57
  • I added it in header.php in a div that i created. The idea is that on each page it will display a custom title description. I do not have any debugging information. sorry. It just seems to happen on whatever static page I set as my post page under settings>reading>post page – sharkers Mar 25 '13 at 17:21
1

Try using:

<?php
// Determine context
$page_id = ( 'page' == get_option( 'show_on_front' ) ? get_option( 'page_for_posts' ) : get_the_ID );
// Echo post meta for $page_id
echo get_post_meta( $page_id, 'Page Description', true ); 
?>

What's happening:

  1. You're using a static page as front page, and a static page to display the blog posts index
  2. You've added your post custom metadata (i.e. custom field) to a Static Page, and then assigned that page to display the blog posts index
  3. In that scenario, WordPress uses either home.php or index.php (in that order, per the Template Hierarchy), to render the blog posts index. WordPress will never use page.php or any custom page template to display the blog posts index.
  4. In either home.php or index.php, WordPress makes no reference to ID of the page assigned to display the blog posts index.
  5. Thus, if you want to reference data associated with that page ID, you need to query it directly
  6. Enter get_option( 'page_for_posts' ), which returns the ID of the page assigned to display the blog posts index.

Note: since you've put this code in your header.php, it's going to output in every template file; so you'll want to add some fail-safes.

Edit

Example fail-safes:

  1. To display just on the blog posts index, wrap the whole thing in:

    if ( is_home() ) {}
    
  2. To display on static pages and the blog posts index, wrap the whole thing in

    if ( is_home() || is_page() ) {}
    
| improve this answer | |
  • thanks chip. this allowed there to display the custom field on all pages, but like you said, it now displays the value i have set for the 'blog' page on every page. pardon this question if it sounds too simple, but how do i add the 'failsafes' you referenced? – sharkers Mar 25 '13 at 18:07
  • this could help you: if( is_page('blog') ) { /* ... */ } – montrealist Mar 25 '13 at 18:21
  • you guys have been a great help. Im definitely making headway on figuring this out. Im just fairly new to web design, coding and wordpress in particular. I guess my lack of basic knowledge is holding me back. – sharkers Mar 25 '13 at 18:36
  • See updated answer for some example "fail safe" wrappers. – Chip Bennett Mar 25 '13 at 19:24
  • Chip! that worked! thank you so much for taking time out of your day to help with such a noob issue. Thank you! thank you! thank YOU! – sharkers Mar 25 '13 at 19:39
2

Well, your blog page is not one particular post/page, it is rather a template which populates a list of posts, and as such, it cannot have any custom fields associated to it.

Where exactly did you input the custom field value that you expected would show up on that page?

Edit: Your description is in the header, so you can simply:

  1. Create a standard page called 'Blog'. Then go to wp-admin -> Settings -> Reading, and change the 'Posts page' value to your 'Blog' page. You may need to adjust your menu to reflect this change.

  2. Create a custom template for your Blog page. It should pretty much be a copy-paste from index.php and content.php, if your theme is similar to the Twentytwelve theme. If it isn't then you'll need to investigate which template is responsible for populating your list of posts. Codex has many useful guides, most useful for you in this case is probably Pages.

  3. Edit your Blog page from #1 to make it use your new template from #2 (Edit Page -> Page Attributes -> Template drop-down).

| improve this answer | |
  • I just put a page description. If you look at the website for example, on the home page it says "welcome to the farm" and the events page says "whats going on". For the blog page, i want it to say "on our mind" I entered the information on the 'create custom field' section on the dashboard in the 'value' section. how can i rectify this issue? – sharkers Mar 25 '13 at 16:25
  • @sharkers just edited my answer, check it out. – montrealist Mar 25 '13 at 16:54
  • thanks dalbaeb. I do have the 'blog' page set as a standard page and it is set as the value for my 'posts' page. the template i am using for the blog page is 'default template'. The exact same as all my other pages. The issue seems to be from setting it as a posts page. i changed that value to a different page and it ended in the same result. no custom field display on that page. will making a new template help if its an exact copy? – sharkers Mar 25 '13 at 17:12
  • @sharkers You mean when you set another page as the Posts page, you still don't get the custom field showing up? Even if it's your "About" page? – montrealist Mar 25 '13 at 17:44
  • That is correct. Whatever page i set as the posts page. the custom field that normally displays wont. and im left with an empty div. – sharkers Mar 25 '13 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.