• merlin01

    (@merlin01)


    I know basically nothing about php and would love someone’s help regarding the following edit I’m trying to make on my WordPress website.

    Here is the current and working section of the code I’m trying to edit:

    elseif ( is_singular( 'post' ) ) {
    		$title = get_the_title();
    		$subtitle = get_the_date() . ' - ' . get_the_category_list( ', ' );

    Here is what I’d like to add to the code above:

    Published:
    Updated: <?php echo date('j F Y'); ?>

    Here is what I’ve tried without success:

    elseif ( is_singular( 'post' ) ) {
    		$title = get_the_title();
    		$subtitle = 'Published:' get_the_date() . ' - ' . get_the_category_list( ', ' )
    		'Updated: <?php echo date('j F Y'); ?>';

    Additionally, it would actually be amazing if someone could help me make the edits noted above to apply strictly to a certain post category (as the above will be applied to all ‘post'(s).

    For more information or parameters, below is the full section of code as it appears in my functions.php file:

    function rainy_page_header() {
    	$title = '';
    	$subtitle = '';
    
    	if ( is_front_page() && is_home() ) {
    		$title = get_theme_mod( 'rainy_default_blog_title', 'Blog' );
    		$subtitle = get_theme_mod( 'rainy_default_blog_subtitle', 'Blog subtitle' );
    	} elseif ( is_singular( 'post' ) ) {
    		$title = get_the_title();
    		$subtitle = get_the_date() . ' - ' . get_the_category_list( ', ' );
    	} elseif ( is_home() ) {
    		$blog = get_post( get_option( 'page_for_posts' ) );
    		$title = $blog->post_title;
    		$subtitle = get_post_meta( $blog->ID, 'rainy_subtitle', true );
    	} elseif ( is_tax() ) {
    		$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    		$title = $term->name;
    		$subtitle = category_description();
    	} elseif ( is_category() ) {
    		$title = single_cat_title( '', false );
    		$subtitle = category_description();
    	} elseif ( is_tag() ) {
    		$title = single_tag_title( '', false );
    		$subtitle = tag_description();
    	} elseif ( is_day() ) {
    		$title = get_the_date();
    		$subtitle = __( 'Daily archives', 'spartan01' );
    	} elseif ( is_month() ) {
    		$title = get_the_date( 'F Y' );
    		$subtitle = __( 'Monthly archives', 'spartan01' );
    	} elseif ( is_year() ) {
    		$title = get_the_date( 'Y' );
    		$subtitle = __( 'Yearly archives', 'spartan01' );
    	} elseif ( is_author() ) {
    		$title = get_the_author();
    		$subtitle = __( 'Author archives', 'spartan01' );
    	} elseif ( is_search() ) {
    		$title = __( 'Search results for: ', 'spartan01' ) . get_search_query();
    	} elseif ( is_404() ) {
    		$title = __( 'Error 404', 'spartan01' );
    		$subtitle = __( 'Page not found', 'spartan01' );
    	} else {
    		$title = get_the_title();
    		$subtitle = get_post_meta( get_the_ID(), 'rainy_subtitle', true );
    	}
    
    	echo '<div class="page-header">';
    		echo '<h1 class="page-title">' . $title . '</h1>';
    
    		if ( $subtitle ) {
    			echo '<div class="page-subtitle">' . $subtitle . '</div>';
    		}
    	echo '</div>';
    }

    Any help/support is greatly appreciated.

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

    (@bcworkz)

    You don’t use <?php ?> tags where you’ve used them because the entire code block is already PHP. To add text to the $subtitle string, it must be concatenated to the existing text, like so:
    $subtitle .= "<br>\nUpdated: " . date('j F Y');

    This will place “Updated: {current date}” on a line below the “Published: {date} – categories”

    To only apply this to certain categories, first we need to get the queried post with $post = get_queried_object(). Then determine the categories with `$cats = get_the_category( $post->ID );

    Loop through all the categories returned with foreach( $cats as $cat ), but before that, initialize a flag variable to false. In the loop, if `$cat->name == ‘my_cat_slug’ then set the flag variable to true.

    Finally, after the loop, if the flag variable is true then concatenate the updated text like previously discussed.

    Yeah, I could have just written the code instead, but then you wouldn’t learn much. Try coding this up yourself, the logic is laid out, you only need to determine the proper syntax, which is what is most important to learn.

    Thread Starter merlin01

    (@merlin01)

    Thank you very much for the response! I’ll try to decipher your instructions regarding applying to only ‘certain categories’.

    I do appreciate the push to actually learn!

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

The topic ‘How to Make Simple Edit to PHP/Function file in WordPress’ is closed to new replies.