ThemeShaper Forums » Thematic

[closed]

different code on different category archives? conceptual problem: narrow target

(14 posts)
  • Started 3 years ago by darrenc
  • Latest reply from umberto
  • This topic is resolved
  1. darrenc
    Member

    i need help conceptually understanding if something like this is possible, and how it would be done:

    let's say i have a thematic child theme, and i want to harness different code on different category archives. so for example my website has two categories, "apples" and "oranges." for whatever reason i've decided that i need the date html to be coded differently on the category archive of "apples" while "oranges" and every other page/post will get the standard treatment.

    i have my function ready to go:

    function childtheme_override_postmeta_entrydate(){
    	$entrydate = "apples date format goes here";
    	return apply_filters('thematic_post_meta_entrydate', $entrydate);
    }
    

    but this will get applied globally to everything, not even just the category archives.

    i am guessing i need to use the hierarchy and make a category-apples.php template -- but is that the best practice and how would i go about achieving the result i want using thematic? i obviously don't want to re-code the page from scratch, but maybe such narrow targeting isn't possible?

    stumped! (and still a beginner)

    Posted 3 years ago #
  2. you need conditional tags: http://codex.wordpress.org/Conditional_Tags

    function childtheme_override_postmeta_entrydate() {
       if(is_category('apples')){
          //$entrydate = "apples date format goes here";
       } elseif (is_category('bananas')){
          //$entrydate = "bananas date format goes here";
       } else {
          //$entrydate = "generic date format goes here";
       }
    return apply_filters('thematic_post_meta_entrydate', $entrydate);
    }

    i believe you could also just filter for the conditions you want

    function filter_cat_date($entrydate){
    
      if(is_category('apples')){
        $entrydate = "apples date format goes here";
      }
    
    return $entrydate;
    }
    add_action('thematic_postmeta_entrydate','fitler_cat_date');

    totally untested, but ought to work. should at least get you on the right path.

    Posted 3 years ago #
  3. jonnycj
    Member

    Not sure whether this is useful but I used a similar idea to show just the posts for a specific category inside a specific page:

    I created a page template called <page-poemofthemonth.php> which was just a copy of my index.php page from Thematic child theme, renamed and with the following template code at the top:
    <?php
    /*
    Template Name: Poem of the Month Page
    */
    ?>
    I then created a new page inside Wordpress admin called 'Poem of the Month' and specified in 'Page Attributes' that it use this template.

    This page template was then edited as follows to act as a container for the posts from the category I created called <Poem of the Month> where the category slug is <poemofthemonth>.

    Inside that page template, below the line <thematic_indexloop();> I added another line calling in a function to display just posts from the specified category. I call this function from within the template using:

    // the custom poemofthemonthloop luminal_poemofthemonthloop();

    I then have this corresponding function in my child theme functions.php page:

    function luminal_poemofthemonthloop() {
    query_posts( 'category_name=poemofthemonth&posts_per_page=4' );
    $counter = 1;
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div id="post-<?php the_ID() ?>" class="<?php thematic_post_class() ?>">
    <?php thematic_postheader();
    if ($counter == 1 && has_post_thumbnail() && !is_paged()) {
    the_post_thumbnail('poemofthemonth-thumbnail');
    }
    ?>
    <div class="entry-content">
    <?php the_excerpt(); ?>
    " class="more"><?php echo more_text() ?>
    <?php
    $counter++; ?>
    </div>
    </div><!-- .post -->
    <?php endwhile; else: ?>
    <h2>Eek</h2>
    <p>There are no posts to show!</p>
    <?php endif;
    wp_reset_query();
    }

    This displays the first four posts in the category below any content held inside the page itself. The first post displays a thumbnail image if it exists.

    Still working on building navigation bar for these results.

    This code is something I may have found in a book called Wicked Wordpress Themes - cheers lads :-)

    Posted 3 years ago #
  4. you could also just copy the existing blog template, rename it (both in the comments and in the php filename) and then edit the query vars in the template.

    Posted 3 years ago #
  5. darrenc
    Member

    helga, you rock. thanks so much for taking the time to reply. your first technique works perfectly.

    since you brought it up, your second technique does not seem to work. you have a typo in the last line (fitler)... but even when i stripped out the conditionals,

    function filter_cat_date($entrydate){
    	$entrydate = "apples date format goes here";
    	return $entrydate;
    }
    add_action('thematic_postmeta_entrydate', 'filter_cat_date');

    does not replace the date. it seems to fail silently. i suspect "thematic_postmeta_entrydate" isn't actually a hook? i don't my way around enough to override a function like that, i assume the only way to do it is with the childtheme_override_postmeta_entrydate. if you have the time to provide more insight i am interested to know why this approach didn't work, or how it might.

    ---

    jonnycj, going off what i just learned from helga, i believe you could also have approached what you wanted to do in the following way -- in your child theme's functions.php...

    if (is_page("Poem of the Month"){ // this is the equilavent of manually making the page and applying the template
    	function luminal_poemofthemonthloop() {
    		// do your stuff
    	}
    	add_action("thematic_belowcontainer", "luminal_poemofthemonthloop"); // pop it in below the content container in the html
    }
    

    someone can please correct that if it is wrong.

    Posted 3 years ago #
  6. well it ought to be:

    function filter_cat_date($entrydate){
    	$entrydate = "apples date format goes here";
    	return $entrydate;
    }
    add_filter('thematic_postmeta_entrydate', 'filter_cat_date');

    except that that doesn't seem to work either and i have no idea why. clearly in my first code, i made more than 1 typo... b/c i should have been using add_filter instead of add_action.... which happens when i just write straight into the post reply box w/o testing. but i tested the above and while it should work based on my understanding of filters, it doesn't. i'm sure Gene is busy, but maybe he can explain why my filter didn't working like i thought it would.

    so with that in mind, why don't you just use the override?

    almost every thematic function (like function thematic_some_function() )can be overridden by copying the whole function to your theme, and then replacing the thematic in the function title.

    function childtheme_override_some_function(){
    //do my stuff
    }

    so in your case:

    function childtheme_override_postmeta_entrydate() {
       if(is_category('apples')){
    	$entrydate = "apples date format goes here";
       } else {
                $entrydate = '<span class="meta-prep meta-prep-entry-date">' . __('Published: ', 'thematic') . '</span>';
    	    $entrydate .= '<span class="entry-date"><abbr class="published" title="';
    	    $entrydate .= get_the_time(thematic_time_title()) . '">';
    	    $entrydate .= get_the_time(thematic_time_display());
    	    $entrydate .= '</abbr></span>';
        }
    	return $entrydate;
    }

    then in your function for jonnycj, you've got the right idea except that conditionals have to go INSIDE the function. frustrated the hell out of me for a day or 2 until someone pointed that out to me.

    function luminal_poemofthemonthloop() {
       if (is_page("Poem of the Month"){ // this is the equilavent of manually making the page and applying the template
    		// do your stuff
    	}
    }
    add_action("thematic_belowcontainer", "luminal_poemofthemonthloop"); // pop it in below the content container in the html

    however i should say that "Poem of the Month" will not match anything. you have to either use the 'nice name'/slug or the page ID. spaces will kill it.

    Posted 3 years ago #
  7. darrenc
    Member

    fantastic, i found the simple mistake with your code. the filter name seems to be "thematic_post_meta_entrydate" not "thematic_postmeta_entrydate"

    function filter_cat_date($entrydate){
    	if (is_category('apples')) {
    		$entrydate = "apples date format goes here";
    	}
    	return $entrydate; // if not apples, return the default thematic $entrydate
    }
    add_filter('thematic_post_meta_entrydate', 'filter_cat_date');
    

    thank you so much helga. "so with that in mind, why don't you just use the override?" since i am a beginner, i really just wanted to learn it both ways. there's something very elegant about just making a quick target of a situation rather than using the entire override function, but i like that i now *understand* both options. it helps me wrap my brain around how all these bits and pieces come together.

    also, thank you for correcting my jonnycj example.

    Posted 3 years ago #
  8. darrenc
    Member

    heres the correct summary for anyone who comes to this. given the original default thematic function for date post meta (and all the other functions that might appear in this similar construction)

    // Create entry date for post meta
    if (function_exists('childtheme_override_postmeta_entrydate'))  {
            function thematic_postmeta_entrydate() {
                    return childtheme_override_postmeta_entrydate();
            }
    } else {
            function thematic_postmeta_entrydate() {
                $entrydate = '<span class="meta-prep meta-prep-entry-date">' . __('Published: ', 'thematic') . '</span>';
                $entrydate .= '<span class="entry-date"><abbr class="published" title="';
                $entrydate .= get_the_time(thematic_time_title()) . '">';
                $entrydate .= get_the_time(thematic_time_display());
                $entrydate .= '</abbr></span>';
                return apply_filters('thematic_post_meta_entrydate', $entrydate);
            }
    } // end postmeta_entrydate()
    

    you can very specifically target and manipulate it using conditional tags by using the override function:

    function childtheme_override_postmeta_entrydate(){
    	if (is_category('cat-a')){
    		$entrydate = "format the date differently on the 'cat-a' category";
    	} elseif (is_category('cat-b')){
    		$entrydate = "format the date differently on the 'cat-b' category";
    	} else {
    		// since the thematic_postmeta_entrydate function gets overwritten by using override,
    		// we need to include the default fallback action
    		$entrydate = '<span class="meta-prep meta-prep-entry-date">' . __('Published: ', 'thematic') . '</span>';
    		$entrydate .= '<span class="entry-date"><abbr class="published" title="';
    		$entrydate .= get_the_time(thematic_time_title()) . '">';
    		$entrydate .= get_the_time(thematic_time_display());
    		$entrydate .= '</abbr></span>';
    	}
    	return apply_filters('thematic_post_meta_entrydate', $entrydate);
    }
    

    or doing the exact same thing as the above, this time by manipulating the filter.

    function my_child_date_filter($entrydate){
    	if (is_category('cat-a')){
    		$entrydate = "format the date differently on the 'cat-a' category";
    	} elseif (is_category('cat-b')){
    		$entrydate = "format the date differently on the 'cat-b' category";
    	}
    	return $entrydate; // if not cat-a or cat-b, return the default thematic $entrydate
    }
    add_filter('thematic_post_meta_entrydate', 'my_child_date_filter');
    

    two ways to skin the cat.

    Posted 3 years ago #
  9. well bravo. sorry i provided code w/ so many mistakes, but i am quite glad that know you seem to have your head wrapped around the basics of thematic. almost all changes get made the same way. while the overrides are brilliant, sometimes the filter is a more elegant solution.

    and it is good to point out that the filter name you are targeting is not always the same as the function name. the filter name is the one in that apply_filters definition. i always forget that, so good catch!

    Posted 3 years ago #
  10. darrenc
    Member

    huge thanks helga. appreciate you being around to answer questions.

    Posted 3 years ago #
  11. jonnycj
    Member

    Thanks Guys - I was following the path of adding a second Loop into a page template rather than use one template with a conditional because I need a lot of layout variation between the pages. I also need users to be able to edit the page content that appears before the second Loop easily in the WP backend.
    One thing I cannot resolve though is if I have 20 posts in a category and I want the first four to show on the first page using the second Loop (below the page content from the first Loop), how do I then create a navigation below this second Loop to link to the remaining 16 posts, split across two further pages ?
    I had naively hoped that using the code I specified above inside my page template, with the following below it would be enough:

    // create the navigation below the content
    thematic_navigation_below();

    I have tried this using query_posts and get_posts but just not sure what I need to add.... any thoughts would save me :-)

    Posted 3 years ago #
  12. umberto
    Member

    I'm trying to remove the post meta on category pages.

    Based on this thread and a snippet Chris posted here: http://forums.themeshaper.com/topic/removing-authortime-information-under-title#post-4865 I've tried this:

    function no_postmeta() {
    if(is_category()) {}
    }
    add_filter('thematic_postheader_postmeta', 'no_postmeta');

    which works but removes the postmeta from all pages and this:

    if(is_category()) {
    function no_postmeta() {}
    }
    add_filter('thematic_postheader_postmeta', 'no_postmeta');

    which causes an error.

    Looks like it should be simple and am quietly getting my head around filters and functions but would appreciate any pointers.

    Posted 3 years ago #
  13. umberto- you should probably have created your own thread for your question.

    you aren't filtering quite right. you could do it one of 2 ways:

    function no_postmeta($postmeta) {
    if(is_category()) { $postmeta = '';}
    return $postmeta;
    }
    add_filter('thematic_postheader_postmeta', 'no_postmeta');

    or

    function no_postmetaB($postheader) {
    if(is_category()) { $postheader = thematic_postheader_posttitle();}
    return $postheader;
    }
    add_filter('thematic_postheader', 'no_postmetaB');
    Posted 3 years ago #
  14. umberto
    Member

    Thanks HtV - and sorry about appending onto this thread - was a bit blurry at the time :)

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.