1

I modified the site title the 'the_title' filter. I am using ACF pro repeater, I want to achieve is that if there is content in the repeater a + will be added to the post title.

I wrote this script:

function mm_title_update( $title, $id = null ) {
    if ( ! is_admin() && ! is_null( $id ) ) {
        $post = get_post( $id );
        if (  $post->post_type == 'info' )  {
            $is_index = have_rows('index_copy', $post); //acf repeater field
   
            //check if the repeater is set
                if($is_index === false ){
                    return $title; 
                
                //die;
                }else{
                    $title .= '+';
                    return $title; 
                };
        }
    }
    
    return $title;
    
}
add_filter( 'the_title', 'mm_title_update', 1, 2 );

function mm_remove_title_filter_nav_menu( $nav_menu, $args ) {
    // we are working with menu, so remove the title filter
    remove_filter( 'the_title', 'mm_title_update', 1, 2 );
    return $nav_menu;
}
// this filter fires just before the nav menu item creation process
add_filter( 'pre_wp_nav_menu', 'mm_remove_title_filter_nav_menu', 1, 2 );

function mm_add_title_filter_non_menu( $items, $args ) {
    // we are done working with menu, so add the title filter back
    add_filter( 'the_title', 'mm_title_update', 1, 2 );
    return $items;
}
// this filter fires after nav menu item creation is done
add_filter( 'wp_nav_menu_items', 'mm_add_title_filter_non_menu', 1, 2 );

Now, the script works well on single and archive pages but it doesn't work on the site's front page.

in the front and archive pages I am using the same functionality to see the titles. the titles are called by a get_template_part from 'functions.php' file

this is the code in functions.php

 get_template_part( 'parts/title', 'loop' ); 

this is the title-loop.php file

<?php
/**
 * @package hello
 */
?>

<span class="flex infolink">
    <i aria-hidden="true" class="far fa-file-alt"></i>
    <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" class="cat-post-title">   
        <?php 
        isset()
        echo the_title(); 
        ?>
    </a>    
</span>

I don't understand why I do not see the + sign on post loop titles on the front page. any help would be amazing tnx

2
  • 1
    if ( $post->post_type == 'info' ) { // ... } this checks that the current post bieing viewed if of a post_type caled "info" - do you have your frontpage set to show such a page?
    – Q Studio
    Commented Jan 24, 2021 at 14:16
  • yes, thats the only post type I use in my site
    – DavSev
    Commented Jan 25, 2021 at 17:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.