• Resolved bobbyblank

    (@bobbyblank)


    I’ve seen a thousand solutions to editing template-tags.php by adding stuff to the functions.php file, but none work…they just wreck the theme. I can change the template-tags.php file in the parent folder and get exactly what I want, but I can’t figure out how to bring the edits into a child theme. All I need to do is drop categories and tags onto their own lines with a
    tag.

    I changed this:

    printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',

    to this:

    printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span><br />',

    and it works perfectly – but I had to make the change in the parent theme’s template-tags.php

Viewing 7 replies - 1 through 7 (of 7 total)
  • Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    That code is inside a function, as you can see:

    if ( ! function_exists( 'twentyfifteen_entry_meta' ) ) :
    /**
     * Prints HTML with meta information for the categories, tags.
     *
     * @since Twenty Fifteen 1.0
     */
    function twentyfifteen_entry_meta() {
    [...]
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
    		if ( $categories_list && twentyfifteen_categorized_blog() ) {
    			printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'twentyfifteen' ),
    				$categories_list
    			);
    		}
    [..]
    }
    endif;

    At the start of the function is a bit of code that checks to see whether the function exists:

    if ( ! function_exists( 'twentyfifteen_entry_meta' ) ) :

    If the function already exists, for example if you declared it in your Child Theme, then the parent function will not run.

    So, copy the whole function and paste it into your Child Theme functions.php file. Then modify it there.

    Thread Starter bobbyblank

    (@bobbyblank)

    Doesn’t seem to work – does the same thing all the other solutions I tried – adds a bunch of crazy code at the top of my blog.

    My entire functions.php looks like this, is it correct? –

    <?php
    /*
    *  Connect Style Sheets
    */
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style', get_stylesheet_uri(), array( 'parent-style' ) );
    }
    ?>
    
    if ( ! function_exists( 'twentyfifteen_entry_meta' ) ) :
    /**
     * Prints HTML with meta information for the categories, tags.
     *
     * @since Twenty Fifteen 1.0
     */
    function twentyfifteen_entry_meta() {
    [...]
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
    		if ( $categories_list && twentyfifteen_categorized_blog() ) {
    			printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span><br />',
    				_x( 'Categories', 'Used before category names.', 'twentyfifteen' ),
    				$categories_list
    			);
    		}
    [..]
    }
    endif;
    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Don’t use the code I wrote above 🙂 I wrote it for demonstration purposes. If you use it I wouldn’t be surprised if your website broke. Fortunately it did not break because you closed off php (?>).

    Make sure that PHP is inserted before the closing php tag (?>).

    Thread Starter bobbyblank

    (@bobbyblank)

    Whatever I did, it messed it up again. I couldn’t even get back in the editor to fix it – I had to undo in FTP.

    Where does the function that needs to be replaced begin & end? Do I need the […} and [..} parts? Where does the ?> part go? Do I need to include the if ( ! function_exists( 'twentyfifteen_entry_meta' ) ) : part, or do I start with function twentyfifteen_entry_meta() {?

    My parent template-tags.php portion that I need to edit is this – I just don’t know exactly what parts need to be added to the child’s functions.php.

    if ( ! function_exists( 'twentyfifteen_entry_meta' ) ) :
    /**
     * Prints HTML with meta information for the categories, tags.
     *
     * @since Twenty Fifteen 1.0
     */
    function twentyfifteen_entry_meta() {
    	if ( is_sticky() && is_home() && ! is_paged() ) {
    		printf( '<span class="sticky-post">%s</span>', __( 'Featured', 'twentyfifteen' ) );
    	}
    
    	$format = get_post_format();
    	if ( current_theme_supports( 'post-formats', $format ) ) {
    		printf( '<span class="entry-format">%1$s<a href="%2$s">%3$s</a></span>',
    			sprintf( '<span class="screen-reader-text">%s </span>', _x( 'Format', 'Used before post format.', 'twentyfifteen' ) ),
    			esc_url( get_post_format_link( $format ) ),
    			get_post_format_string( $format )
    		);
    	}
    
    	if ( in_array( get_post_type(), array( 'post', 'attachment' ) ) ) {
    		$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    
    		if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    			$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    		}
    
    		$time_string = sprintf( $time_string,
    			esc_attr( get_the_date( 'c' ) ),
    			get_the_date(),
    			esc_attr( get_the_modified_date( 'c' ) ),
    			get_the_modified_date()
    		);
    
    		printf( '<span class="posted-on"><span class="screen-reader-text">%1$s </span><a href="%2$s" rel="bookmark">%3$s</a></span>',
    			_x( 'Posted on', 'Used before publish date.', 'twentyfifteen' ),
    			esc_url( get_permalink() ),
    			$time_string
    		);
    	}
    
    	if ( 'post' == get_post_type() ) {
    		if ( is_singular() || is_multi_author() ) {
    			printf( '<span class="byline"><span class="author vcard"><span class="screen-reader-text">%1$s </span><a class="url fn n" href="%2$s">%3$s</a></span></span>',
    				_x( 'Author', 'Used before post author name.', 'twentyfifteen' ),
    				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    				get_the_author()
    			);
    		}
    
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
    		if ( $categories_list && twentyfifteen_categorized_blog() ) {
    			printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Categories', 'Used before category names.', 'twentyfifteen' ),
    				$categories_list
    			);
    		}
    
    		$tags_list = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
    		if ( $tags_list ) {
    			printf( '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    				_x( 'Tags', 'Used before tag names.', 'twentyfifteen' ),
    				$tags_list
    			);
    		}
    	}
    
    	if ( is_attachment() && wp_attachment_is_image() ) {
    		// Retrieve attachment metadata.
    		$metadata = wp_get_attachment_metadata();
    
    		printf( '<span class="full-size-link"><span class="screen-reader-text">%1$s </span><a href="%2$s">%3$s &times; %4$s</a></span>',
    			_x( 'Full size', 'Used before full size attachment link.', 'twentyfifteen' ),
    			esc_url( wp_get_attachment_url() ),
    			$metadata['width'],
    			$metadata['height']
    		);
    	}
    
    	if ( ! is_single() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
    		echo '<span class="comments-link">';
    		/* translators: %s: post title */
    		comments_popup_link( sprintf( __( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'twentyfifteen' ), get_the_title() ) );
    		echo '</span>';
    	}
    }
    endif;
    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    You need to add the entire function, for example:

    <?php
    /*
    *  Connect Style Sheets
    */
    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-style', get_stylesheet_uri(), array( 'parent-style' ) );
    }
    
    function twentyfifteen_entry_meta() {
    [...]
    		$categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentyfifteen' ) );
    		if ( $categories_list && twentyfifteen_categorized_blog() ) {
    			printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span><br />',
    				_x( 'Categories', 'Used before category names.', 'twentyfifteen' ),
    				$categories_list
    			);
    		}
    [..]
    }
    
    ?>

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Replace the [...] bits in my code with the actual code, or just copy the entire function

    Thread Starter bobbyblank

    (@bobbyblank)

    that worked – thanks!!

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘edit template-tags.php’ is closed to new replies.