Site logo

[Resolved] Displaying Posted (and updated) Date on pages

Home Forums Support [Resolved] Displaying Posted (and updated) Date on pages

Home Forums Support Displaying Posted (and updated) Date on pages

Viewing 11 posts - 16 through 26 (of 26 total)
  • Author
    Posts
  • #769502
    Leo
    Staff
    Customer Support

    #2/#3 There are two ways to use hooks, one is to use Hook Element and the other is to use functions:
    https://docs.generatepress.com/article/using-hooks/

    It’s generally easier to use hook element if you aren’t comfortable with coding as you can set it to show on certain pages without using PHP conditional tags.

    The difference in performance should be minimal. Code is code no matter how you add them ๐Ÿ™‚

    #769576
    culpable

    Thanks Leo, that helps a lot. On your point about the hook element’s ability to show on certain pages without code โ€“ does that mean that if I use the display rule “All Pages”, then I can omit the “php if” part of the below:

    <?php if ( 'page' == get_post_type() ) : ?>
    	<div class="entry-meta">
    		<?php generate_posted_on(); ?>
    	</div>
    <?php endif; ?>

    Also โ€“ in what location should I place this hook (before_main_content)?

    And lastly โ€“ I presume that I tick the “Execute PHP” button? ๐Ÿ™‚

    #770028
    Leo
    Staff
    Customer Support

    This is all you need if you use the Hook Element:

    <div class="entry-meta">
        <?php generate_posted_on(); ?>
    </div>

    That depends on where you want it to show:
    https://docs.generatepress.com/article/hooks-visual-guide/

    And yes check execute PHP ๐Ÿ™‚

    #771531
    culpable

    Yes! It’s working now ๐Ÿ™‚

    Is there a way to reverse the order in which it shows the dates (i.e. display “Published ____ & Last updated ____”)?

    It’s currently showing: https://imgur.com/a/Voskp7n

    And I’m using the following CSS code along with your hook above:

    
    .posted-on .updated {
        display: inline-block;
    }
    .posted-on .updated:before {
        content: "Last updated ";
    }
    .posted-on .updated:after{
        content: " &"
    }
    .posted-on .published:before {
        content: " Published ";
    }
    

    Thank you for your time Leo ๐Ÿ™‚

    #771637
    Leo
    Staff
    Customer Support

    Any chance you can link me to the page in question?

    #771839
    culpable

    Sure thing Leo โ€“ image and link here

    #772460
    Leo
    Staff
    Customer Support
    #772790
    culpable

    I tried adding the code in the same hook as your previous suggestion:

    <div class="entry-meta"><?php generate_posted_on(); ?></div>
    <?php
    add_filter( 'generate_post_date_output', function( $output ) {
    	$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">%2$s</time>';
    	
    	if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    		$time_string = $time_string . '<time class="updated" datetime="%3$s" itemprop="dateModified">%4$s</time>';
    	}
    	
    	$time_string = sprintf( $time_string,
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_attr( get_the_modified_date( 'c' ) ),
    		esc_html( get_the_modified_date() )
    	);
    
    	echo sprintf( '<span class="posted-on">%1$s</span>', // WPCS: XSS ok, sanitization ok.
    		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
    			esc_url( get_permalink() ),
    			esc_attr( get_the_time() ),
    			$time_string
    		)
    	);
    } );
    ?>

    But it doesn’t seem to be working. Did I enter it in wrong? (highly likely, sorry Leo)

    Also Leo as per this link, I just noticed my author pages aren’t showing anything.

    I’m guessing this is because I have no posts, only pages. Is there code I can add to fix this? If so, can you show me how I have to wrap PHP code such that it executes within the Elements hook โ€“ would this require a display rule on Author archive pages?

    Otherwise, if it’s easier I can add the code to display the authored pages as a PHP snippet.

    #773208
    Leo
    Staff
    Customer Support

    Tom’s code needs to be added separately as a PHP function.

    Adding PHP: https://docs.generatepress.com/article/adding-php/

    #773431
    culpable

    Working!!!

    Since the author question is separate, I’ll make a new post for that.

    Thank you very much for your time Leo.

    #773456
    Leo
    Staff
    Customer Support

    No problem ๐Ÿ™‚

Viewing 11 posts - 16 through 26 (of 26 total)
  • You must be logged in to reply to this topic.