This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Adding custom field to sidebar

  • Creator
    Topic
  • #6868
    viper
    Participant

    I’m trying to add a custom field to display at the top of all default right sidebars. I created the custom field in a random post.

    There are probably a few ways to do this. With the slightly confusing deep levels of nested actions and relationships in different files, I have gone with adding a function in the functions file, and using add_action, based off the Primary Framework Action Hook location, which in this case seems to be: themeblvd_fixed_sidebar_before.

    In my functions file it looks like this

    The styling shows up, but it’s not returning the value associated with the variable (custom field).

    I would prefer to know where to place this code in the exact sidebar location itself, but at least this mostly works too.

    Pardon me if this is a general WordPress question.

    Thank you very much…

Viewing 9 replies - 1 through 9 (of 9 total)
  • Author
    Replies
  • #6870
    Jason Bobich
    Keymaster

    I think it’s because you’re trying to access the global $post object without first referencing it. At the top of your function, try putting this:

    global $post;
    function sidebar_top() {
    	global $post;
    	?>
    	...
    	<?php
    }
    #6872
    viper
    Participant

    Hmm… nothing happening. The only result is the styling (I set a background color) is now gone, so it’s just empty space.

    This is both a custom field and a Jumpstart exercise, so is this approach (creating a custom field in a random post and using a function and add_action with the primary action hook location) at least sound?

    #6874
    viper
    Participant

    I would think adding a custom field to a random post is not the best way, because it seems the custom field is only “created,” or called for, if/when that particular post is called for.

    Obviously I’m not a WordPress/php expert, I’m just trying to figure out how to create a custom field to place at the top of the sidebar…

    Thank you.

    #6875
    Jason Bobich
    Keymaster

    I also just noticed this is incorrect with what you’re doing.

    This:

    get_post_meta($post->ID, 'subtitle', $single = true);

    Should be this:

    get_post_meta($post->ID, 'subtitle', true);

    Does that solve it?

    #6876
    Jason Bobich
    Keymaster

    Yeah, the custom field would belong to whatever page/post you’re showing at that time.

    #6878
    viper
    Participant

    That did work! Some combination of both corrections have been successful. The custom field value (text) is appearing.

    More generally, if you wanted something to appear at the top of ALL right sidebars, not just for a single post, where would you define that custom field, if not to an individual post?

    Would you have to just add text in the function itself for adding above sidebars?

    Then I’ll let you go, thank you…

    #6881
    Jason Bobich
    Keymaster

    Yeah you’d just add text it in the function itself.

    If you wanted it configurable from somewhere else, one thing with this framework you could do is to add your own theme option:

    http://dev.themeblvd.com/tutorial/add-remove-edit-theme-option/

    And then in your function hooked to the top of your sidebar, you’d pull it with themeblvd_get_option:

    http://dev.themeblvd.com/tutorial/get-a-theme-options-value/

    #6882
    viper
    Participant

    Thank you I will look at all that. Appreciate your help, cheers.

    #6883
    Jason Bobich
    Keymaster

    And I guess it’s probably worth mentioning, that if you wanted you could pull a custom field for some post across your entire site by just putting the ID directly into retrieving the custom field.

    So instead of this:

    get_post_meta($post->ID, 'subtitle', true);

    You’d do this:

    get_post_meta(123, 'subtitle', true);

    Where “123” is the ID of whatever post that custom field is attached to. Just seems kind of weird to do that though, I guess.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.