ThemeShaper Forums » Thematic

[closed]

An OOOOPS!!! function mistake concerning featured posts

(7 posts)
  • Started 2 years ago by 4rederick
  • Latest reply from 4rederick
  • This topic is resolved
  1. With the help of Helga from this post (http://forums.themeshaper.com/topic/adding-function-to-home-page-only-headache) I was able to create a featured-post div on my front page. Unfortunately, the way I used the function seems to be resulting in having everything below the featured-post div in my html to carry the link to the featured post (for example, try clicking on my footer). Not too familiar with php yet, so I'm having a bit of trouble finding the error. Here is my the related function in my functions.php, followed by my home-page-template, which also might be a cause for the error. Probably a simple fix. Thanks ... Love this forum ...

    //==================== 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 ====================

    HERE IS MY HOME PAGE TEMPLATE

    <?php
    /*
    Template Name: Home
    */
    ?>

    <?php

    // calling the header.php
    get_header();

    // action hook for placing content above #container
    thematic_abovecontainer();

    ?>

    <div id="container">

    </div><!-- #container -->

    <?php

    // calling footer.php
    get_footer();

    ?>

    Posted 2 years ago #
  2. i think you forgot to reset the query. try adding:

    wp_reset_query();

    just after the endwhile

    http://codex.wordpress.org/Function_Reference/wp_reset_query

    this is why i always use get_posts.

    Posted 2 years ago #
  3. Sadly that doesn't work either ...

    //==================== 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;
    wp_reset_query();
    echo '</div>';

    }

    }
    add_action('thematic_abovecontainer','featured_posts');

    Posted 2 years ago #
  4. Thanks, Helga. I'm going to attempt to do this with get_posts. This post looks like it will help a lot: http://forums.themeshaper.com/topic/adding-a-list-of-recent-posts-to-a-custom-page-template#post-18192

    Posted 2 years ago #
  5. Yay! Feels so much better when you figure something out for yourself. Geesh, I might be learning something ;-)

    I removed the lines from functions.php and edited my page template as so ... seems to be working. Clean enough?

    <?php
    /*
    Template Name: Home
    */
    ?>

    <?php

    // calling the header.php
    get_header();

    // action hook for placing content above #container
    thematic_abovecontainer();

    ?>

    <div id="featured-post">
    <?php
    $args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date' );
    $postslist = get_posts( $args );
    foreach ($postslist as $post) : setup_postdata($post); ?>
    <div>
    <span class="date"><?php the_date(); ?></span>
    " id="post-<?php the_ID(); ?>"><?php the_title(); ?>
    <?php the_excerpt(); ?>

    </div>
    <?php endforeach; ?>
    </div>

    <div id="container">

    </div><!-- #container -->

    <?php

    // calling footer.php
    get_footer();

    ?>

    Posted 2 years ago #
  6. i know you are supposed to be able to use new WP_Query, but i have a lot fewer issues w/ get_post. congrats on solving it. don't forget to mark as resolved.

    Posted 2 years ago #
  7. Though I kind of like it sorted by date now, I was curious to know how I'd sort by category. Say I only wanted the posts from the category Smelly, how would I do that? If it's not a simple solution, don't worry about it. I'm sure I'll figure it out when I need it.

    Posted 2 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.