ThemeShaper Forums » Thematic

[closed]

thematic_content() filter with conditional is_front_page

(19 posts)
  • Started 9 years ago by amorita
  • Latest reply from amorita
  • This topic is not resolved
  1. Hi,

    I've been trying to get this snippet to work for me for hours today, and just can't figure it out.

    function childtheme_content($content) {
    	if (is_page_template('homepage.php'))  {
    	$content= 'excerpt';
    	return $content;
    	}
    }
    add_filter('thematic_content', 'childtheme_content');

    And, homepage.php has a loop:

    <div id="container">
    
    			<?php thematic_abovecontent(); ?>
    
    			<div id="content">
    
                <?php
    			$wp_query = new WP_Query();
    			$wp_query->query( array( 'posts_per_page' => 1, 'paged' => $paged ) );
    			$more = 0;
    			?>
    
    				<?php
    	        thematic_abovepost();
                	thematic_indexloop();
                	?>
    
    			</div><!-- #content -->
    
    				<!-- stuff -->
    
    			<?php thematic_belowcontent(); ?>
    
    		</div><!-- #container -->

    What happens is that the filter works, but the conditional doesn't. I tried is_home() (posts display as excepts on the home page, as well as the "blog" page where I don't want it to do that), is_front_page() (doesn't seem to match anything), and the above.

    I saw somewhere (http://forums.themeshaper.com/topic/filter-thematic_content-plus-custom-loop) that thematic_abovepost() is needed, but it's there. Anything else I'm missing?

    Thanks for your help!

    Posted 9 years ago #
  2. middlesister
    Member

    Try putting the return statement after the curly braces in your conditional

    function childtheme_content($content) {
    	if (is_page_template('homepage.php'))  {
    	$content= 'excerpt';
            }
    	return $content;
    }
    add_filter('thematic_content', 'childtheme_content');
    Posted 9 years ago #
  3. Hi middlesister, thanks for the help!

    Unfortunately, though, no dice... Whether I put the return statement in or outside of the if statement, the result is the same - it seems to me, the conditionals aren't returning anything... If I take the if statement, it works fine, but that means whatever changes I make, it's site-wide.

    I am having the same problem in this other snippet I am trying to use to change the way category archive is displayed:

    // The Category Loop
    if (is_page_template( 'category-questions.php' ) )  { //this conditional isn't working for some reason. all category loops will be missing the header...
    	function childtheme_override_category_loop() {
    		while (have_posts()) : the_post(); 
    
    				thematic_abovepost(); ?>
    
    				<div id="post-<?php the_ID();
    					echo '" ';
    					if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    						post_class();
    						echo '>';
    					} else {
    						echo 'class="';
    						thematic_post_class();
    						echo '">';
    					}
         				// thematic_postheader();
         				?>
    					<div class="entry-content">
    <?php
    // stuff to show custom field called 'questions,' for some fancy styling. maybe in the future?
    ?>
    <?php thematic_content(); ?>
    
    					</div><!-- .entry-content -->
    					<?php thematic_postfooter(); ?>
    				</div><!-- #post -->
    
    			<?php 
    
    				thematic_belowpost();
    
    		endwhile;
    	}
    }
    add_action ('thematic_categoryloop', 'childtheme_override_category_loop');
    
     // end category_loop

    Again, if I take the if () out, the add_action() happens, but across all categories. I need to control how the page displays per category or page...

    Cheers,
    Akira

    Posted 9 years ago #
  4. middlesister
    Member

    Aha I see. Its the right conditionals we need to find. Well, is_page_template() refers to when you have selected a specific page template for a wordpress Page. I assumed you hade a wordpress Page that was using that page template. is_home() like you saw, refers to the default 'blog' page and is_front_page() refers to whatever you have set as your front page in your Settings - regardless if it is a Page or the 'blog'. How are you using the page template and where is this loop supposed to show up?

    For your second example, the same applies. The conditional is_page_template( 'category-questions.php' ) is only true if we are on a wordpress Page that has the page template 'category-questions.php' applied. If you want to target a specific category archive page, use is_category('category-slug') instead. For a wordpress Page in general, you can use is_page('page-slug').

    Posted 9 years ago #
  5. Thanks middlesister.

    The way these pages are setup:
    homepage: it's a static page using a template "homepage.php" (with the above loop). I tried to get to it by is_page_template('homepage.php'), is_front_page() and came empty-handed. On the other hand, if I use is_home(), it matches, but my main blog page (set up as a separate page and a "blog" link) matches as well, which beats the purpose of the if ().

    the second example is a category archive using the "category-xxx" protocol to assign the template to a page that's basically a pointer (user clicks on "answers" page, which goes to a Q&A category archive).

    All this is live here, if that helps:
    http://guptains.dreamhosters.com/

    Thanks for the tips!

    Posted 9 years ago #
  6. Oh, and I tried the is_page('home'), is_category('questions') first. No match. Strange, isn't it?

    Posted 9 years ago #
  7. Try using a different template name. the-best-page.php for example, and call that using the is_page_template tag. You might be messing with WordPress' Template Hierarchy: http://codex.wordpress.org/Template_Hierarchy

    Middlesister, I think I've pointed this out to you before in another thread ;)

    Posted 9 years ago #
  8. Okay, I'll try that in a few hours and report back. Thanks Joperron!

    Posted 9 years ago #
  9. i don't 100% follow what you are trying to achieve. are you using a template to adjust the index loop parameters? or are you trying to create a 2nd loop?

    if you are trying to implement a 2nd loop, i'd recommend using get_posts.
    http://codex.wordpress.org/Function_Reference/get_posts

    using a new query often messes up conditional logic, though supposedly you can use wp_reset_query to fix that.
    http://codex.wordpress.org/Function_Reference/wp_reset_query

    if you are trying to adjust the query params you can do that by adding query_posts with your desired arguments.
    http://codex.wordpress.org/Function_Reference/query_posts

    function restrict_indexloop() {
    	global $wp_query;
    	$args = array_merge( $wp_query->query, array( 'posts_per_page' => 1 ) );
    	query_posts( $args );
    }
    add_action ('thematic_above_indexloop', 'restrict_indexloop');

    lastly, your category conditional is failing b/c you have to put the condition INSIDE the function and not the other way around. in short

    function childtheme_override_category_loop() {
            if (is_page_template( 'category-questions.php' ) )  {
                //do the special bacon-flavored loop
            } else {
                //do the normal loop
    	}
    }

    i'm not a big fan of using page templates to show posts in a blog style. i've done it, but from what i can tell, i think most of your issues could be resolved w/o resorting to page templates.

    Posted 9 years ago #
  10. middlesister
    Member

    @joperron: lol yes that is true, and like I said I have never used a page template named home myself. Maybe I should try remembering to point out this potential issue for others too so you won't have to jump in every thread with a reminder ;)

    @helga: saw that too late, you are fast :-)

    @amorita: Helga is right, probably most of what you want to do can be done without using template files.

    But if you still want to go the template route: if you look at that template hierarchy page, you can see that if you have a file named category-questions.php it will be used as the category archive page for that category. You can say that the conditional is already met, everything on that page will only be used with that category. So in that case, there is neither a need for a conditional or a childtheme_override function, just write the loop the way you want it.

    Posted 9 years ago #
  11. Sorry, guys, nope.

    The funny thing is, the conditionals work in other places (e.g., the top header "answers", "welcome" etc. are generated by conditionals). Just not when I'm trying to manipulate something within the loop(s), it seems...

    hmmm. I am working on a work-around for the category archive page. Any insight/suggestion on the other, original issue would be much appreciated in the meantime...

    Posted 9 years ago #
  12. @helgatheviking, I didn't see your response when I was typing the last reply! Thanks! I will try and do it without the homepage.php template, and get back here (hopefully) with a documentation of how I solved it:)

    @middlesiser, For the category archive page, if I follow what you are saying (and you are right, I am using the template to be used as a category archive page for this particular category), I should write the loop directly in the template, not in the functions.php, correct?

    Cheers guys!

    Posted 9 years ago #
  13. middlesister
    Member

    Yes exactly. You either use childtheme_override_category_loop() with contitionals in your functions.php, or write the loop the way you want it without any conditonals directly in your page template.

    Posted 9 years ago #
  14. Thanks guys! I am making progress now.

    For the original issue of displaying the latest post in the static homepage, I am putting all the customization back into functions.php and using a default static page template for the homepage here:
    http://guptains.dreamhosters.com/

    I got it to work so far, by putting this in function.php

    // homepage content
    
    function gupta_homeindexloop () {
    if (is_front_page()) {
    	global $wp_query;
    	$args = array_merge( $wp_query->query, array( 'posts_per_page' => 1 ) );
    	query_posts( $args );
    	thematic_abovepost();
    	thematic_indexloop();
    }
    }
    add_action ('thematic_belowcontent', 'gupta_homeindexloop');

    The question is, how do I make it an excerpt instead of full post? I am not sure I am getting this quite right. The excerpts above this are generated by another set of queries, thus:

    function atlas_get_the_excerpt( $postID, $autoGenerate = true, $length = 20, $ellipsis = ' [...]' ) {
    
        $post = get_post( $postID );
        $title = get_the_title( $postID );
        $excerpt = $post->post_excerpt;
    
        if ( $excerpt == '' && $autoGenerate ) {
            $content = strip_tags( strip_shortcodes( $post->post_content ) );
            $content = str_replace( array( "\r\n", "\n\n", '  ' ), ' ', $content );
            $words = explode( ' ', $content );
            if ( count( $words ) > $length )
                $excerpt = implode( ' ', array_slice( $words, 0, $length ) ) . $ellipsis;
            else
                $excerpt = $content;
        }
    
        return $excerpt;
    }add_action('thematic_abovecontent','atlas_get_the_excerpt');

    ..and spitting them out by ID (this works for me here, because these are specific page contents that won't change much).

    And I am not sure if it's screwing with the rest of the page, or it's really inefficient, etc.

    As for the category page, I took Middlesister's advice and put the loop codes right in the template file. It's working fine now:)

    Posted 9 years ago #
  15. What I am trying now:

    function guptahome_content($content) {
    	if (is_front_page())  {
    	$content= 'excerpt';
    	}
    	return $content;
    }
    add_filter('thematic_abovepost', 'guptahome_content');

    Needless to say, it's not working...:(

    Posted 9 years ago #
  16. thematic_abovepost is not a filter. it is a hook. try:

    function guptahome_content($content) {
    	if (is_front_page())  {
    	$content= 'excerpt';
    	}
    	return $content;
    }
    add_filter('thematic_content', 'guptahome_content');

    never mind- that is back where you started. though perhaps it works now? that is the common way to switch to excerpts. is your front page also your blog index? if so, then is_home() might be better

    Posted 9 years ago #
  17. Thanks for the reply!

    The front page is a static page, so is_front_page _should_ be the right conditional, or so I thought, but for some reason I can't make it work... and if I use is_home() then both the front page and blog home get affected.

    Anyhow, this is where I am heading now:

    (back to using gupta_home.php as a home page template—I know, I know... but all this passing variables and filters and stuff is a little over this newbie's head, and I wanted to just see it work!)

    <?php
    			$wp_query = new WP_Query();
    			$wp_query->query( array( 'posts_per_page' => 1 ) );
    			$more = 0;
    			?>
    
    				<?php
    		global $options, $blog_id;
    
    		foreach ($options as $value) {
    		    if (get_option( $value['id'] ) === FALSE) {
    		        $$value['id'] = $value['std'];
    		    } else {
    		    	if (THEMATIC_MB)
    		    	{
    		        	$$value['id'] = get_option($blog_id,  $value['id'] );
    		    	}
    		    	else
    		    	{
    		        	$$value['id'] = get_option( $value['id'] );
    		    	}
    		    }
    		}
    
    		/* Count the number of posts so we can insert a widgetized area */ $count = 1;
    		while ( have_posts() ) : the_post();
    
    				thematic_abovepost(); ?>
    
    				<div id="post-<?php the_ID();
    					echo '" ';
    					if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    						post_class();
    						echo '>';
    					} else {
    						echo 'class="';
    						thematic_post_class();
    						echo '">';
    					}
         				//thematic_postheader(); ?>
    					<div class="entry-content">
    <?php $thematic_content_length='excerpt'; thematic_content($thematic_content_length); ?>
    
    					<?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
    					</div><!-- .entry-content -->
    				</div><!-- #post -->
    
    			<?php
    				$count = $count + 1;
    		endwhile;
                	?>

    while loop is superfulous as there's only one post to display, but I just lifted this from content_extentions.php and am afraid to break it:)

    Am I doing anything disastrously wrong?

    Cheers,
    Akira

    Posted 9 years ago #
  18. why is the front page a static page? are you actually displaying static content, or do you just want the home page to have different query parameters?

    Posted 9 years ago #
  19. There's some static content that's written in the template (like the sliding logos, link to the chat page, etc.). I guess the page doesn't _have_ to be static, but since the "blog" page is a separate index page, this works for me.

    I guess I didn't know how to make a homepage that's separate from the blog homepage (is_home()) that's not a page, either.

    Anyhow, I am making a template that's pretty much all custom stuff replacing all the action hooks... Not a refined way to get it done, but at least it's getting done:)

    Thanks for all your help though, your pointing out the if() needing to be inside the function was huge for me.

    Posted 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.