How do I add a custom field to a sidebar?
-
I would like to be able to have unique pictures in the sidebar of several pages on my site.
I have discovered that I could possible use custom fields to do this. I have read that I can add code to the sidebar php (in my case I am using a child theme of Travelify and wish to add the custom field to sidebar-right.php)
The code I found is:
<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'customField', true); ?>
Further investigation told me that I should modify ‘customField’ to something more meaningful, so I chose ‘pageImage’. I added this to the end of the sidebar-right.php code.
On my page, I added the new custom field: “pageImage” and gave it a value of the URL of the required image stored in my media.
On viewing the page, in the place of the sidebar is the URL of the image. I feel like I’m halfway there, but need some expertise to complete the task!
This is what my sidebar-right.php looks like:
<?php /** * Displays the right sidebar of the theme. * */ ?> <?php /** * travelify_before_right_sidebar */ do_action( 'travelify_before_right_sidebar' ); ?> <?php /** * travelify_right_sidebar hook * * HOOKED_FUNCTION_NAME PRIORITY * * travelify_display_right_sidebar 10 */ do_action( 'travelify_right_sidebar' ); ?> <?php /** * travelify_after_right_sidebar */ do_action( 'travelify_after_right_sidebar' ); ?> <?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'pageImage', true); ?>
[Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]
- The topic ‘How do I add a custom field to a sidebar?’ is closed to new replies.