• Resolved cascadingstyles

    (@cascadingstyles)


    I am trying to customise the Basic theme (by WP Puzzle) using a child theme to apply a hyperlink to the author name, for the post meta that appears under the post title on a single post page. The archive pages display a link to the author page but the single posts do not.

    I have found the relevant piece of code inside the theme files called html-blocks.php. It shows that the author name post meta will output a span rather than a link. The parent file snippet looks like this:

    case 'author':
    	$meta_html[ $meta ] = '<span class="author' . $preview . '">' . get_the_author() . '</span>';
    break;

    I have referred to this document to try to customise this line inside a duplicate of this file copied to the child theme and listed inside the same directory.

    This is the code I have tried:

    case 'author':
    	$meta_html[ $meta ] = '<span class="author' . $preview . '"><a href="' . get_the_author_meta('url', $author_id) . '">' . get_the_author() . '</a></span>';
    break;

    Currently:

      Any changes I make to the html-blocks.php file in the child theme does not have any effect.
      I applied the code to the parent theme file just to confirm I was working with the right file and was able to apply a link to the home page only, for some reason, the author url is not getting applied. Perhaps this has something to do with the staging extension I am using for the development site?

      Any suggestions welcome. Thank you in advance for your help!

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Child theme files only automatically override parent template files, not other PHP code. I don’t know this theme, but I’m reasonably sure html-blocks.php is not a template file. In the case of non-template parent theme code, you need to utilize filter and action hooks to modify behavior. Unless the function declared by the parent is “pluggable” (declaration starts with something like if ( ! function_exists( 'function_name' ) ) :), in which case you can simply re-declare the same function using your own code.

    If all else fails, declare your own function using a unique name and call it from which ever template is applicable. Or just alter the template directly to do as you wish. Directly outputting the author’s link on the single post template should be relatively straight forward.

    Thread Starter cascadingstyles

    (@cascadingstyles)

    Hi @bcworkz Thank you so much for your reply. It looks like the function is indeed pluggable, in which case, does that mean I can just copy paste it into functions.php of my child theme? I will try this in any case.

    Here is the whole function:

    if ( ! function_exists( 'basic_get_postmeta' ) ):
    	function basic_get_postmeta() {
    
    		$default_meta_list = get_theme_mod( 'postmeta_list', 
    						   apply_filters( 'basic_postmeta_list_defaults', array( 'date', 'category', 'comments' ) )
    						  );
    		$default_meta_list = ! is_array( $default_meta_list ) ? explode( '_', $default_meta_list ) : $default_meta_list;
    
    		$meta_list           = apply_filters( 'basic_post_meta_list', $default_meta_list );
    		$displayed_meta_list = $meta_list;
    
    		if ( is_customize_preview() ) {
    			$all       = array_merge( $meta_list, array( 'date', 'category', 'comments', 'tags', 'author' ) );
    			$meta_list = array_unique( $all );
    		}
    
    		if ( empty( $meta_list ) ) {
    			return;
    		}
    
    		$meta_html = array();
    
    		foreach ( $meta_list as $meta ) {
    			$preview = ( ! in_array( $meta, $displayed_meta_list ) ) ? ' hide' : '';
    			switch ( $meta ) {
    				case 'date':
    					$meta_html[ $meta ] = '<span class="date' . $preview . '">' . get_the_time( get_option( 'date_format' ) ) . '</span>';
    					break;
    				case 'author':
    					$meta_html[ $meta ] = '<span class="author' . $preview . '"><a href="' . get_the_author_meta('url', $author_id) . '">' . get_the_author() . '</a></span>';
    					break;
    				case 'category':
    					$meta_html[ $meta ] = '<span class="category' . $preview . '">' . get_the_category_list( ', ' ) . '</span>';
    					break;
    				case 'comments':
    					$meta_html[ $meta ] = '<span class="comments' . $preview . '"><a href="' . get_comments_link() . '">' . __( 'Comments', 'basic' ) . ': ' . get_comments_number() . '</a></span>';
    					break;
    				case 'tags':
    					$meta_html[ $meta ] = '<span class="tags' . $preview . '">' . get_the_tag_list( __( 'Tags: ', 'basic' ), ', ' ) . '</span>';
    					break;
    			}
    		}
    		$meta_html = apply_filters( 'basic_post_meta_list_html', $meta_html );
    
    		$html = '';
    		foreach ( $meta_list as $meta ) {
    			$html .= ( array_key_exists($meta,$meta_html) ) ? $meta_html[ $meta ] : '';
    		}
    
    		$html = apply_filters( 'basic_post_meta_html', $html );
    
    		echo '<aside class="meta">';
    		do_action( 'basic_post_meta_before_first' );
    
    		echo $html;
    
    		do_action( 'basic_post_meta_after_last' );
    		echo '</aside>';
    	}
    endif;
    add_action( 'basic_after_post_title', 'basic_get_postmeta', 10 );

    Thanks again.

    Thread Starter cascadingstyles

    (@cascadingstyles)

    Hi again @bcworkz I tried your advice and it worked, the author name is now hyperlinked. However it links to the home page. Any idea how I can get it to link to the author profile?

    Here is a sample post and the profile I would like it linked to (I am using profile ID as the url).

    Thank you in advance.

    Moderator bcworkz

    (@bcworkz)

    Try this:

    case 'author':
    	$meta_html[ $meta ] = '<span class="author' . $preview . '"><a href="' . get_the_author_meta('user_url', $author_id) . '">' . get_the_author() . '</a></span>';
    	break;

    (using different meta field key name) I think ‘url’ is to their personal site, or maybe I have it backwards, unverified. If that doesn’t work, determine the correct meta key and use get_user_meta().

    Thread Starter cascadingstyles

    (@cascadingstyles)

    Hi @bcworkz Thanks for your reply. While searching for the correct meta key, I found this post. I was able to fix the problem using this code:

    case 'author':
    	$meta_html[ $meta ] = '<span class="author' . $preview . '">' . get_the_author_posts_link() . '</span>';
    	break;

    Thanks again for your support!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get author meta function to retrieve author url’ is closed to new replies.