Title: functions.php
Last modified: August 12, 2025

---

# functions.php

 *  Resolved [gwenm](https://wordpress.org/support/users/gwenm/)
 * (@gwenm)
 * [10 months, 2 weeks ago](https://wordpress.org/support/topic/functions-php-13/)
 * Hello, I’m using your theme and I can’t customize it. Is it because it’s a block
   theme? I created a child theme and added functions.php (I’m used to this kind
   of thing). My goal is to make the same article as the current page disappear (
   in the widget) to avoid duplication, and above all, it’s of no interest to the
   user.
 * I tested these two codes, which work in a normal theme, but yours doesn’t. So
   what should I do? Or is the problem elsewhere?
 *     ```wp-block-code
       function exclude_current_post_from_recent_widget($args) {if (is_single()) {$args['post__not_in'] = array(get_the_ID());}return $args;}add_filter('widget_posts_args', 'exclude_current_post_from_recent_widget');
       ```
   
 *     ```wp-block-code
       // Remplacer le widget "Articles récents" par une version qui exclut l'article courantfunction custom_exclude_recent_posts_widget($args) {    $number = get_option('widget_recent_entries');    $number = !empty($number[2]['number']) ? absint($number[2]['number']) : 5;    $query_args = array(        'post_type'      => 'post',        'post_status'    => 'publish',        'posts_per_page' => $number,        'post__not_in'   => is_single() ? array(get_the_ID()) : array(),    );    $recent_posts = new WP_Query($query_args);    echo $args['before_widget'];    $title = apply_filters('widget_title', 'Articles récents');    if ($title) {        echo $args['before_title'] . $title . $args['after_title'];    }    if ($recent_posts->have_posts()) {        echo '<ul>';        while ($recent_posts->have_posts()) {            $recent_posts->the_post();            echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';        }        echo '</ul>';        wp_reset_postdata();    } else {        echo '<p>Aucun article récent.</p>';    }    echo $args['after_widget'];}// Remplacer le widget par défaut par notre versionfunction replace_recent_posts_widget() {    unregister_widget('WP_Widget_Recent_Posts');    wp_register_sidebar_widget(        'custom_recent_posts',        'Articles récents (exclut courant)',        'custom_exclude_recent_posts_widget',        array(            'description' => 'Affiche les articles récents SANS l’article actuellement lu.',            'classname'   => 'widget_recent_entries',        )    );}add_action('widgets_init', 'replace_recent_posts_widget', 11);
       ```
   
 * Thank you for help^^

Viewing 1 replies (of 1 total)

 *  Thread Starter [gwenm](https://wordpress.org/support/users/gwenm/)
 * (@gwenm)
 * [10 months, 1 week ago](https://wordpress.org/support/topic/functions-php-13/#post-18597048)
 * The most common (and technical) solution: Given the current limitations of the
   default block, the most efficient solution is to use code. You’ll need to use
   a code snippet that modifies the loop query.
 * How to do this?
 * Create a child theme if you haven’t already.
 * Edit your child theme’s functions.php file.
 * Add code that uses the pre_get_posts or query_loop_block_query filter to exclude
   the current post. The code should look something like this:
 *     ```wp-block-code
       function exclude_current_post_from_query_loop( $query ) {    if ( $query->get( 'post_type' ) === 'post' && is_single() && is_main_query() ) {        $current_post_id = get_the_ID();        $query->set( 'post__not_in', array( $current_post_id ) );    }}add_action( 'pre_get_posts', 'exclude_current_post_from_query_loop' );
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘functions.php’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/castcore/1.0.0/screenshot.png)
 * CastCore
 * [Support Threads](https://wordpress.org/support/theme/castcore/)
 * [Active Topics](https://wordpress.org/support/theme/castcore/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/castcore/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/castcore/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [gwenm](https://wordpress.org/support/users/gwenm/)
 * Last activity: [10 months, 1 week ago](https://wordpress.org/support/topic/functions-php-13/#post-18597048)
 * Status: resolved