ThemeShaper Forums » Thematic


Add post_meta custom field data only when fields are true

(3 posts)
  • Started 10 years ago by candregg
  • Latest reply from candregg
  • This topic is resolved
  1. I'm a relative noob trying to filter post content selectively.

    In my installation, only posts in one category include the custom fields "sponsor text" and "sponsor link".

    If I create a custom category loop by category, the custom fields do not show up in single posts; if I apply it globally, then the special div to hold the meta data appears on ALL posts, even where the custom field data doesn't exist.

    Here is the global function:

    //  Custom Category Loop 
    function remove_category_loop(){ //  first remove the thematic action
        remove_action('thematic_categoryloop', 'thematic_category_loop');
        remove_action('thematic_archiveloop', 'thematic_archive_loop');
    function child_category_loop() { // now rebuild your version of the action
     while (have_posts()) : the_post();
    	 global $post;
         $sponsortext = get_post_meta($post->ID , 'sponsor', true);
    	 $sponsorlink = get_post_meta($post->ID , 'sponsorlink', true);
            <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
               <?php thematic_postheader(); ?>
               <div class="entry-content">
                 <?php thematic_content(); ?>
                 <div id="sponsor">
    	<p><?php echo $sponsortext; ?></p>
        <p>For more information visit the website, <a href="http://<?php echo $sponsorlink; ?>" target="_blank">http://<?php echo $sponsorlink; ?></a></p>
               <?php thematic_postfooter(); ?>
            </div><!-- .post -->
         <?php endwhile;
    add_action('thematic_categoryloop', 'child_category_loop');
    add_action('thematic_archiveloop', 'child_category_loop');

    This code works great for the one category page,,
    But not for the general blog category page,

    I'm sure the answer is an if-else construction,but so far I haven't been able to code one that works.

    Please advise, if possible.

    Posted 10 years ago #
  2. "if I apply it globally"

    does that mean you tried filtering the_content()?

    which is what i'd do and just add in a condition to test if $sponsortext exists

    function add_after_content($content) {
      global $post;
      $sponsortext = get_post_meta($post->ID , 'sponsor', true);
      $sponsorlink = get_post_meta($post->ID , 'sponsorlink', true);
      if($sponsortext && $sponsorlink){
        $sponsor = '<div id="sponsor">';
        $sponsor .= '<p>'. $sponsortext.'</p>';
        $sponsor .= '<p>For more information visit the website, <a href="http://'.$sponsorlink.'>" target="_blank">http://'.$sponsorlink.'</a></p>';
        $sponsor .= '</div>';
        $content .= $sponsor ;
      return $content;
    add_filter('the_content', add_after_content);

    totally untested, you might need to escape the backslashes... i'm not sure, but i think the concept is right.

    ps- you can now use

    function childtheme_override_category_loop()

    and you don't need to add or remove any actions. it's magic.

    Posted 10 years ago #
  3. For the most part, that worked like a charm: One remaining issue is that the &sponsorlink call is generating an extra character string, %3e, that is messing up the actual sponsor's links. Any idea where that's coming from?

    Other than that, it's beautiful. I knew some kind of conditional test was the answer; I just kept messing up the syntax for so many hours that I got frustrated. Thanks for demonstrating it so clearly.

    Yes, I've seen the function childtheme_override_category_loop() in the newer version of thematic, but have not (until now) had occasion to use it.

    Will file your magic and tips for future use.

    Posted 10 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.