WordPress.org

Ready to get started?Download WordPress

Forums

add image above the loop? Function? (7 posts)

  1. squidz
    Member
    Posted 3 years ago #

    I'm using the Notes Blog "Core" theme as a parent and have a child theme draped over it.

    The site is just using the straight up blog layout.

    What I want to do is insert an graphic above the loop on the home page. Ideally, one that the user can manage. Is there a way to put a widget in the spot?

  2. Christopher Spires
    Member
    Posted 3 years ago #

    If the widget route is the direction you want to go, first you need to register a new widget in your themes function.php:

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array(
    'name' => 'Post Image Widget',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '',
    'after_title' => '',
    ));
    ?>

    Then you simply need to add the widget to your themes post.php before The Loop:

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Post Image Widget') ) : ?>

    Post Image Widget being whatever you want to name the widget. You can then add a text widget with with your <img /> tag in the body. If you're looking for a more user friendly route, add the above code to your theme and install the Image Widget.

    There's a good "widgetizing" tutorial here.

  3. squidz
    Member
    Posted 3 years ago #

    Thanks BODA82. I'll try it out! Really just getting into PHP and making WP do stuff outside of the box. Hadn't realized creating a widget was so simple.

  4. Christopher Spires
    Member
    Posted 3 years ago #

    Indeed, adding widgets are easy. And I apologize because I misspoke before. You said you only wanted it on the homepage, so you'll want to utilize the is_home conditional or you can depend on a plugin. If you don't want to depend on a plugin you can do something like this:

    <?php if ( is_home()) {
        <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Post Image Widget') ) : ?>
    } else {
      // don't display the widget
    } ?>

    I've not actually tired this, so if it doesn't work give me a shout and I'll do some testing myself.

  5. squidz
    Member
    Posted 3 years ago #

    hehe...after popping it in above the loop and added the <?php endif; ?> it needed to let the page load, I realized it needed to be conditional. I've been trying to work it in just as you show above, in place of the hard coded banner that I had in the conditional.

    But it creates a syntax issue that I haven't gotten past. I did find that there is a plugin called Widget Logic which add conditional fields to all widgets. Pretty nifty. But I was still trying to see if it would work without that extra plugin.

  6. Christopher Spires
    Member
    Posted 3 years ago #

    This should work:

    <?php if ( is_home()) { ?>
        <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("Post Image Widget") ) : ?><?php endif; ?>
    <?php } else { ?>
        <!-- Not The Homepage -->
    <?php } ?>

    You'll have to style it accordingly. I suggest something like:

    <?php
    if ( function_exists('register_sidebar') )
    register_sidebar(array(
    'name' => 'Post Image Widget',
    'before_widget' => '<div id="home-widget">',
    'after_widget' => '</div>',
    'before_title' => '',
    'after_title' => '',
    ));
    ?>

    Then give your stylesheet #home-widget with the appropriate styles depending on your theme.

  7. squidz
    Member
    Posted 3 years ago #

    Perfect! Thank man. It was the syntax of the function itself.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.