Archived topic
How to add code and text/html before and after the category title
10 replies · Started by Pete on December 31, 2014
Add this code in your child theme functions.php to add code and/or text/html before and after the category title without editing the archive.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;
}
// 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;
}
Very cool - thanks for the code, Pete! :)
Guys, 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?
This 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.
Or just use the hooks add on!
Thanks 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.
Are you wanting to add to the H1 (inside the title) - not after it?
Yes, 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.
Have 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/
