Forums

Display post/page in sidebar (17 posts)

  1. inhouse
    Member
    Posted 1 year ago #

    Hello,

    I would like to display a specific post OR page within my sidebar so a user could update it every day such as (daily specials). I don't like a client to have to edit the basic text widget because the styling can break very easily. I'd rather they edited a post/page using the WYSIWYG editor. I installed the "Featured Post Widget" but this is only displaying 3 lines from the post. Is there a way for me to load a "get post" or something like that in my sidebar.php file? I've tried a couple solutions I found in the forums and I can't get any of them to work for me. Is there another solution that would work better? Thanks!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

  3. inhouse
    Member
    Posted 1 year ago #

    I'm sorry but I'm not sure how this would work to display a specific post or page. I could see how this would get a list of posts in a category, but I'd like to only show 1 post all the time. What would I need to add to sidebar.php? This is the first time I've really messed with anything like this before. Thanks for replying so quick too!
    <?php get_posts( $args ); ?>

  4. inhouse
    Member
    Posted 1 year ago #

    @esmi, thanks so much for replying, but I'm so confused by this. I've read both pages multiple times and I think I learned a little, but what I've attempted has not worked. I am placing this in my sidebar.php file and this makes my sidebar disappear.

    <?php get_posts( $query = new WP_Query( 'name=todays-specials' ); ?>

  5. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    You use the same queries and some form of the Loop to display posts whether it's one post or 100 posts.

  6. inhouse
    Member
    Posted 1 year ago #

    Any idea if I'm doing this right and why it's not working? Seems as though I have the code right, but I'm not sure if that goes in sidebar.php to display in the sidebar or where it should go?

  7. inhouse
    Member
    Posted 1 year ago #

    Also tried <?php get_posts( $query = new WP_Query( 'pagename=todays-specials' ); ?> in sidebar.php and the sidebar is still blank. No background color or anything. Please help! Address is here

  8. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Either use get_posts or WP_Query - not both!

  9. inhouse
    Member
    Posted 1 year ago #

    Ok, so it should be <?php $query = new WP_Query( 'page_id=38' ); ?> or $query = new WP_Query( 'page_id=38' );? I also tried the get_posts to no avail. Can you be more specific? I really appreciate the help. This is all I have in my sidebar:

    <div id="sidebar">
    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar-Main') ) { ?>
    
    <?php get_posts( 'pagename=todays-specials' );?>
    
    <?php } ?>
    </div>
  10. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    <?php
    $args = array(
    	'page_id' => 38,
    	'posts_per_page' => 1
    );
    $my_pages = get_posts( $args );
    foreach($my_pages as $post) : setup_postdata($post); ?>
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
    <?php endforeach;
    wp_reset_query();?>
  11. inhouse
    Member
    Posted 1 year ago #

    OMG, I was not even close to getting this to work... I can't thank you enough. This is by far the most complicated thing I've attempted and I'm still a little unclear as to how it works but I will keep learning and now I have an awesome example to follow. Thanks a ton!

  12. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    No problem. Once you can understand get_posts, you'll be able to move onto WP_Query which is even more powerful.

  13. inhouse
    Member
    Posted 1 year ago #

    How would I change this to display a page instead of a post? Page ID=41 so I tried to change that and now nothing is displayed.

  14. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Change 'page_id' => 38, to 'page_id' => 41,

  15. inhouse
    Member
    Posted 1 year ago #

    Right, that's what I did and nothing was displayed. It isn't working correctly. I tried various page_id and none work. I thought it was as simple as changing IDs but does something else need to change too?

  16. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    If you just want another Page, nothing else needs to change.

  17. inhouse
    Member
    Posted 11 months ago #

    Can anyone see why changing 'page_id' => 38, from an ID of a Post to an ID of a Page wouldn't work? I have a new site I am implementing this same code and I still cannot get a page to show up in the sidebar, only a Post. I've used various page IDs and none will work. Any post ID I use works perfectly. I would really rather display a page than a post in this scenario. This client doesn't maintain a blog so this would be the only post in the site. Displaying a page makes more sense. The idea is to make the "Current Heating Oil Prices" easily editable by the client.

    New site build is here. Thanks a lot.

Topic Closed

This topic has been closed to new replies.

About this Topic