ThemeShaper Forums » Thematic

[closed]

display:none vs remove_action

(27 posts)
  • Started 4 years ago by Seijun
  • Latest reply from flick
  • This topic is resolved
  1. I was wondering, is it better to use the css display:none to get rid of actions, or remove_action in the functions php? Which is faster for a browser to process, or does it even matter?

    Posted 4 years ago #
  2. Jamie Mitchell
    Member

    remove action is better...

    display:none should only be used for stuff like your skip to content or the edit link.

    think how your site will work without a stylesheet, your site should still be readable. so imagine if the stylesheet did not load, all the stuff you set to display:none would pop right back in.

    Posted 4 years ago #
  3. @Jamie: Thanks for the clear explanation there. I was wondering about the same thing but this makes so much sense

    so imagine if the stylesheet did not load, all the stuff you set to display:none would pop right back in.

    Posted 4 years ago #
  4. Good point.
    Where can I find a decent tutorial on properly using remove_action?
    I have a huge list of things set to display:none based on what page or type of page the user is looking at, and I dont have a clue about how to transfer those commands to php!

    .meta-prep-author,
    .vcard,
    .comments-closed-link,
    .meta-sep,
    #noresults-searchform,
    #nav-below,
    .not-singular #nav-above,
    .not-singular .entry-content img.size-thumbnail

    are all classes set to display:none in my css.

    Posted 4 years ago #
  5. @Seijun: For actions, I would start with the Thematic guide on Theme Hooks - scroll to the end - for the first step to removing Thematic actions. Or do a search on the forum.

    // Remove default Thematic actions
    function remove_thematic_actions() {
     remove_action('thematic_hookname','thematic_actionname',optionalpostitionnumber);
    }
    add_action('init','remove_thematic_actions');

    However, given that you're trying to remove some id/css classes, I think you could consider using filters as well.

    For instance, .meta-sep is called here in thematic_postheader_meta:

    // Create post meta
    function thematic_postheader_postmeta() {
    
        $postmeta = '<div class="entry-meta">';
        $postmeta .= thematic_postmeta_authorlink();
        $postmeta .= '<span class="meta-sep meta-sep-entry-date"> | </span>';
        $postmeta .= thematic_postmeta_entrydate();
    
        $postmeta .= thematic_postmeta_editlink();
    
        $postmeta .= "</div><!-- .entry-meta -->\n";
    
        return apply_filters('thematic_postheader_postmeta',$postmeta); 
    
    } // end thematic_postheader_postmeta

    So your filter would be something similar excluding the line with the meta-sep class, and also the thematic_postmeta_authorlink() as I presume you don't want the author to be shown as well (vcard, meta-prep-author) so maybe:

    //New PostMeta
    function replacement_meta($postmeta) {
    
        $postmeta = '<div class="entry-meta">';
        $postmeta .= thematic_postmeta_entrydate();
        $postmeta .= thematic_postmeta_editlink();
        $postmeta .= "</div><!-- .entry-meta -->\n";
        return $postmeta;
    }
    add_filter('thematic_postheader_postmeta', 'replacement_meta');
    Posted 4 years ago #
  6. Thanks Flick, I will try that.
    Although, I dont think it would really matter that much if people saw the author name and other meta stuff on accident... The only thing in there that would be a real problem if the css didnt load would be ".not-singular .entry-content img.size-thumbnail". Im using custom excerpts and thumbnails, which put together means my post excerpts show the excerpt thumbnail PLUS all the image thumbs from my post, when I only want the excerpts thumbnail to show. The extra images that I dont want are all of the class size-thumbnail, and the one I want to keep is always class "wp-post-thumbnail" or something like that.

    Posted 4 years ago #
  7. @Flick,

    If you would add comments to the postmeta how would that look?

    I tried to put in
    $postmeta = '<div class="comments">'
    $postmeta .= "</div><!-- coments -->\n";

    But that did not work

    Soren

    Posted 4 years ago #
  8. @Soren: I'm a little confused, where does <!--coments --> come from?

    I have a feeling the function you're looking for is get_comments_number() for the number of comments, but as I've not tested it I'm not sure.

    Posted 4 years ago #
  9. Maybe I'm going about this all wrong, but how do I use remove_action on the author name? Or can I even? The thematic website only lists a handful of actions involving the header area, but nothing for anywhere else.

    I would like to just tell thematic in the shortest way possible "dont show the author info or any of the comments closed stuff".

    Posted 4 years ago #
  10. @Seijun: I find it really helpful to look at content-extensions.php and searching for e.g. comments closed.

    So for instance, comments closed seems to appear at:
    thematic_postfooter_postcomments, which "Create comments link and edit link"
    thematic_postfooter_postconnect, which "Create permalink, comments link, and RSS on single posts"

    So you can use the code shown in content-extensions.php to build your custom filter for these functions.

    This is how I would approach it anyway. Am not sure about other ways at the moment I'm afraid.

    Posted 4 years ago #
  11. Thank you.
    I have the post-meta and footer areas just about where I want them, although so far I have been unable to find any way to get rid of the extra thumbnails with php. Also, I am struggling to insert a post last modified date. Perhaps you have some advice in that area.. I have a post modified date inserted, but it keeps showing up above entry-utility instead of in it.

    Posted 4 years ago #
  12. @Seijun: Did you decide to opt for the CSS solution in the end? Sometimes simple = good :)

    And sorry I missed your previous post (only read Soren's post instead) - I see you're using the Thumbnail for Excerpts plugin on your test site.

    Not particularly sure how to resolve your query; I'm fairly certain the standard child template does not include post thumbnails as a default setting. What happens when you de-activate the plugin - does it still generate multiple thumbnails?

    If not, then I think it's probably more like a result of how the plugin works, although I am having difficulty visualising the issue myself.

    e.g. the archives page is loaded and shows 3 excerpts of news posts with *the* thumbnail, but it also shows lots of other thumbsnails? Where are they being shown exactly?

    Posted 4 years ago #
  13. The two plugins I'm using are "thumbnails for excerpts" and "advanced excerpts". AE allows me to keep all my post formatting, which is essential for what Im using the site for. It is also very important for each post to have exactly one thumbnail, which is what I chose "excerpts for thumbnails" for. However, thumbs for excerpts does not work unless AE has img tags enabled. BUT, when img tags are enabled, all the thumbs from the post as well as the thumb generated by "thumbnails for excerpts" appear. I don't know how to tell AE to only allow certain types of images. Its either all or none. Ive gotten around this with display:none because the thumbnails generated by my plugin (the ones I want to keep) have a different class than the images I don't want to keep.
    http://wolfdogrescue.net/wp/category/adoptables/
    This is the place where extra thumbs was a problem. If you look at the source code, you will see several image thumbs per post, but my css only keeps one displayed.
    As you pointed out earlier, if my css didnt load for someone, something like this would be a problem.

    Posted 4 years ago #
  14. @Seijun: Jamie Mitchell provided us both with that great explanation :)

    Thanks for the link.

    I decided to install Thumbnails for Excerpts (and Advanced Excerpts) on my local installation, and was wondering if you've set the plugin so that excerpts are activated for both the home and archives pages in the Thumbnail Excerpt settings page?

    http://img59.imageshack.us/img59/4432/thumbnailforexcerptsset.png

    Once I'd activated it (setting them both to Yes), only the thumbnail was displayed. And the other images were not displayed until the single post was loaded.

    I think this is because, by default, excerpts do not display post images.

    Posted 4 years ago #
  15. Yes, the plugin is set for home and archive.
    Actually, the strange thing was that the thumbs plugin wasn't working until I installed advanced excerpt. No idea why that was (can anyone else reproduce that?). Then I also discovered that the thumbs plugin actually has its own excerpt length setting as well as a line of code that strips formatting, so it was overriding the advanced excerpt plugin and not allowing me to format excerpts or set their length. So I had to go into the php for the thumbs plugin and delete the line of code that strips formatting and also change the excerpt length. THEN all the other images from the posts popped up, but it did allow me to have the formatting I wanted. If I knew enough about php, I could probably add something to the thumbs plugin code telling it to only strip other images. Here is the code BTW after I modified it.

    if($content)
    			$content=apply_filters('the_excerpt', $content);
    		else {
    			$content = $post->post_content;
    			$content = strip_shortcodes($content);
    			$content = str_replace(']]>', ']]>', $content);
    			$excerpt_length = 500;
    			$words = explode(' ',$content,$excerpt_length+1);
    			if(count($words) > $excerpt_length){
    				array_pop($words);
    				array_push($words,'...');
    				$content=implode(' ',$words);
    			}
    Posted 4 years ago #
  16. Oh! I almost have it.. added $content = strip_tags($content, ""); to the thumb excerpts php to allow br tags. Now how do I add other tags to the allowed list? $content = strip_tags($content, "<a>"); didn't work...

    Posted 4 years ago #
  17. @Seijun: Just realised my screenshot had a big 'No' under Archives, but it is actually 'Yes' on my settings page of course.

    I can definitely replicate Thumbnails for Excerpts not working properly if AE is deactivated when loading the Category view as no thumbnail is shown (although it shows up fine on the homepage) - having looked at the comments on TfE, it seems the consensus is the same for others.

    However, if you're happy to forgo plugins, this tutorial looks really promising. I've just tested it out and incorporated extras from Mark Jaquith's tutorial as well as this forum thread about read more

    Assuming you don't need to modify index, and it's just categories we're talking about, this is what is in the child theme functions.php in my test setup:

    if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
    	add_theme_support( 'post-thumbnails', array( 'post' ) );
    	set_post_thumbnail_size( 150, 150, true ); // Normal post thumbnails
    }
    
    function remove_category_loop() {
         remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_category_loop');
    
    // Now we will create our own loop.
    function thumb_category_loop(){
           while ( have_posts() ) : the_post()  // Start the loop:
        // This is just what we decide to show in each post ?>
        <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
            <?php thematic_postheader(); ?>
            <div class="entry-content">
            <?php the_post_thumbnail();  // we just called for the thumbnail ?>
    		<?php thematic_content(); ?>
            </div>
            <?php thematic_postfooter(); ?>
        </div><!-- .post -->
        <?php
        endwhile; // loop done, go back up
    }
    // And in the end activate the new loop.
    add_action('thematic_categoryloop', 'thumb_category_loop');
    
    // Code to change length of excerpt
    
    function new_excerpt_length($length) {
    return 500;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

    This is how I've styled wp-post-image in the child theme style.css file:

    .wp-post-image{
    vertical-align:text-top;
    margin: 0px 15px 0px 0px;
    }

    Essentially everything can be done, it seems, just with native Thematic and WordPress. My previous posts haven't been very helpful, so hope this is a slight improvement.

    Posted 4 years ago #
  18. @Seijun: Crosspost - In answer to your question, I was wondering about the stripped HTML (for the code above) when I came across this post by Duncan MacLeod

    http://palehorseinformation.com/2009/12/23/fixing-the-wordpress-excerpt/

    And it seems like the line to add (to your code) is probably:

    $text = strip_tags( $text, '<b><em><i><strong><sub><sup>' ); // allowed tags (change as needed)

    and include the <a> tag.

    More info from this comment.

    Posted 4 years ago #
  19. Ive seen those posts before about adding thumbs using wp's built in "add post thumbnail" thing, but I could never get the images to hard-crop. I copy/pasted your code exactly and the images scale down but do not crop.
    I would like to try the method explained here http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it because it would mean not having to deal with assigning a thumb to each post (I avoid interacting with wp's "media" section whenever possible).

    Ok, I've added this:

    // Improve excerpt formatting
    function improved_trim_excerpt($text) {
    	global $post;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
    		$text = strip_tags($text, '<br />');
    		$excerpt_length = 80;
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words)> $excerpt_length) {
    			array_pop($words);
    			array_push($words, '[...]');
    			$text = implode(' ', $words);
    		}
    	}
    	return $text;
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'improved_trim_excerpt');
    
    // Catch first image of post
    function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_img = $matches [1] [0];
    
      return $first_img;
    }
    
    // Create new category loop
    function remove_category_loop() {
         remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_category_loop');
    
    // Now we will create our own loop.
    function thumb_category_loop(){
           while ( have_posts() ) : the_post()  // Start the loop:
        // This is just what we decide to show in each post ?>
        <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
            <?php thematic_postheader(); ?>
            <div class="entry-content">
            <img src="<?php echo catch_that_image(); // we just called for the thumbnail ?>" />
    		<?php thematic_content(); ?>
            </div>
            <?php thematic_postfooter(); ?>
        </div><!-- .post -->
        <?php
        endwhile; // loop done, go back up
    }
    // And in the end activate the new loop.
    add_action('thematic_categoryloop', 'thumb_category_loop');

    I am getting the first thumbnail from my post. But it would be nice it is also linked to the post. Additionally, I am trying to discover how to filter it so that the image tags don't appear for post excerpts that have a custom excerpt..

    Do you know why thumbs for excerpts will not work on category pages by itself? Seems a little strange.

    Posted 4 years ago #
  20. Ok, ALMOST there!
    Changed
    <img src="<?php echo catch_that_image(); // we just called for the thumbnail ?>" />
    to:
    <a href="<?php echo get_permalink() ?>"><img class=post-thumb src="<?php echo catch_that_image(); // we just called for the thumbnail ?>" /></a>

    Now my thumbnail links to the post.

    Posted 4 years ago #
  21. @Seijun: Glad you've found a good solution for your problem :)

    Posted 4 years ago #
  22. Well, my problem is almost solved. I'm still trying to figure out how to filter this so it doesnt show up in the blog category, and also add something to size the "catch_that_image" thumbnail.

    Posted 4 years ago #
  23. @Seijun: Do you mean filtering the function so the thumbnails don't show up for your Blog? If I've understood you correctly, I believe this can be done by creating a custom category-blog.php (or something) template.

    And re: thumbnail - are you thinking of actually resizing the image (since it's 100 x 100 on your adoptables link) on the fly using CSS or having a completely different thumbnail size?

    Posted 4 years ago #
  24. What I mean is, adding a filter to this:

    // Create new category loop
    function remove_category_loop() {
         remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_category_loop');
    
    // Now we will create our own loop.
    function thumb_category_loop(){
           while ( have_posts() ) : the_post()  // Start the loop:
        // This is just what we decide to show in each post ?>
        <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
            <?php thematic_postheader(); ?>
            <div class="entry-content">
    <a href="<?php echo get_permalink() ?>"><img class=post-thumb src="<?php echo catch_that_image(); // we just called for the thumbnail ?>" /></a>
    		<?php thematic_content(); ?>
            </div>
            <?php thematic_postfooter(); ?>
        </div><!-- .post -->
        <?php
        endwhile; // loop done, go back up
    }
    // And in the end activate the new loop.
    add_action('thematic_categoryloop', 'thumb_category_loop');

    that says "dont do any of this if the post has no images". Or, if thats too complicated "dont do any of this for post excerpts in the blog category".

    What I mean about resizing thumbs.. "catch_that_image" takes the first image it finds in a post to use for the excerpt but it doesnt resize anything. Right now all my images are already hard-cropped to 100x100 in the post so there is no issue. But if I posted a 600x800 image, i would still want it hard-cropped to 100x100 in the excerpt.

    Posted 4 years ago #
  25. (Just to make life easier for those referencing the thread for your solution to 'scan posts for images': http://forums.themeshaper.com/topic/can-i-replace-the-loop-with-a-filter#post-10167

    I needed to have get_the_image set to scan my post for images if none were found as attachments.<?php echo get_the_image( array( 'image_scan' => true ) ); ?>

    Someone advised me that one could use CSS to resize images and the article that I found at the time uses e.g.

    .post-thumb {
    max-width: 100px;
    width: expression(this.width > 100 ? 100: true);
    height: auto;
    }

    So the max width of the thumb will always be only up to 100, and the auto height keeps the aspect ratio or you could set it for 100px.

    Posted 4 years ago #
  26. Thank you for all for your help. I have most everything working the way I want it now. I can mark this thread resolved.

    Posted 4 years ago #
  27. @Seijun: I've learnt a lot too, thanks!

    Posted 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.