0

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,
) );
}

2 Answers 2

1

I have gone through your code and found that the function longform_the_related_posts displays 8 related posts. This function is defined in functions.php of parent theme and called in single.php.

Thus, to override the function, follow below steps -
1. Create another function that displays 4 related posts in functions.php of child theme. (As you have already created function longform_the_related_posts_change() )
2. Copy the file single.php from parent to child theme and replace the line 47

From

echo longform_the_related_posts();

To

echo longform_the_related_posts_change();

This would call the function that display 4 posts.
Hope this helps.

1
  • Thank you, that worked this way. I am stupid, that I did not see that myself. Still it is totally weird, why the function got not exchanged accordingly, or where it is hidden somewhere else. Crazy. :)
    – vega
    Aug 10, 2015 at 13:21
0

The above code looks okay. I think the reason why you are getting 8 posts instead of 4 is that, you still have a code that fetches 8 posts in parent theme's functions.php.

Try removing filter of parent theme in child theme. Like this -

remove_filter('<filter_name>','<function_name>');

By doing this, parent themes's function would not take effect and child theme's code would work.

For more info on remove_filter - https://codex.wordpress.org/Function_Reference/remove_filter

Hope this helps.

5
  • Thank you, but can you be a bit more specific on how that line/code should look like?
    – vega
    Aug 7, 2015 at 18:36
  • In parent theme, you should have a line - add_filter( 'longform_the_related_posts', 'longform_the_related_posts_change' ); Then you can do like this in child theme - remove_filter( 'longform_the_related_posts', 'longform_the_related_posts_change' ); Aug 10, 2015 at 5:48
  • What?! Nope, I do not have that line in the parent theme. I added this line in my child theme's functions.php to rename/alter the existing function "longform_the_related_posts" to "longform_the_related_posts_change". The point was to change the values of the function then. Sorry, I do not want to offend you, but do you know the Wordpress structure and the point of having a functions.php in the child theme and how to work with that?
    – vega
    Aug 10, 2015 at 11:14
  • Yes, I do understand the WordPress structure. Can you tell me, where is the existing code of displaying 8 articles written in parent theme? Inside functions.php or in any other .php file ? Aug 10, 2015 at 11:34
  • Alright, nice. :) Here is the source code. The code for the "related_posts" is sitting in the functions.php. Here is the code: github.com/cohhe/longform (functions.php).
    – vega
    Aug 10, 2015 at 11:53

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.