Last active
March 4, 2020 04:30
-
-
Save yeriepiscesa/2cf106786e539618c38f9066d25ad5be to your computer and use it in GitHub Desktop.
Kode dari artikel https://solusipress.com/menampilkan-listing-artikel-2-kolom-pada-tema-generatepress/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Display 2 columns post archive in GeneratePress theme */ | |
class SolusiPress_Post_Grid { | |
protected $post_count = 0; | |
protected $valid_area = false; | |
public function __construct() { | |
add_action( 'wp_head', array( $this, 'custom_css' ) ); | |
add_filter( 'post_class', array( $this, 'two_columns' ), 10, 3 ); | |
add_action( 'get_template_part', array( $this, 'before_template'), 10, 3 ); | |
add_filter( 'the_content', array( $this, 'remove_content' ) ); | |
add_action( 'generate_after_loop', array( $this, 'add_clearfix' ) ); | |
} | |
private function check_page() { | |
return ( is_home() || is_archive() ) && get_post_type($post_id) == 'post'; | |
} | |
public function custom_css() { | |
if( $this->check_page() ) { | |
echo '<style type="text/css"> .grid-no-content .entry-content { display:none; } </style>'; | |
} | |
} | |
public function two_columns( $classes, $class, $post_id ) { | |
if( $this->check_page() && $this->valid_area ) { | |
$this->post_count++; | |
$classes[] = "grid-50"; | |
$classes[] = "grid-no-content"; | |
} | |
$this->valid_area = false; | |
return $classes; | |
} | |
public function before_template( $slug, $name, $templates ) { | |
if( $slug == 'content' ) { | |
$this->valid_area = true; | |
if( $this->post_count % 2 == 0 ) { | |
$this->add_clearfix(); | |
} | |
} | |
} | |
public function remove_content( $content ) { | |
if( $this->post_count > 0 ) { | |
$content = ''; | |
} | |
return $content; | |
} | |
public function add_clearfix() { | |
if( $this->post_count > 0 ) { | |
echo '<div class="clearfix"></div>'; | |
} | |
} | |
} | |
new SolusiPress_Post_Grid(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment