WordPress.org

Ready to get started?Download WordPress

Forums

Simple Page Sidebars
[resolved] Use a generated sidebar elsewhere (20 posts)

  1. Streetproject
    Member
    Posted 1 year ago #

    I want to use one of the generated sidebars on single.php. How can I use a generated one?

    get_sidebar('name-here-i-guess?')

    Thanks for your help in advance

    http://wordpress.org/extend/plugins/simple-page-sidebars/

  2. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    Hi there,

    The plugin is mainly suited for use with pages and doesn't provide a mechanism for overriding sidebars for posts within the dashboard. Can you explain a little more about your setup and what you're trying to do so I can make the best suggestion?

    • Are you wanting your sidebar to vary between posts, or will all posts use the same sidebar?
    • Should your posts sidebar be different than the main blog page sidebar?
    • Are you trying to use this to create the sidebar and then make a change to your template to use the generated sidebar?

    I should be able to provide better feedback knowing the answers to those questions.

    Thanks,
    Brady

  3. Streetproject
    Member
    Posted 1 year ago #

    Brady,

    First, thanks for the quick response (and the great plugin for that matter)

    Just so you know, here is my individual setup:

    The parent theme has only one sidebar, and I have been using the child theme to better suit our theme. Even though our blog is hosted in a separate WP installation, I know we are considering merging the posts into our main WP installation. So because of that I created a new post type and a separate category taxonomy.

    Ideally, I wanted to create/link sidebars on individual posts (not that they would change page-to-page, but my company changes there mind a lot and the option is always nice).

    So plan B was to just call the specific created sidebar on the single and category templates for that custom post type (since they needed their own templates anyways).

    So in quick, the answer to your questions:

    • Do I want them to very between posts? Yes (but that is probably a much bigger fix)
    • Is the post sidebar different then the blog sidebar? Yes (because it is a different post type)
    • Am I using the plugin to create sidebars then use in my template? Yes (because I figured question 1 and 2 would be a bigger hack)

    Thanks again for your quick response.
    Matt

  4. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    Thanks for the detailed response, Matt. Unfortunately, it sounds like most of what you need to do should be done in your child theme templates, or there are a couple of other sidebar plugins that are more advanced that might let you do everything from within the dashboard.

    However, if you've already created the sidebar using this plugin and you'd like to keep it simple and use it in your templates, the standard dynamic_sidebar( 'Generated Sidebar Name' ) template tag should work fine to display the custom sidebar.

    To modify it between posts, though, you'll have to do a few if/else checks to call the right sidebar on the correct post and that can become unmanageable if there are a lot of them, especially if you have to arbitrarily register the sidebar with a page first. In that case, I'd definitely recommend seeing if there were a plugin more suited for using with posts.

    - Brady

  5. Streetproject
    Member
    Posted 1 year ago #

    Yeah, unfortunately that looses all the registered argumets applied to the original sidebar.

    Your plugin is honestly really impressive, and I hope you continue to invest in it to eventually allow some more control (apply sidebars to posts/custom post_types).

    And I appreciate your quick response. I was expecting to not get an answer for a while.

    It looks like I'll be registerring sidebars by hand, and loading them by meta-data attached to posts/pages.

    Thanks again for your help,
    Matt

  6. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    Hmm, I didn't think about the custom sidebar inheriting the default sidebar's arguments. Good point.

    Thanks for the compliments on the plugin. I originally named it the way I did so I could focus on providing an easy solution for a single sidebar on pages only, but I have thought about how it would work for all post types (and multiple widget areas on the same post/page). Unfortunately, I think it'd need to be rewritten to make it more generic.

    That being said, I think a couple of small tweaks would allow you to use this with posts if you would be ok with limited support (no quick edit/bulk edit, etc). Let me know if you're interested in testing some quick changes out so you don't have to implement a custom solution.

  7. Streetproject
    Member
    Posted 1 year ago #

    I appreciate your help.

    I wanted to download your plugin not for myself, but for my bosses who want to be able to add/delete sidebars. I liked your plugin for it's simplicity, but the simplicity came at the cost of no customization (like you said, not your goal so not your fault). I was hoping that since I was a developer I could just add some hooks to add some more options, but the hacks were getting pretty extensive so I just went ahead and used my own built-in functions.

    As for the custom version, I actually had already gotten it to where it would load/save on posts/custom post_types (for debug purposes), however I am doing everything in my power to not edit plugin/parent theme files. We normally just make our edits right on the original files, but I want to move away from that as a company, so that we can automatically update those files instead of having to do the updates by hand.

    Anyways, I'll go ahead and mark this as resolved. If you wanna discuss anything else feel free to keep the conversation going.

  8. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    I was thinking more of the lines of building the tweaks into the plugin and leaving them undocumented, sort of an Easter egg for developers that wanted to use the plugin and didn't need anything more advanced, but wanted slightly more flexibility.

    It's definitely a good policy to not edit plugins and parent themes directly if you want to continue receiving updates, so nice to see y'all move in that direction. And feel free to let me know if you see a place where a hook might make sense.

    In any case, I went ahead and pushed the tweaks I mentioned that should give you the ability to use this with additional post types. If you'd like to give it a shot, the update can be downloaded on GitHub. I'd like to do a little more testing before pushing it to the plugin directory.

    If you do decide to give it a go, you'd just need to do something like this to get it to work with other post types:

    add_action( 'init', 'prefix_init' );
    function prefix_init() {
    	add_post_type_support( 'post', 'simple-page-sidebars' );
    }

    That'll give you the Sidebar meta box on the Add/Edit Post screen.

    Let me know if that helps at all.

  9. Streetproject
    Member
    Posted 1 year ago #

    The above code didn't work on my installation.

    Just incase you are interested:
    1. Added code to functions.php
    2. Went to WP->Posts->Add New
    3. Verified sidebar meta-box missing from the "Screen Options" tab.

  10. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    Did you download the update off GitHub and install it in place of your old version?

  11. Streetproject
    Member
    Posted 1 year ago #

    whoops.. that was without your GitHub changes...

  12. Streetproject
    Member
    Posted 1 year ago #

    Okay, changes work great. Will this get pushed here to wordpress?

  13. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    Great! Yeah, I'll push them eventually. If you're running the update, you won't get a notice though, since it'll be the same version number.

  14. Streetproject
    Member
    Posted 1 year ago #

    Okay. Thanks a bunch! This helps developers like me out a lot!

  15. Streetproject
    Member
    Posted 1 year ago #

    Just as a really sloppy (but working) method for anyone searching for a way to use a sidebar on a template:

    before using the get_sidebar() call, query a page that uses the sidebar you want and then use the_post() right before the call. You can use wp_reset_query() after as well.

    So here is the new code:

    <?php
    	query_posts('page_id=12'); // change to the ID of your page
    	the_post();
    		get_sidebar();
    	wp_reset_query();
    ?>

    This will work for now.

  16. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    That's a pretty heavy impact to do something like this, so I wouldn't recommend anyone use that code. Using query_posts() is typically frowned upon as well as there's usually a better way to do what it does. Here are a couple of resources that should help clarify why it shouldn't be used:

    I'd prefer to add a filter or a template tag to do this in a manner that's much better for performance. If you don't mind explaining where you're trying to do this and why, I may be able to make a change or suggest a better alternative.

    • You already have the page id, so by default, you should know the id of the sidebar, right? Or are you wanting to allow the sidebar to change in the template if it's changed for that page?
    • You can already change the default args that a sidebar is registered with if you're needing those to match your theme's defaults.
    • What kind of template are you doing this for? And is there a reason you don't want to do it in sidebar.php?
  17. Streetproject
    Member
    Posted 1 year ago #

    I couldn't intervene your sidebar call unless I used query_posts. It was a category (custom taxonomy) page specifically and it would always take the last sidebar of whatever was queried (which I reset, but then it would use the default).

    Adding the sidebar name and all filters/actions didn't have any effect (your code overrides all mine).

    Any other suggestions on how to call a generated sidebar onto a category page? I normally don't use global query override, but that was the only way to change the sidebar (at least as far as I could investigate).

  18. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    Some of the functionality is fenced off a bit since this was meant to be restricted to pages and for use in the admin, whereas pretty much anything is possible in templates. I can add a filter or two for bypassing some of it programmatically, though, if it's necessary.

    Typically, I would just do a check in sidebar.php for a category, then call the particular sidebar directly using dynamic_sidebar().

    if ( is_category() ) {
    	dynamic_sidebar( 'my-custom-sidebar' );
    } else {
    	dynamic_sidebar( 'default-sidebar' );
    }

    If that call uses different args than your theme, you can override those using the 'simple_page_sidebars_widget_defaults' filter to make them match your theme.

    If you really wanted to tie the category sidebar to a particular page, then you could grab the meta data from the page to pass it along:

    if ( is_category() ) {
    	$sidebar_name = get_post_meta( '12', '_sidebar_name', true );
    	dynamic_sidebar( $sidebar_name );
    } else {
    	dynamic_sidebar( 'default-sidebar' );
    }

    Is there a reason something like that wouldn't work?

  19. Streetproject
    Member
    Posted 1 year ago #

    Doesn't that go back to the issue of not maintaining the registered styles? (before title, after title, etc..)

  20. Brady Vercher
    Member
    Plugin Author

    Posted 1 year ago #

    That's where the 'simple_page_sidebars_widget_defaults' filter comes into play so that you can make the sidebars registered via the plugin use the same args as your theme.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.