ThemeShaper Forums » Thematic

[closed]

Adding function to home page only ( headache! )

(12 posts)
  • Started 4 years ago by mogito2000
  • Latest reply from 4rederick
  • This topic is not resolved
  1. mogito2000
    Member

    Hi there

    Im fairly new to php and am having a majotr headache trying to get is_home() working on this function...i have read through tutorials and have tried implimenting it numerous way but always seem to end up with the white screen of death!

    Here is the funtion:

    function featured_posts(){

    $my_query = new WP_Query('tag=featured&showposts=2');

    echo '<div id="featured-posts">';
    $feat_class = array();

    while ($my_query->have_posts()) : $my_query->the_post();
    $feat_class = array();
    // Category for the post queried
    foreach ( (array) get_the_category() as $cat )
    $feat_class[] = 'category-' . $cat->slug;
    $feat_class = join(" ", $feat_class);
    ?>
    <div id="featured-<?php the_ID(); ?>" class="<?php echo $feat_class; ?>">
    <?php
    $posttitle = '<h4><a href="';
    $posttitle .= get_permalink();
    $posttitle .= '" title="';
    $posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
    $posttitle .= '" rel="bookmark">';
    $posttitle .= get_the_title();
    $posttitle .= "</h4>\n";
    echo $posttitle;

    get_the_image();
    the_excerpt();
    ?>

    </div><!-- .post -->
    <?php
    endwhile;
    echo '</div>';
    }
    add_action('thematic_above_indexloop','featured_posts');

    Could someone point me in the right direction?

    Thank you for any help!

    Mogito

    Posted 4 years ago #
  2. you know that you know you don't have any conditional logic in the code you've posted?

    function featured_posts(){
    
    if (is_home()) {
    	echo "bacon!";
    	}
    
    }
    add_action('thematic_above_indexloop','featured_posts');

    puts bacon! in the anticipated position on only the home page. if you get nothing from your above code (on any page) then you don't have a conditional problem, you have a problem w/ your query or loop.

    Posted 4 years ago #
  3. mogito2000
    Member

    Thanks for your reply, yeah i know there is are no conditions in the supplied code - the code works as it is but im trying to add a condition to it. Im just not very sure where to start when adding the condition...

    ...I understand the concept of the code you posted, its just trying to slot a condition into an already existed function thats confusing me!!

    Ie how would i add a a condition the the code i have....? whenever i try, it seems to brake it...

    Posted 4 years ago #
  4. you just sub in the meat of your function for my echo "bacon". the conditional logic goes INSIDE the function.

    function featured_posts(){
    
    if (is_home()) {
    
    $my_query = new WP_Query('tag=featured&showposts=2');
    
    echo '<div id="featured-posts">';
    $feat_class = array();
    
    while ($my_query->have_posts()) : $my_query->the_post();
    $feat_class = array();
    // Category for the post queried
    foreach ( (array) get_the_category() as $cat )
    $feat_class[] = 'category-' . $cat->slug;
    $feat_class = join(" ", $feat_class);
    ?>
    <div id="featured-<?php the_ID(); ?>" class="<?php echo $feat_class; ?>">
    <?php
    $posttitle = '<h4><a href="';
    $posttitle .= get_permalink();
    $posttitle .= '" title="';
    $posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
    $posttitle .= '" rel="bookmark">';
    $posttitle .= get_the_title();
    $posttitle .= "</h4>\n";
    echo $posttitle;
    
    //get_the_image();
    the_excerpt();
    ?>
    
    </div><!-- .post -->
    <?php
    endwhile;
    echo '</div>';
    
    }
    
    }
    add_action('thematic_above_indexloop','featured_posts');
    Posted 4 years ago #
  5. mogito2000
    Member

    mmmmm.......thats what i thought. I tried your code and i get the same death screen... It seems as though the code doesnt like the conditional logic. Can you see any reason for this?

    Thanks very much for your thoughts on this by the way....my head is fried from looking at code today!

    Posted 4 years ago #
  6. what about if you ONLY have that function? meaning get rid of your other functions to isolate it. still white screen?

    add this to your wp-config.php file in the root of your wp install

    define('WP_DEBUG', true);

    it will help you see what errors are causing your white screen of death.

    idk what further to tell you as the above code worked just fine on my local machine. in fact, i had better bookmark this as it is something i'd like to implement w/ a slider in the future.

    my brain is also pretty scrambled at this point myself so i can't investigate more right now.

    Posted 4 years ago #
  7. mogito2000
    Member

    Hi there,

    Removing all the other code in fucntions.php seemed to do the trick, so i guess i had an error elsewhere.....!

    With regards to define('WP_DEBUG', true); - i had a lot of depreciated code, i know it sounds stupid but would you advise taking care of everything that debug reports even if its not causing a parse error?

    Thanks for your help on the above problem helgatheviking , im now much better equipped to debug any future problems on my own!

    Posted 4 years ago #
  8. www.josephfrederick.com

    I used this great function (thanks, Helga) but for some reason the home conditional doesn't seem to be working correctly. I want the featured post to only appear on the Home Page (I'm going to style it and place it in its own box in the upper right corner) but now it is appearing instead on the Blog page. Is my code right? Could something else be affecting it?

    //==================== START Add featured posts on home page ====================
    function featured_posts(){

    if (is_home()) {

    $my_query = new WP_Query('tag=featured&showposts=1');

    echo '<div id="featured-posts">';
    $feat_class = array();

    while ($my_query->have_posts()) : $my_query->the_post();
    $feat_class = array();
    // Category for the post queried
    foreach ( (array) get_the_category() as $cat )
    $feat_class[] = 'category-' . $cat->slug;
    $feat_class = join(" ", $feat_class);
    ?>
    <div id="featured-<?php the_ID(); ?>" class="<?php echo $feat_class; ?>">
    <?php
    $posttitle = '<h4><a href="';
    $posttitle .= get_permalink();
    $posttitle .= '" title="';
    $posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
    $posttitle .= '" rel="bookmark">';
    $posttitle .= get_the_title();
    $posttitle .= "</h4>\n";
    echo $posttitle;

    //get_the_image();
    the_excerpt();
    ?>

    </div><!-- .post -->
    <?php
    endwhile;
    echo '</div>';

    }

    }
    add_action('thematic_abovecontainer','featured_posts');
    //==================== END Add featured posts on home page ====================

    Posted 3 years ago #
  9. Nevermind, I solved it :)

    Changed it to read ...

    if (is_front_page()) {

    Posted 3 years ago #
  10. kwight
    Member

    Yes, what we think of as the "home page" has the conditional tag of is_front_page(), while is_home() refers to the blog page... not the most intuitive.

    Posted 3 years ago #
  11. Not sure if I should start a new thread or not ... But I think I got an "oops!!" here.
    So I pasted this is in my functions.php (see below) and now every item under that featured posts div also carries the link to the featured post. I have no idea how to fix this. I'm guessing there's a modifier that needs changed in the function, but not yet experienced enough to spot it. Suggestions?

    //==================== START Add featured posts on home page ====================
    function featured_posts(){

    if (is_front_page()) {

    $my_query = new WP_Query('tag=featured&showposts=1');

    echo '<div id="featured-posts">';
    $feat_class = array();

    while ($my_query->have_posts()) : $my_query->the_post();
    $feat_class = array();
    // Category for the post queried
    foreach ( (array) get_the_category() as $cat )
    $feat_class[] = 'category-' . $cat->slug;
    $feat_class = join(" ", $feat_class);
    ?>
    <div id="featured-<?php the_ID(); ?>" class="<?php echo $feat_class; ?>">
    <?php
    $posttitle = '<h4>featured... <a href="';
    $posttitle .= get_permalink();
    $posttitle .= '" title="';
    $posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
    $posttitle .= '" rel="bookmark">';
    $posttitle .= get_the_title();
    $posttitle .= "</h4>\n";
    echo $posttitle;

    //get_the_image();
    the_excerpt();
    ?>

    </div><!-- .post -->
    <?php
    endwhile;
    echo '</div>';

    }

    }
    add_action('thematic_abovecontainer','featured_posts');
    //==================== END Add featured posts on home page ====================

    Posted 3 years ago #
  12. Worried about etiquette here, so I started a new thread ...

    http://forums.themeshaper.com/topic/an-oooops-function-mistake-concerning-featured-posts#post-21365

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.