Your IP : 216.73.216.1
<?php
/**
* Related Posts
*
* @package pally
* @since 1.0.0
*/
namespace Pally;
use WP_Customize_Manager;
use WP_Query;
/**
* Related Posts
*
* @since 1.0.0
*/
class Related_Posts {
/**
* Constructor.
*
* @since 1.0.0
* @access public
*/
public function __construct() {
add_action( 'customize_register', [ $this, 'action_register_customizer_control' ] );
add_action( 'related_posts', [ $this, 'related_posts' ], 10, 1 );
}
/**
* Adds a Customizer setting and control.
*
* @param WP_Customize_Manager $wp_customize Customizer manager instance.
* @since 1.0.0
*/
public function action_register_customizer_control( WP_Customize_Manager $wp_customize ) {
$wp_customize->add_section(
'related_posts_options',
array(
'title' => __( 'Related Posts', 'pally' ),
'panel' => 'theme_options',
)
);
$wp_customize->add_setting(
'show_related_posts',
array(
'default' => true,
'transport' => 'postMessage',
'sanitize_callback' => 'Pally\Customizer::sanitize_checkbox',
)
);
$wp_customize->add_control(
'show_related_posts',
array(
'type' => 'checkbox',
'label' => __( 'Related Posts', 'pally' ),
'description' => __( 'Enable Related Posts below the post content. Posts are related by tags.', 'pally' ),
'section' => 'related_posts_options',
)
);
$wp_customize->selective_refresh->add_partial(
'show_related_posts',
array(
'selector' => '.related-posts',
'container_ally' => true,
)
);
$wp_customize->add_setting(
'related_section_heading',
array(
'default' => __( 'Related Posts', 'pally' ),
'sanitize_callback' => 'sanitize_text_field',
)
);
$wp_customize->add_control(
'related_section_heading',
array(
'type' => 'text',
'label' => __( 'Related Posts Section Heading', 'pally' ),
'description' => __( 'Add a custom heading for the related posts section', 'pally' ),
'section' => 'related_posts_options',
)
);
$wp_customize->add_setting(
'number_of_related_posts',
array(
'default' => 3,
'transport' => 'postMessage',
'sanitize_callback' => 'absint',
)
);
$wp_customize->add_control(
'number_of_related_posts',
array(
'type' => 'number',
'label' => __( 'Number of Related Posts', 'pally' ),
'section' => 'related_posts_options',
)
);
$wp_customize->selective_refresh->add_partial(
'number_of_related_posts',
array(
'selector' => '.single .related-posts-entries',
)
);
}
/**
* Output related posts.
*
* @param int $post_id The ID of the current post.
* @since 1.0.0
* @return void
*/
public static function related_posts( $post_id ) {
if ( get_theme_mod( 'show_related_posts', true ) === true ) {
$tags = get_the_tags( $post_id );
if ( $tags ) {
$tag_ids = [];
$tag = get_tags( $tag_ids );
foreach ( $tags as $tag ) {
$tag_ids[] = $tag->term_id;
}
$count = $tag->count;
if ( $count >= 1 ) {
?>
<div class="related-posts">
<h2 class="rp-title"><?php echo esc_html( get_theme_mod( 'related_section_heading', __( 'Related Posts', 'pally' ) ) ); ?></h2>
<div class="rp-holder">
<?php
$tag_post_args = [
'tag__in' => $tag_ids,
'post__not_in' => [ $post_id ],
'post_type' => 'post',
'posts_per_page' => get_theme_mod( 'number_of_related_posts', 3 ),
'post_status' => 'publish',
'ignore_sticky_posts' => true,
];
$pally_related_posts_query = new WP_Query( $tag_post_args );
while ( $pally_related_posts_query->have_posts() ) {
$pally_related_posts_query->the_post();
?>
<a href="<?php the_permalink(); ?>"><?php the_title();?></a>
<?php
} //End While.
wp_reset_postdata();
?>
</div>
</div> <!-- .related-posts -->
<?php
}
}
}
}
}