ThemeShaper Forums » Thematic

[closed]

Custom hook -> INTO main aside?

(14 posts)
  • Started 3 years ago by krowchuk
  • Latest reply from krowchuk
  • This topic is resolved
  1. Hi folks,
    As hard as I am trying to figure it out, I just don't get it! :-)

    I have been reading and playng and reading and playing and I just don't get how I could add a hook inside the main aside sidebar.

    What I am trying to do...

    I would like to have my feature image appear at the top of my main primary aside sidebar, before any widgets (Currently I don't use any widgets in my main aside), on every single post and every static page. I would also like to insert a custom field directly below that image containing some text - a custom caption.

    thematic_abovemainasides() doesn't solve the problem, it actually breaks the layout.

    example... from my current "non-thematic" theme, my code for the sidebar.

    '
    <div class="sidebar widesidebar">

    <?php echo get_the_post_thumbnail($wp_query->post->ID); ?>
    <div id="callout-caption">
    <h2>
    <?php echo get_post_meta($post->ID, 'callout_caption', true); ?>
    </h2>
    </div>

    • <?php if(function_exists('echo_ald_crp')) echo_ald_crp(); ?>
    • <?php if(function_exists('echo_ald_wherego')) echo_ald_wherego(); ?>

    <?php if (!function_exists('dynamic_sidebar') ||
    !dynamic_sidebar('wide')) : ?>
    <?php endif; ?>

    </div><!--end sidebar-->
    '

    can be seen on my website at: www.krowchukdressage.com/blog/about

    Any help would be most appreciated!

    Cheers,
    Chris

    Posted 3 years ago #
  2. i think thematic_abovemainasides is exactly what you are looking for. it breaks the layout b/c you aren't applying the proper classes to whatever div you are adding to that hook. if you look at the primary and secondary divs (where the sidebars live... you will see they actually look like

    <div id="primary" class="aside main-aside">

    so for your new div you need to use the same classes to pick up the same style rules that limit the sidebar's widths and position. otherwise a div normally spans the full width - hence breaking your layout.

    Posted 3 years ago #
  3. Dear Helgatheviking!

    Why is it that when faced with something one assumes is going to be complicated we overlook the simple? ;-)

    Of course! This makes perfect sense. Thank you so much!

    I plugged that in and voila! However, do I need to add theme support for thumbnails? I ask because it worked before I added this to my functions.php... (albeit the image was wrong size before the addition of theme support)

    'add_theme_support ('post-thumbnails');
    set_post_thumbnail_size ( 300, 9999 );
    '

    Interestingly it worked with the above, as well as just setting size as below...

    '
    set_post_thumbnail_size ( 300, 9999 );
    '

    So I am going to make the assumption that thumbnail support is built in, and all I have to do is set my featured image size? It works so far!

    Cheers!
    Chris

    Posted 3 years ago #
  4. Whoops!

    New issue...

    My custom field for the captions is not displaying anything. The <p> and </p> are created but the custom field value is not being displayed.

    I copy/pasted the custom field get_post_meta php code from my current theme, this should work shouldn't it?

    Here is my complete function:

    '
    // Add thumb and custom fields to new aside and place above main asides
    function thumbnail() {
    ?>
    <div id="primary" class="aside main-aside">

      <?php echo get_the_post_thumbnail($wp_query->post->ID); ?>
      <p><?php echo get_post_meta($post->ID, 'callout_caption', true); ?></p>

    </div><!-- end primary main aside -->
    <?php
    }
    add_action('thematic_abovemainasides', 'thumbnail');
    '

    What am I missing? eek!

    Cheers!
    Chris

    Posted 3 years ago #
  5. if you look at the functions.php post-thumbnail support for WP2.9+ is enabled by default, though it looks like you can filter it off if you really wanted to. (why anyone would is beyond me)

    for your next question i am going to guess that it isn't working b/c inside the function $wp_query and $post don't mean anything unless you "global" them.... hence no output. untested, but try this:

    function thumbnail() {
    //declare variable for use inside of function
    global $wp_query;
    //get the post ID# for current post
    $postID = $wp_query->post->ID;
    ?>
    <div id="primary" class="aside main-aside">
    
          <?php the_post_thumbnail($postID); ?>
          <p><?php echo get_post_meta($postID, 'callout_caption', true); ?></p> 
    
    </div><!-- end primary main aside -->
    <?php
    }
    add_action('thematic_abovemainasides', 'thumbnail');
    Posted 3 years ago #
  6. Yes, that fixed the custom field so that it displays the value now, thank you!
    However, now I have lost the scaling of the thumbnail image so any featured image that is larger than 300px wide escapes the div and covers the main content. Seems for some reason the post thumbnail size I set...

    '
    // Set thumbnail size
    set_post_thumbnail_size ( 300, 9999 );
    '

    ... at the top of my functions.php is now not working?

    I vaguely recall the reason for the custom field being this way... and yes now that I think about it and confirm by opening my current theme page.php, I had placed global $wp_query right at the very top (actually, index, single, etc. etc. as well) and I think it had to do with another issue I received help with (I should have commented it! - let that be a lesson all of its own!).

    I think, it had to do with placing a different thumbnail image for each page in an article that is paginated using nextpage. This person showed me how to set one custom field and use an array of images that would place the corresponding image for each page at the top of a column to the left of the content loop.

    When I read this back, it makes even less sense! Hah! An example might be better: http://www.krowchukdressage.com/blog/training-articles/ is the parent page and I have a number of subpages (not posts) under this. Some of these articles are very long (5, 6, 7 pages) and have multiple images that are specifically related to each page break in the article (using nextpage). Featured image wouldn't work obviously, so we created this... which I saved as a custom named sidebar called from a custom template page.php...

    '
    <div class="sidebar widesidebar">

    <?php global $page;
    //global var for what page we are on

    if($page == '' || !isset($page)){ $page=1; }
    //set the var to 1 if it is not set

    $the_sidebar_array=get_post_meta($post->ID,'sidebar-photos-array',true);
    //get custom field

    if(strpos($the_sidebar_array,',')!=''){
    //do this if comma

    $the_photos=explode( ',',$the_sidebar_array );
    //create a real array from comma sep custom field value

    array_unshift($the_photos,'');
    //add blank val to 0 array key to make it easier to match page number

    $the_image = wp_get_attachment_image_src( $the_photos[$page],'full',false );
    //get image thumbnails src for the image

    if( !empty ($the_image)) {echo '<img class="centered" src="'. $the_image[0].'" />';}
    //if it has image, echo it out

    }else{
    //do this if no comma

    if($the_sidebar_array !=''){
    //just making sure it is really a value - we don't want and error if blank

    $the_image = wp_get_attachment_image_src( $the_sidebar_array,'full',false );
    //get image thumbnails src

    echo '<img class="centered" src="'.$the_image[0].'" />';
    //if it has image, echo it out
    }
    }

    ?>

    <div id="callout-caption">
    <h2>
    <?php echo get_post_meta($post->ID, 'callout_caption', true); ?>
    </h2>
    </div>

    • <?php if(function_exists('echo_ald_crp')) echo_ald_crp(); ?>
    • <?php if(function_exists('echo_ald_wherego')) echo_ald_wherego(); ?>

    <?php if (!function_exists('dynamic_sidebar') ||
    !dynamic_sidebar('trainingarticles')) : ?>
    <?php endif; ?>

    </div><!--end sidebar-->
    '

    The deeper I get into this re-tooling the more worried that I'm going to need lots of help! I really appreciate the help. I'm actually starting to learn something!

    Posted 3 years ago #
  7. once you've set the post thumbnail size WP will crop and resize NEWLY uploaded images to that size... not resize anything you've already uploaded. for that, you need to regenerate your thumbnails

    http://wordpress.org/extend/plugins/regenerate-thumbnails/

    also i only loosely follow what you are trying to do w/ the custom fields. what if you switched gears and used multiple featured images instead:

    http://wordpress.org/extend/plugins/multiple-post-thumbnails/

    Posted 3 years ago #
  8. Hi,

    Okay, now I am really confused! So, before I pasted in your rewritten function, the thumbnail was showing up fine, scaled perfectly, but the custom field was showing up blank. I pasted in your function (which did not change the thumbnail size, all it addressed was the custom field, I think!) and now the thumbnail has to be regenerated? I will take your word for it, but I don't understand how or why!!! ;-)

    I guess if I changed the size of the thumb I would understand why, however, we didn't change the size. What is the logic behind that?

    I will follow your lead and try the plugin.

    Cheers!
    Chris

    Posted 3 years ago #
  9. Hi again,

    Well, I tried regenerate and that didn't solve the problem. I then removed the feature image and selected it again, that didn't solve the problem. So I deleted it permanently and went to page edit-> set feature image-> upload from computer-> and set the image once more, saved my page, viewed it and the image is not cropped. scaled... it is full size.

    Thinking I might need that hair in my ears for a wild comb-over once this is done! ;-)

    Looking through my functions file... I don't know if this has anything to do with it but I thought it would be worth a try, I added 'add_theme_support' back into functions.php so now I have it back to:

    // Set thumbnail size support
    add_theme_support ('post-thumbnails');
    set_post_thumbnail_size ( 300, 9999 );

    and still the image is too large, showing full size 432px wide.

    Now, this is odd because back when I had it this way..

    function thumbnail() {
    ?>
    <div id="primary" class="aside main-aside">
    
          <?php echo get_the_post_thumbnail($wp_query->post->ID); ?>
          <p><?php echo get_post_meta($post->ID, 'callout_caption', true); ?></p> 
    
    </div><!-- end primary main aside -->
    <?php
    }
    add_action('thematic_abovemainasides', 'thumbnail');

    The post thumb was the right size, but the custom field was blank.

    Now, no matter what I do the custom field shows up but the post thumb is not getting scaled!

    If I take your suggestion out and return it to mine, the thumb is scaled but the custom field is blank!

    Eek!

    Cheers!
    Chris

    Posted 3 years ago #
  10. also i only loosely follow what you are trying to do w/ the custom fields. what if you switched gears and used multiple featured images instead:

    Yeah I know it is kind of weird, and I really am a much better horse trainer, instructor, and rider than website developer! Still, I enjoy putzing with it when the weather is miserable.

    The code for the custom image array was developed for me to address an issue, and it is working fine in my old theme. Basically, I have some articles that have multiple images and I wanted to move them out of the flow of the content and place them in a sidebar to the left of the main content. I also wanted to paginate the articles because they were rather long. I wanted to break the page using the nextpage tag and have an unique and corresponding image for each page, appear beside the article, not inline. So, the custom field array was suggested. for the field value I enter a comma delimited list of image ID#s, like 32,47,55 and then when you view page 1, image 32 displays, page 2, image 47 etc.

    OH! I should add that this multiple featured image method is only used on one template, for the sub pages under training articles. All other pages use the regular featured image feature.

    This all happened when I decided to change my theme from a 2 column (content, sidebar) to a 3 column (sidebar, content, sidebar) layout and wanted my pictures to be placed in the left sidebar, top.

    One other thing this gentleman helped me with, I wanted a secondary list of post excerpts from various categories appearing below my sidebars and main content, that I could customize the category page by page.

    This might also have something to do with him adding the global $wp_query; to the top of all my templates?

    I'm all for a simpler way! I don't have a lot of time to putz and stay current. That is why I thought I would try and rebuild using Thematic.

    Posted 3 years ago #
  11. Dear helgatheviking,

    Thank you so much for helping me thus far. After some sleep and thought I have decided to approach this in a simpler manner. I think I will simplify the thumbnail and custom fields and attack the articles/images that are split up using the nextpage tag by simply breaking them up into individual subpages. I think going forward that will be the simplest solution and will give me the flexibility (perhaps even better flexibility!) than the overly complicated mess it is now.

    So, to simply add the thumbnail and a custom field to my main aside, this code seems to work locally...

    // Thumbs and custom field
    function thumbnail() {
    ?>
    <div id="primary" class="aside main-aside">
      <ul>
        <?php $postid = get_the_ID(); ?>
        <?php echo get_the_post_thumbnail(); ?>
          <div class="callout-caption">
           <h3>"<?php echo get_post_meta($postid, 'callout_caption', true); ?>"</h3>
          </div><!-- end callout-caption -->
      </ul>
    </div><!-- end primary main aside -->
    <?php
    }
    add_action('thematic_abovemainasides', 'thumbnail');

    Not being very well versed in php and barely understanding half of what is documented in the codex, this works local, but is it good code?

    Cheers!
    Chris

    Posted 3 years ago #
  12. well imho, if it works then it is good code! good enough anyway. there are things you learn along the way that sometimes make things faster and/or cleaner, but if it works that is a good start.

    the only thing i see is

    <?php echo get_the_post_thumbnail(); ?>

    could be

    <?php the_post_thumbnail(); ?>

    but i couldnt say if that actually would make a difference in terms of load time or anything (that is still beyond me). it is just a touch cleaner since your way is a little redundant.

    Posted 3 years ago #
  13. Hiya!

    Now that is strange, I deleted the "echo" and the thumbnail disappeared! Popped it back in and the thumbnail reappeared.

    scratch scratch!

    Posted 3 years ago #
  14. Oops! I lied!

    ;-)

    of course, had I deleted the "echo get_" then of course it would work just fine!

    I declare this thread... solved!

    Cheers!
    Chris

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.