0

I'm using the Gazette theme which has a jet pack enabled featured posts grid when the front page is set to show latest posts.

I would like to use a static page instead of recent posts, but keep the featured posts grid.


UPDATE

I was able to figure this out with a little moral support from the community

Link to answer - Display Featured Posts Grid on Static Page (Gazette)


Demo: https://gazettedemo.wordpress.com/

Source: https://public-api.wordpress.com/rest/v1/themes/download/gazette.zip

Code parts associated with the featured posts

@ index.php

<?php
    if ( is_home() ) {
        // Include the featured content template.
        get_template_part( 'featured-content' );
    }
?>

    <?php if ( have_posts() ) : ?>

        <?php /* Start the Loop */ ?>
        <?php while ( have_posts() ) : the_post(); ?>

            <?php
                get_template_part( 'content', get_post_format() );
            ?>

        <?php endwhile; ?>

        <?php the_posts_navigation(); ?>

    <?php else : ?>

        <?php get_template_part( 'content', 'none' ); ?>

    <?php endif; ?>

@ functions.php line 197

wp_enqueue_script( 'gazette-featured-content', 
get_template_directory_uri() . '/js/featured-content.js', 
array( 'jquery' ), '20150507', true );

@ featured-content.php

$featured_posts = gazette_get_featured_posts();
if ( empty( $featured_posts ) ) {
    return;
}
?>
    <?php
        foreach ( $featured_posts as $post ) {
            setup_postdata( $post );

             // Include the featured content template.
            get_template_part( 'content', 'featured-post' );
        }

        wp_reset_postdata();
    ?>

@ content-featured-post.php

/**
 * The template for displaying featured posts on the front page
 */
<?php
    // Output the featured image.
    if ( has_post_thumbnail() ) {
        the_post_thumbnail( 'gazette-featured-content-thumbnail' );
    }
?>

@ inc/jetpack.php line 22

add_theme_support( 'featured-content', array(
    'filter'      => 'gazette_get_featured_posts',
    'description' => __( 'Omitted', 'gazette' ),
    'max_posts'   => 6,
) );

@ js/featured-content.js line 24

var featuredContent, header, primary;
featuredContent = $( '#featured-content' );
header = $( '#masthead' );
primary = $( '#primary' );

if ( ! featuredContent.length ) {
    return;
}

@ js/header.js line 24

function headerStyle() {
    var featuredContent, featuredContentHeight;

    featuredContent = $( '#featured-content' );
    featuredContentHeight = 0;
    if ( featuredContent.length ) {
        featuredContentHeight = featuredContent.outerHeight();
    }

    if ( $( '.site-branding' ).outerWidth() === 0 ) {
        $( 'body' ).addClass( 'no-branding' );
    } else {
        $( 'body' ).removeClass( 'no-branding' );
    }

These are all the references I found to the featured posts grid layout.

How can I modify to display the grid on the homepage setup as a static page?

Notes

I added this to the page.php and it does show up in the source for static, but simply does not display.

if ( is_page(35) ) {
    // Include the featured content template.
    get_template_part( 'featured-content' );
}

2 Answers 2

1

Bingo. I added the featured content template (as originally and later suggested by @user53340) to the page.php with a condition so the featured grid would only display on the page I designate.

Add to page.php

if ( is_page(ID) ) {
    // Include the featured content template.
    get_template_part( 'featured-content' );
}

After changing the front page display to my static page I noticed the featured content was being output in the source, however it was not showing up on the page itself.

I realized the body classes determined if this would work or not. All I had to do is add blog to the body class for that page. The front page display sets the body class to page for static page and blog for posts page.

Using the code below I appended the blog class to the body.

function add_blog_to_body_class($classes) {
    $classes[] = 'blog';
        return $classes;
}
add_filter('body_class','add_blog_to_body_class');

I am able to set my static page & have my featured posts grid :D

2
  • Please accept your own answer to prevent the system marking this question as unresolved.
    – cjbj
    Commented Aug 20, 2016 at 14:58
  • I will in 23 (and counting) hours, when the "system" will allow it. Thank you. Commented Aug 20, 2016 at 14:59
0

If I understand your question which is to be honest a little vague, you should be able to change if ( is_home() ) to if ( is_front_page() ) This will display the 'featured-content' template part on a static front page (i.e a home page) instead of the blog page. In Wordpress is_home = blog home page and is_front_page = static front page. A little confusing, but if you think WordPress was originally made for blogging, it makes perfect sense. is_home explained here is_front_page explained here

EDIT: If you want the featured grid to display everywhere, you should be able to remove the if statement so it just reads get_template_part( 'featured-content' ); that's assuming I have understood you correctly, and based on the limited code you have supplied. Hope this helps anyway.

10
  • Unfortunately I can't get it to work. Here's a direct link. The index.php is the posts page with the featured area, page.php is where I believe I would have the featured area using if (is_page('ID')) public-api.wordpress.com/rest/v1/themes/download/gazette.zip Commented Aug 19, 2016 at 23:28
  • Have you tried if (is_page('slug')) for example if (is_page('about')) so the slug is the URL friendly name of your page title. You will need to have the right permalink structure set in Wordpress to use the title as the slug I believe, but is_page('') should also work with the id. You should still be able to just use get_template_part() without the if statement and that should work. If your link is to the files, I will download and check in a moment for you.
    – user53340
    Commented Aug 20, 2016 at 7:05
  • Good morning, I will take another look into this issue shortly and share what I've tried. I greatly appreciate your insight :) Commented Aug 20, 2016 at 11:49
  • get_template_part() is definitely working. The issue is with the JavaScript file. When you use get_template_part() I can see the HTML is there, it's just the js that is stopping it from rendering. That's what you need to look at.
    – user53340
    Commented Aug 20, 2016 at 11:59
  • Are you suggesting that it may not be called for pages? Commented Aug 20, 2016 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.