- This topic has 10 replies, 3 voices, and was last updated 8 years, 1 month ago by Tom.
-
AuthorPosts
-
December 31, 2014 at 9:02 pm #60351Pete
Add this code in your child theme
functions.php
to add code and/or text/html before and after the category title without editing thearchive.php
. Look at the code below to edit as you wish.// Add Shortcode for php code before category title function code_before_cat_title_custom_shortcode() { // start php code echo '</h1>'; echo '<p>Any php can be inserted code before the category title here<p>'; } // end php code add_shortcode( 'code-before-cat-title', 'code_before_cat_title_custom_shortcode' ); // Insert the php code AND the html before the category title add_filter( 'single_cat_title', 'code_before_single_cat_title' ); function code_before_single_cat_title( $text2 ) { $append2 = ''; // Use the Shortcode Exec PHP plugin to convert your code into a shortcode and insert it here $append2 .= do_shortcode('[code-before-cat-title]' ); $text2 = '</h1>' . $append2 . '<p>Some text/html can be inserted after the top code here</p><h1 class="page-title">' . $text2 . '</h1><p>Some text/html can be inserted after the category title here</p>'; return $text2; } // Add Shortcode for php code after category title function code_after_cat_title_custom_shortcode() { // start php code echo '<p>Any php can be inserted code after the category title here<p>'; } // end php code add_shortcode( 'code-after-cat-title', 'code_after_cat_title_custom_shortcode' ); // Insert the php code AND the html after the category title add_filter( 'single_cat_title', 'before_after_code_single_cat_title' ); function before_after_code_single_cat_title( $text ) { $append = ''; // Use the Shortcode Exec PHP plugin to convert your code into a shortcode and insert it here $append .= do_shortcode('[code-after-cat-title]' ); $text = $text . '</h1><p>Some text/html can be inserted before the bottom code here</p>' . $append . ' <br /><p>Some text/html can be inserted after the bottom code here</p>'; return $text; }
December 31, 2014 at 9:51 pm #60357Pete// Add Shortcode for php code before category title function code_before_cat_title_custom_shortcode() { // start php code return // Show an optional term description. $term_description = term_description(); if ( ! empty( $term_description ) ) : printf( '<div class="taxonomy-description">%s</div>', $term_description ); endif; if ( get_the_author_meta('description') && is_author() ) : // If a user has filled out their decscription show a bio on their entries echo '<div class="author-info">' . get_the_author_meta('description') . '</div>'; endif; } // end php code add_shortcode( 'code-before-cat-title', 'code_before_cat_title_custom_shortcode' ); // Insert the php code AND the html before the category title add_filter( 'single_cat_title', 'code_before_single_cat_title' ); function code_before_single_cat_title( $text2 ) { $append2 = ''; $append2 .= do_shortcode('[code-before-cat-title]' ); $text2 = '</h1>' . $append2 . '<p>Some text/html can be inserted after the top code here</p><h1 class="page-title">' . $text2 . '</h1><p>Some text/html can be inserted after the category title here</p>'; return $text2; } /***** next one *****/ // Add Shortcode for php code after category title function code_after_cat_title_custom_shortcode() { // start php code return // Show an optional term description. $term_description = term_description(); if ( ! empty( $term_description ) ) : printf( '<div class="taxonomy-description">%s</div>', $term_description ); endif; if ( get_the_author_meta('description') && is_author() ) : // If a user has filled out their decscription show a bio on their entries echo '<div class="author-info">' . get_the_author_meta('description') . '</div>'; endif; } // end php code add_shortcode( 'code-after-cat-title', 'code_after_cat_title_custom_shortcode' ); // Insert the php code AND the html after the category title add_filter( 'single_cat_title', 'before_after_code_single_cat_title' ); function before_after_code_single_cat_title( $text ) { $append = ''; $append .= do_shortcode('[code-after-cat-title]' ); $text = $text . '</h1><p>Some text/html can be inserted before the bottom code here</p>' . $append . '<p>Some text/html can be inserted after the bottom code here</p>'; return $text; }
December 31, 2014 at 10:57 pm #60363January 1, 2015 at 10:45 am #60540TomLead DeveloperLead DeveloperVery cool – thanks for the code, Pete! 🙂
February 24, 2016 at 7:20 pm #175031MikeGuys, I’m really interested in what’s happening in this thread. If I understand correctly this code inserted in my functions.php file will allow me to add text to the end of the heading shown on tag archives? Is that a scenario this code would help with?
It looks like there might be some placeholder code in here that I don’t need, like shortcodes and such. I just need a way to append a few words after within the H1 title of a tag archive page without editing the page template.
Thoughts?
February 24, 2016 at 7:26 pm #175032PeteThis is probably not the best way to go for what you need. Maybe google “suffix append title WordPress”. I have a little snippet of code I’ll try to track down that does what you need.
February 24, 2016 at 7:27 pm #175033PeteOr just use the hooks add on!
February 24, 2016 at 7:50 pm #175040MikeThanks so much for the quick response. How would I use the Hooks add on for this? seems like it only does above and below the title/headline/h1. Id like to inject a standard suffix inside the H1 of tag archive pages.
- This reply was modified 8 years, 1 month ago by Mike.
February 25, 2016 at 12:10 am #175060TomLead DeveloperLead DeveloperAre you wanting to add to the H1 (inside the title) – not after it?
February 25, 2016 at 6:09 am #175142MikeYes, append text within the H1 tag. I run a small music blog and I tag the posts with genres. On the tag archive page I want the H1, instead of just saying “Rock” — I want it to add “Rock Song Archive” but only on the tag archive.
Thanks! Btw, I am your newsest, biggest fan, this framework is so good it’s surreal.
February 25, 2016 at 11:55 am #175195TomLead DeveloperLead DeveloperHave you tried something like this?:
add_filter( 'single_cat_title', 'generate_custom_archive_title' ); function generate_custom_archive_title( $text ) { if ( is_archive() ) return $text . ' Song Archive'; return $text; }
Adding PHP: https://generatepress.com/knowledgebase/adding-php-functions/
-
AuthorPosts
- You must be logged in to reply to this topic.