WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display only parent category in entry meta (11 posts)

  1. jamo_dodger
    Member
    Posted 5 months ago #

    Hello,

    I am trying to play around with the post entry meta to try and display just the parent cateogry of a post. I am using the following which I place in my child theme functions.

    if ( ! function_exists( 'twentytwelve_entry_meta' ) ) :
    /**
     * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
     *
     * Create your own twentytwelve_entry_meta() to override in a child theme.
     *
     * @since Twenty Twelve 1.0
     */
    function twentytwelve_entry_meta() {
    	// Translators: used between list items, there is a space after the comma.
    	$categories_list = get_the_category_list( __( ', ', 'twentytwelve' ) );
    
    	// Translators: used between list items, there is a space after the comma.
    	$tag_list = get_the_tag_list( '', __( ', ', 'twentytwelve' ) );
    
    	$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s">%4$s</time></a>',
    		esc_url( get_permalink() ),
    		esc_attr( get_the_time() ),
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() )
    	);
    
    	$author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
    		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    		esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
    		get_the_author()
    	);
    
    	// Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
    	if ( $tag_list ) {
    		$utility_text = __( '%1$s by %3$s<span class="by-author"> %4$s</span>.', 'twentytwelve' );
    	} elseif ( $categories_list ) {
    		$utility_text = __( 'Posted in %1$s on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
    	} else {
    		$utility_text = __( 'This entry was posted on %3$s<span class="by-author"> by %4$s</span>.', 'twentytwelve' );
    	}
    
    	printf(
    		$utility_text,
    		$categories_list,
    		$tag_list,
    		$date,
    		$author
    	);
    }
    endif;

    This displays:

    |Category by James on date and time|

    However every post is placed in a parent category and a child/number of its child categories:

    |Web Design WordPress CMS by James on date and time|

    How can I ONLY display the parent category a post is tagged in??

    Thank you

    James

  2. esmi
    Forum Moderator
    Posted 5 months ago #

    If this is a child theme, then you should not be using the if ( ! function_exists( 'twentytwelve_entry_meta' ) ) : [...] endif; conditional.

  3. jamo_dodger
    Member
    Posted 5 months ago #

    Ah okay Esmi,

    What would you suggest?

  4. jamo_dodger
    Member
    Posted 5 months ago #

    I know this is a mild annoyance in its simplicity Esmi, but I have replaced the following in my content.php file:

    <?php twentytwelve_entry_meta(); ?>

    with:

    <span class="meta-category"><?php the_category(', ') ?></span>
    <span class="meta-author">by <?php the_author(); ?></span>
    <span class="meta-date">on <?php the_time('jS F Y') ?></span>

    and that delivers the same output as the above.

    Is this the right way to do this and if so how can i only display just the parent category?

  5. esmi
    Forum Moderator
    Posted 5 months ago #

    You just need your own customised copy of:

    function twentytwelve_entry_meta() {
    [...]
    }

    in your child theme.

  6. jamo_dodger
    Member
    Posted 5 months ago #

    For anyone who is trying to achieve this I got it to work.

    I removed the:

    <?php twentytwelve_entry_meta(); ?>

    and replaced it with:

    <span class="meta-category"><?php
    $category = get_the_category();
    if($category[0]){
    echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } ?></span>
    <span class="meta-author">by <?php the_author_posts_link(); ?></span>
    <span class="meta-date">on <?php the_time('jS F Y') ?></span>

    This works great. Not sure if this is the right way to do it but I have searched for ages and all the solutions seem to use the if conditional Esmi said NOT to use

    I used this page in the codex:

    https://codex.wordpress.org/Function_Reference/get_the_category

  7. jamo_dodger
    Member
    Posted 5 months ago #

    Thank you Esmi I misunderstood, I have taken out the if conditional at the top

    if ( ! function_exists( 'twentytwelve_entry_meta' ) ) :
    /**
     * Prints HTML with meta information for current post: categories, tags, permalink, author, and date.
     *
     * Create your own twentytwelve_entry_meta() to override in a child theme.
     *
     * @since Twenty Twelve 1.0
     */

    and the endif at the bottom and it works.

    I am going to keep this thread open whilst I try and figure out to display only the parent category via this functions.php method.

  8. jamo_dodger
    Member
    Posted 5 months ago #

    Hi Esmi I have managed to display only the parent category using the method above and my child functions.php now looks like:

    function twentytwelve_entry_meta() {
    	// Translators: used between list items, there is a space after the comma.
    
    	$category = get_the_category(); if($category[0]){ echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; }
    	// Translators: used between list items, there is a space after the comma.
    	$tag_list = get_the_tag_list( '', __( ', ', 'twentytwelve' ) );
    
    	$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s at %2$s</time></a>',
    		esc_url( get_permalink() ),
    		esc_attr( get_the_time() ),
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() )
    	);
    
    	$author = sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s" rel="author">%3$s</a></span>',
    		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    		esc_attr( sprintf( __( 'View all posts by %s', 'twentytwelve' ), get_the_author() ) ),
    		get_the_author()
    	);
    
    	// Translators: 1 is category, 2 is tag, 3 is the date and 4 is the author's name.
    	if ( $tag_list ) {
    		$utility_text = __( '%1$s •<span class="by-author"> by %4$s</span>%3$s', 'twentytwelve' );
    	} elseif ( $categories_list ) {
    		$utility_text = __( '%1$s •<span class="by-author"> by %4$s</span>%3$s', 'twentytwelve' );
    	} else {
    		$utility_text = __( '%1$s •<span class="by-author"> by %4$s</span>%3$s', 'twentytwelve' );
    	}
    
    	printf(
    		$utility_text,
    		$categories_list,
    		$tag_list,
    		$date,
    		$author
    	);
    }

    Everything works fine but I cannot figure out how to display the date using the format M - A short textual representation of a month.

    So my entry meta looks like:

    Uncategorized • by James January 29, 2014 at 12:02 am

    And I need it to look like:

    Uncategorized • by James Jan 29, 2014 at 12:02 am

    Any ideas?? or should I just revert back to replacing:

    <?php twentytwelve_entry_meta(); ?>

    with:

    <span class="meta-category"><?php
    $category = get_the_category();
    if($category[0]){
    echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } ?></span>
    <span class="meta-author">by <?php the_author_posts_link(); ?></span>
    <span class="meta-date">on <?php the_time('jS F Y') ?></span>

    in my content.php??

  9. jamo_dodger
    Member
    Posted 5 months ago #

    *Last block of code should be:

    <span class="meta-category"><?php
    $category = get_the_category();
    if($category[0]){
    echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>'; } ?></span>
    <span class="meta-author">• <?php the_author_posts_link(); ?></span>
    <span class="meta-date"><?php the_time('M d h:i a') ?></span>
  10. jamo_dodger
    Member
    Posted 5 months ago #

    After a bit of sleep found the solution. I inserted the format strings from the content.php snippet above into the functions.php block below:

    $date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s at %2$s</time></a>',
    		esc_url( get_permalink() ),
    		esc_attr( get_the_time('h:i a') ),
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date('M d') )
    	);

    Just goes to show if you have a problem and cannot figure it out (I found this horrifically difficult), keep trying, the solution is never far away!

  11. jamo_dodger
    Member
    Posted 5 months ago #

    resolved

Reply

You must log in to post.

About this Topic