ThemeShaper Forums » Thematic

[closed]

Introducing custom fields into Thematic

(24 posts)
  • Started 10 years ago by Jamie Mitchell
  • Latest reply from Mickstah
  • This topic is resolved
  1. Jamie Mitchell
    Member

    hey all...

    I could not see this mentioned before so thought it would be interesting to others as well.

    i often add a custom field for a thumbnail image to wordpress posts in the index.php, in thematic's case i would need to duplicate the index.php to my child theme and put in the following above the thematic_postheader

    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php
    the_title(); ?>"><img src="<?php bloginfo('url'); ?>/wp-content/uploads/<?php
    $values = get_post_custom_values("thumbnail"); echo $values[0]; ?>" alt=""
    class="postimg" /></a>

    works perfectly

    ...but

    it would be better if i could add this to the same place via function.php of the child theme, after all that is the purpose of child themes...

    how would this be achieved

    ...J

    Posted 10 years ago #
  2. Hey Jamie,

    is your code below the <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">?

    Cheers,

    Chris

    Posted 10 years ago #
  3. Jamie Mitchell
    Member

    Hey Chris...

    yes it would go inside the post div

    thanks for you time...J

    Posted 10 years ago #
  4. Hey Jamie,

    this would be something like the following code added to your functions.php:

    function my_postheader($postheader) {
        $postheader = 'your functionality' . $postheader;
        return $postheader;
    }
    add_filter('thematic_postheader', 'my_postheader');

    Cheers,

    Chris

    Posted 10 years ago #
  5. Jamie Mitchell
    Member

    thankyou again Chris.

    i see where your headed and i used your code and it inserted the example of 'your functionality' exactly in the right place...perfect

    but how do i use my code above with this, obviously i can't use it exactly as is, i was looking into the hooks and filters to try and learn more, and i can see a rough idea of what i would need to do, but not enough to get my head around making it work.

    ...J

    Posted 10 years ago #
  6. Will publish a working example in approx. 30 minutes.

    Posted 10 years ago #
  7. Jamie .. should your code sit above the title or between title and post meta (author, publishing date)?

    Posted 10 years ago #
  8. Jamie Mitchell
    Member

    your to kind :)

    here is an example of what it would look like if i put it in the index.php

    <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
    
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
    <?php the_title(); ?>"><img src="<?php bloginfo('url'); ?>/wp-content/uploads/
    <?php
    // this is where the thumbnail gets printed
    	$values = get_post_custom_values("thumbnail"); echo $values[0];
    ?>" alt="" class="postimg" /></a>
    
    <?php thematic_postheader(); ?>
    
       /////////rest of loop in here ///////////////
    
    	 </div><!--end post-->
    Posted 10 years ago #
  9. Jamie Mitchell
    Member

    i almost had it, but my permalinks actual address kept displaying.

    Posted 10 years ago #
  10. :-) .. try this one:

    function my_postheader($postheader) {
    		$ph = $postheader;
        $postheader = '<a href="' . get_permalink();
    		$postheader .= '" rel="bookmark" title="Permanent Link to ';
    		$postheader .= get_the_title() . '"><img src="';
    		$postheader .= get_bloginfo('url') . '/wp-content/uploads/';
    		$values = get_post_custom_values("thumbnail");
    		$postheader .= $values[0] . '" alt="" class="postimg" /></a>';
    		$postheader .= $ph;
        return $postheader;
    }
    add_filter('thematic_postheader', 'my_postheader');

    Cheers,

    Chris

    Posted 10 years ago #
  11. Jamie Mitchell
    Member

    ok...

    this is what i have so far, fixed the permalink address from showing by using get__permalink instead of the_permalink...

    <?php
    function my_postheader($postheader) {
        $postheader = '<a href="' . get_permalink() . '
    " title="' . __('Permalink to ', 'thematic') . the_title() . '">
    image stuff</a>' . $postheader;
        return $postheader;
    }
    add_filter('thematic_postheader', 'my_postheader');
    ?>

    where 'image stuff' will be the rest of the code used for the custom field.

    ...J

    Posted 10 years ago #
  12. Jamie Mitchell
    Member

    oops, i did not refresh there for a bit and realized you posted your code already...

    Grrrr your to clever for me...i wasn't even close

    that looks awesome, i will try it out.

    cheers...J

    Posted 10 years ago #
  13. Jamie Mitchell
    Member

    YEH !!!

    works a charm

    and once again...your the man.

    thanks again Chris for your time, this will be used for my personal blog, so i will post the link up shortly.

    J

    Posted 10 years ago #
  14. Good idea .. It's always nice to see what happens with my code snippets :-)

    Cheers,

    Chris

    Posted 10 years ago #
  15. Jamie Mitchell
    Member

    hi again Chris...

    how can i add an if statement in there

    like

    if (is_front_page() || is_home()) {

    so it only works on the front custom page and the blog page. (at the moment it seems to be working on pages too)

    i tried the above, which is what i would use for anything else, but i just got a blank page with this.

    thanks in advance for all your help

    J

    Posted 10 years ago #
  16. Hey Jamie,

    using my last code it would be something like this:

    function my_postheader($postheader) {
    	if (is_front_page() || is_home()) {
    		$ph = $postheader;
    		$postheader = '<a href="' . get_permalink();
    		$postheader .= '" rel="bookmark" title="Permanent Link to ';
    		$postheader .= get_the_title() . '"><img src="';
    		$postheader .= get_bloginfo('url') . '/wp-content/uploads/';
    		$values = get_post_custom_values("thumbnail");
    		$postheader .= $values[0] . '" alt="" class="postimg" /></a>';
    		$postheader .= $ph;
    	}
    	return $postheader;
    }
    add_filter('thematic_postheader', 'my_postheader');

    Did a quick test with one of my icons and it looks great!

    Cheers,

    Chris

    Posted 10 years ago #
  17. Jamie Mitchell
    Member

    awesome, i almost had it except for the little '}' before the return :(

    i just added a is_category() on there too, so categories pages/post can look like the blog with images.

    thanks again...j

    Posted 10 years ago #
  18. Sorry Chris could you please explain what you're doing here:

    $ph = $postheader;
    ...
    $postheader .= $ph;

    Also the if statement seems not to be working on my home page. My thumbnail doesn't loads when I use it. However it does load without the if statement.

    Thanks for your help.

    Posted 9 years ago #
  19. Hi guys-

    I'll take a stab an explaination.

    function my_postheader($postheader) {

    ...

    }
    add_filter('thematic_postheader', 'my_postheader');

    Here $postheader is the potential output of thematic_postheaderbefore the filter is applied. The variable $ph is storing that value. The $postheader = '<a href="' . get_permalink(); is overwriting value $postheader. Wiping the slate clean so to speak. All the little .= bits in between are concatenating the new markup for your filter and then $postheader .= $ph is adding back the original output of thematic_postheader before the filter onto the end of the string.

    Sorry, I'm not really sure why this is'nt working for you. I hope the explanation is accurate and understandable.

    -Gene

    Posted 9 years ago #
  20. Thanks for the explanation here.

    Does this mean that storing $postheader in $ph allows to add custom content without actually filtering the rest of the function? I noticed that if I don't use $ph = $postheader; the rest of the function gets filtered (doesn't output anymore), while my custom content applies.

    By the way, the filter itself works but the if statement doesn't. My custom thumbnails is returned on the single post output too :(

    Posted 9 years ago #
  21. No and yes. Returning a value to a filter hook will the overwrite the original functions value from before the apply_filters

    In your child function you are storing the original variable from thematic_postheader then building a new one and adding the original back on to the end of the one you are creating. By not using $ph=$postheader you aren't storing the original and an empty value is attached at the end instead of the original value from the thematic function.Hope that make sense It's a bit confusing...

    Thats strange. This works in my testing of it excluding from single pages. What are you using to store the thumbnail image?... a custom field? If the code you are using differs from this example at all post it here.

    Posted 9 years ago #
  22. jpklein
    Member

    This thread has been extremely helpful so far; it's given me exactly what I wanted and it works great. I have one more question (since I'm also fairly new to working on child themes and am not exactly a PHP pro here): Using the snippets in this thread, how would you then make the display of the thumbnail conditional?

    For example, if a thumbnail is specified in a post's custom fields, display it on the index (just as we've already done). If a thumbnail isn't specified, skip the code we've added entirely. As it stands now, for any posts I have that don't have thumbnails specified, the snippet that we've inserted throws the rest of the post off slightly (which I don't want).

    I don't have a version of the theme I'm working on online yet (still working on it locally), so I uploaded a screenshot to illustrate what I'm talking about: http://farm3.static.flickr.com/2636/4138118632_1c3055b4a9_o.png

    I feel like I'm missing something extremely obvious here. Any help would be greatly appreciated!

    Thanks!

    Posted 9 years ago #
  23. Hi jpklein,

    To avoid this you could check to see if get_post_custom_values has returned anything as a condition. Try:

    function my_postheader($postheader) {
    	if (is_front_page() || is_home()) {
    		$ph = $postheader;
    		$postheader = '<a href="' . get_permalink();
    		$postheader .= '" rel="bookmark" title="Permanent Link to ';
    		$postheader .= get_the_title() . '">';
    
    		$values = get_post_custom_values("thumbnail");
    		// check to see if custom field exists
    		if ($values) {
    			$postheader .= '<img src="' . get_bloginfo('url') . '/wp-content/uploads/';
    			$postheader .= $values[0] . '" alt="" class="postimg" /></a>';
    			}
    
    		$postheader .= $ph;
    	}
    	return $postheader;
    }
    add_filter('thematic_postheader', 'my_postheader');

    -Gene

    Posted 9 years ago #
  24. Mickstah
    Member

    Hi, sorry to drag up an old topic but this seems to be the best place for this. I am finally forcing myself to learn how to use custom fields and this seems like the perfect exercise except I have been struggling to find anything relevant to what I am after.

    I used the above code and modified it slightly:

    function my_postheader($postheader) {
    $ph = $postheader;
    $postheader .= '<img src="';
    $postheader .= get_bloginfo('url') . '/wp-content/uploads/';
    $values = get_post_custom_values("header");
    $postheader .= $values[0] . '" alt="" class="headerimg" />';
    $postheader .= $ph;
    return $postheader;
    }
    add_filter('thematic_postheader', 'my_postheader');

    This is successfully retrieving the image, however it also it posting the page title, so for example I have the page title 'home' followed by the image, then the page title again before the body text. How do I stop this duplication.

    Secondly is there a thematic hook I can use to get this appearing above the postheader? I have tried using thematic_belowheader (using this as reference http://bluemandala.com/thematic/thematic-structure.html) but so far have had no luck.

    Is there something I'm missing?

    Posted 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.