I am working on dailysuit.de, which features 8 related articles below the content in every post, defined by some tags.
Now I wanted to reduce the number of "posts per page" from 8 to 4. Of course, I can easily achieve that by changing the functions.php, but as soon as the theme gets an update, the figure is back to 8.
So, I wanted to change the function and add it to the child theme's functions.php:
/* Related posts */
function longform_the_related_posts_change() {
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) {
$tag_ids[] = $individual_tag->term_id;
}
$args = array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page' => 4, // Number of related posts to display.
'ignore_sticky_posts' => 1
);
$my_query = new wp_query( $args ); ?>
<h2 class="related-articles-title"><?php _e( 'Related articles', 'longform' ); ?></h2>
<div class="related-articles">
<?php
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div class="related-thumb col-sm-3 col-md-3 col-lg-3">
<a rel="external" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(array(500,350)); ?>
<div class="related-content">
<h2><?php the_title(); ?></h2>
</div>
</a>
</div>
<?php } ?>
<div class="clearfix"></div>
</div>
<?php
}
wp_reset_postdata();
wp_reset_query();
}
add_filter( 'longform_the_related_posts', 'longform_the_related_posts_change' );
Unfortunately, that did not work. It still shows 8 relates posts instead of the 4, that I entered at "posts_per_page".
Anyone knows what is wrong there? Or is there a easier/shorter way without copying all this code?
I also tried the remove_filter thing, that was mentioned, but that did not work out either.
longform_the_related_posts_child () {
remove_filter('longform_the_related_posts');
add_filter('longform_the_related_posts', array(
'longform_the_related_posts_filter' => 'longform_the_related_posts',
'posts_per_page' => 4,
) );
}