Web Hosting
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trying to display a featured post

Discussion in 'Troubleshooting & How-To's' started by Cheryl, Oct 17, 2016.

  1. Cheryl

    New Member

    Joined:
    Oct 17, 2016
    Messages:
    1
    Likes Received:
    0
    Hi Guys,

    Really need help with this please:
    Im trying to display one featured post at the top of my front page. I have followed the WP Beginner tutorial but it is not working unfortunately. So here is the code I have done so far:

    In functions.php:
    function wpb_postsbycategory() {
    // the query
    $the_query = new WP_Query( array( 'category_name' => 'Featured', 'posts_per_page' => 1 ) );

    // The Loop
    if ( $the_query->have_posts() ) {
    $string .= '<ul class="postsbycategory widget_recent_entries">';
    while ( $the_query->have_posts() ) {
    $the_query->the_post();
    if ( has_post_thumbnail() ) {
    $string .= '<li>';
    $string .= '<a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_post_thumbnail($post_id, array( 50, 50) ) . get_the_title() .'</a></li>';
    } else {
    // if no featured image is found
    $string .= '<li><a href="' . get_the_permalink() .'" rel="bookmark">' . get_the_title() .'</a></li>';
    }
    }
    } else {
    // no posts found
    }
    $string .= '</ul>';

    return $string;

    /* Restore original Post Data */
    wp_reset_postdata();
    }
    // Add a shortcode
    add_shortcode('categoryposts', 'wpb_postsbycategory');

    // Enable shortcodes in text widgets
    add_filter('widget_text', 'do_shortcode');


    front-page.php:

    <?php


    get_header(); ?>

    <?php wpb_postsbycategory() ?>


    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2 class="blog-title">Recent Posts</h2>
    <div class='blog-posts'>
    <?php
    $args = array( 'posts_per_page' => '-1', 'offset' => 1 );
    $recent_posts = new WP_Query( $args );
    if( $recent_posts->have_posts() ) : while( $recent_posts->have_posts() ) : $recent_posts->the_post();

    echo "<div class='postWrapper'>

    <div class='featured_image'><a href=". get_permalink($post->ID). ">" . get_the_post_thumbnail($post->ID, 'large') . "</a></div>
    <div class='post_content'>
    <h2 class='postTitle'><a href=" . get_permalink($post->ID) . ">" . get_the_title() . "</a></h2>

    <div class='post'>" . get_the_excerpt( $post ) . "</div>
    <div class='readmore'><a href=". get_permalink($post->ID) . ">Read More...</a></div>
    </div>



    </div>";
    endwhile; endif; wp_reset_postdata();
    ?>
    </div>

    <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar("highlighted") ) : ?>
    <?php endif;?>

    <?php
    get_sidebar();
    get_footer();


    And then I created a category: "Featured" and added a blog post to this category but its showing nothing.

    Please help....
     
    #1

Share This Page

Web Hosting