jakech
asked 5 years ago

Is there a way to override/select which posts appear on the home grid? (I’m trying to keep the Home grid clean and simple.) Thanks.

2 Answers
Allen
answered 5 years ago

@jake: hi i can suggest a solution for you, you can try this:

At first, you need to add a new metabox to Post page to make option that can exclude the post from home page like this :

function dw_fixel_exclude_post_in_home() {
    add_meta_box( 'dw_fixel_exclude_post_in_home', 'Exclude ?', 'dw_fixel_exclude_post_in_home_callback', 'post', 'normal','low');
}
add_action( 'admin_init', 'dw_fixel_exclude_post_in_home' );

function dw_fixel_exclude_post_in_home_callback() {
?>
    <table>
        <tr>
            <td> Exclude from home page? </td>
            <td>
                <?php 
                    $dwpb_enable = get_post_meta( get_the_ID(), '_dw_fixel_exclude_enable', true );
                        $dw_fixel_exclude_from_hompage = '';
                        if ( $dwpb_enable == 'yes' ) {
                            $dw_fixel_exclude_from_hompage = 'checked';
                        }
                ?>
                <label style="margin-right: 50px;"><input type="radio" name="dwpb_enable" value="no" checked><?php _e('No','dw-fixel') ?></label>
                <label style="margin-right: 50px;"><input type="radio" name="dwpb_enable" value="yes" <?php echo $dw_fixel_exclude_from_hompage; ?> ><?php _e('Yes','dw-fix') ?></label>
            </td>
        </tr>
    </table>
<?php
}

function dw_fixel_include_post_in_home_save() {
    if( ! empty( $_POST['dwpb_enable'] ) ) {
        update_post_meta( get_the_ID(), '_dw_fixel_exclude_enable', $_POST['dwpb_enable'] );
    }
}
add_action( 'save_post', 'dw_fixel_include_post_in_home_save' );

After that , you can go to file index.php,
from line 9–>21 change the code like this :

<?php  while( have_posts() ) : the_post(); ?>
            <?php 
            if ( 'yes' != get_post_meta( get_the_ID(), '_dw_fixel_exclude_enable', true ) ) {
                    $block_class = 'block';
                    if( dw_is_featured_post() ) { 
                        $block_class .= ' w2';
                    } 
                    if ( $first && ! dw_is_featured_post() ) {
                        $first = false;
                        $block_class .= ' grid-sizer';
                    }
                ?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            } 
        <?php endwhile; ?>

Hope it help.

jakech
replied 5 years ago

OK, thanks, I’ll give this a try.

jakech
replied 5 years ago

@nobita: Thanks for your help. I can’t quite get this to work. The metabox is not appearing. Can you tell me where I should put the first block of code? I’m currently putting it at the end of /my_site/wp-admin/post.php. Is this correct? But when I create a new test Post, there is no metabox option. Apologies, I am a beginner at WP.

dominic Staff
replied 5 years ago

You can add this code to the functions.php file. If you still face there issue, you can send me username & password of your site (via private answer) we will help you resolve this issue.

jakech
answered 5 years ago

@Dominic – thanks, I added the functions to functions.php and can see the metabox now. However, when I add a new test Post, select "Exclude from home page", then publish it seems to have no effect, i.e. the Post still appears on the home page.

As per instructions above, the theme’s index.php file has been modified as below. (I assume the functions.php file is correctly amended since I can see the metabox – I simply pasted the above functions to the bottom of the functions.php file):

<?php 
global $paged, $block_class, $paged;

get_header(); 
?>  
<?php if( have_posts() ) : ?>
    <div id="main" role="main" class="masonry" >
        <?php $first = true; ?>

        <?php  while( have_posts() ) : the_post(); ?>
            <?php 
            if ( 'yes' != get_post_meta( get_the_ID(), '_dw_fixel_exclude_enable', true ) ) {
                    $block_class = 'block';
                    if( dw_is_featured_post() ) { 
                        $block_class .= ' w2';
                    } 
                    if ( $first && ! dw_is_featured_post() ) {
                        $first = false;
                        $block_class .= ' grid-sizer';
                    }
                ?>
                <?php get_template_part( 'content', get_post_format() ); ?>
            } 
        <?php endwhile; ?>

        <?php dw_paging_nav(); ?>
    </div>
    <?php dw_show_more_button(); ?>
<?php else: ?>
    <?php get_template_part( 'content', 'none' ) ?>
<?php endif; ?>
<?php get_footer(); ?>

Any ideas? Thanks!

nobita
replied 5 years ago

@jakech: sorry about that problem, i found out that some more code need to add to make this work correctly but in the lastest update version of dw fixel ( version 1.1.0 ) , i already add this feature to the theme and re-config the widget , post at category page. You can try download the new version now , glad.

jakech
replied 5 years ago
Powered by DW Question & Answer Pro