Site logo

Archived topic

custom page titles for categories

7 replies · Started by Susanne on May 1, 2016

Viewing posts 1–8 of 8

Hi there, I looked everywhere for this info and could not find it:
I have created three custom post types for my portfolio. One of them has several categories. I want to display the category name as the title on my category archive page
Right now it just says "Archive". I know there is a simple way to do this.
I see that there is a function that says <?php do_action( 'generate_archive_title' ); ?> in the archive template. So I think I have to add a filter for that function?
Can you help? much appreciated

Hmm, this is a tough one..

You would have to do something like this I think:

add_action( 'after_setup_theme','generate_taxonomy_title' );
function generate_taxonomy_title()
{
    if ( is_tax( 'tax-slug' ) ) {
        remove_action( 'generate_archive_title','generate_archive_title' );
        add_action( 'generate_archive_title','generate_custom_taxonomy_archive_title' );
    }
}

function generate_custom_taxonomy_archive_title()
{
?>
    <header class="page-header<?php if ( is_author() ) echo ' clearfix';?>">
        <?php do_action( 'generate_before_archive_title' ); ?>
        <h1 class="page-title">
            Your title
        </h1>
        <?php do_action( 'generate_after_archive_description' ); ?>
    </header>
<?php
}

Something along those lines..

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

Thanks for your quick reply Tom. I am now thinking it would be better to make a custom page template that uses a filterable portfolio. That way I would not have to bother with archive titles at all. Will try to get some more info on that. I found a great link here:
how-to-build-isotope-portfolio-in-your-wordpress-theme/
Will try to make that happen and report back

Cool :)

Hi Tom,

I am a novice and not that familiar with PHP. I tried for some hours to bring your code to work. I believe that it has an error. I printed out some information and got the feeling that the hook is at the wrong place. When it is invoked the post seems to be not yet activated. I printed out the post-type and the tax an both where empty. By the way - there seems to be a coding error at the end of the snippet.

Do you have an idea how the code could work correct and which hook should be used.

Hi Hans,

I just adjusted the code - sorry about that.

Let me know if it works now :)

Hi Tom,

unfortenately I did not succeed with your code. Either I got a syntax error or nothing happend using different hooks. I don't know what I am making wrong. I use now a modification in template-tags.php at the end of the list of if ... elseif which is working fine for me.

elseif ( is_tax() ) :
$tax = get_taxonomy( get_queried_object()->taxonomy );
/* translators: 1: Taxonomy singular name, 2: Current taxonomy term */
printf( __( '%1$s: %2$s', 'genratepress' ), $tax->labels->singular_name, single_term_title( '', false ) ) ;					
 

If that's working, copy the entire function you're editing in template-tags.php (including the function_exists() wrapper) and add it to your functions.php in your child theme (or another method: https://generatepress.com/knowledgebase/adding-php-functions/).

That will make it so you won't lose your changes when you update :)

This archived topic is closed to new replies.