0

I'm using the Types Plugin for Wordpress and created a custom field. I would like to display this custom field in the sidebar of all of my pages. The problem is that it only displays in the sidebar of the page I created the custom field on. It does NOT display in the sidebar on any of the other pages. Here's the code I placed in my generic sidebar.php file:

<?php echo(types_render_field("photo", array("alt"=>"Product image", "width"=>"300","height"=>"200","proportional"=>"true"))); ?>

Any ideas on how to get it to display in the sidebar of ALL the pages? I'm using the Twenty eleven theme with the latest version of Wordpress.

Thanks!

6
  • Did you just add one photo?
    – offroff
    Apr 12, 2012 at 12:50
  • yes. But, this is just one example. No matter what i use as a custom field, text box, text area, etc, it only displays in the sidebar on the page the custom field was created on.
    – Tory
    Apr 12, 2012 at 13:03
  • Did you try to add "photo" on another page as well?
    – offroff
    Apr 12, 2012 at 13:09
  • Yes, and then the photo will appear in the sidebar of that page, as well. But, I don't want to have to add the photo to every page on my site to get it to appear in the sidebar. Or maybe I'm not understanding you. A little more info, I'm using a different page template on each page and the custom field options are specific to each page template. Maybe that has something to do with it??
    – Tory
    Apr 12, 2012 at 13:16
  • Ok, but if you want the same photo on all pages you should add a widget with a photo in it. The function you use are supposed to just show information from the current page
    – offroff
    Apr 12, 2012 at 13:20

3 Answers 3

1

I checked the code in the repository, and this specific function doesn't have an ID parameter, meaning that it only grabs the CF of the current post/page being displayed.

A fast solution would be:

$my_page = get_page_by_path( 'my-page' );
$meta_values = get_post_meta($my_page->ID, 'custom-field-name', true);

Maybe the plugin has another function for that, but you'd better ask in the plugin support page http://wordpress.org/tags/types?forum_id=10

4
  • Thanks for the reply! I added that code to my sidebar.php file and it didn't work. I changed 'my-page' to the page name and replaced'custom-field-name' with the custom field name and nothing appeared at all.
    – Tory
    Apr 12, 2012 at 14:15
  • Hey brasofilo, I put this in my sidebar: <?php $about_page = get_page_by_title('sidebar'); echo "$about_page->post_content"; ?> and I got the custom field shortcode to appear in every sidebar, but not the actual content of the custom field. This what is appearing now: [types field="sidebartitle"][/types] But there is smart quotes which is maybe why it's displaying the short code and not the actual content?
    – Tory
    Apr 12, 2012 at 14:40
  • hi, Tory, I'm not sure if this is what you need echo do_shortcode($about_page->post_content);. . . Did you managed to get the desired custom field content using get_post_meta?
    – brasofilo
    Apr 12, 2012 at 17:42
  • Here's what ended up working for me: $args=array( 'page_id' => 39, 'post_type' => 'page', 'post_status' => 'publish', ); $my_query = null; $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <?php the_content(); endwhile; } wp_reset_query(); // Restore global post data stomped by the_post(). ?> Thanks for your help.
    – Tory
    Apr 12, 2012 at 18:09
1

A dirty solution:

$custom_image = types_render_field("photo", array("alt"=>"Product image","width"=>"300","height"=>"200","proportional"=>"true");
if($custom_image) {
    // this page has a custom photo. Print it.
    print $custom_image;
} else {
   // this page don't have a photo, therefore we output the photo of the default page
   $id = 1; // the id of the default page
   $post = get_post($id);
   setup_postdata($post);
   print types_render_field("photo", array("alt"=>"Product image","width"=>"300","height"=>"200","proportional"=>"true");
   wp_reset_postdata();
}

Or if you really want the same photo everywhere then just use what's in the else clause

1

Not sure since when, but you can pass the post id as an argument

<?php 
$postid = get_the_ID();
echo(types_render_field("photo", array("post_id"=>"$postid","alt"=>"Product image", "width"=>"300","height"=>"200","proportional"=>"true"))); 
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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