[Resolved] Custom Intro for Each Category Archive for First Page Only

Home Forums Support [Resolved] Custom Intro for Each Category Archive for First Page Only

Home Forums Support Custom Intro for Each Category Archive for First Page Only

Viewing 15 posts - 1 through 15 (of 23 total)
  • Author
    Posts
  • #525894
    surfing

    I’m a new customer and am loving GP! I’m trying figuring out the best way to accomplish something with GP.

    I want an introduction on the post archive page for each category. Each category will have its own introduction. Therefore, cars category will have an intro about cars. Then my horses category will have an intro for horses. Furthermore, I only want this introduction to be displayed on the first page of the post category archive. This article on Yoast explains what I’m want to do. How can I do this using GeneratePress?

    Here’s an example on yoast.com of what I want to do.

    Take a look at this post category archive:
    Notice how on the first page there is an introduction, then on the second page the introduction goes away? This is what I want to do. What the best way to achieve this with GP?

    Thanks

    #525970
    Leo
    Staff
    Customer Support

    Hi there,

    I believe our page header add-on is what you are looking for:
    https://docs.generatepress.com/article/page-header-overview/
    https://docs.generatepress.com/article/page-header-global-locations/#post-types-%E2%80%93-archives

    Then I think we can use some CSS to hide it on second page.

    Let me know when you set things up.

    #526008
    surfing

    Leo thanks for the reply.

    I want to have unique content for each category archive page and I have 8 categories on my site. Therefore, I have created unique page headers for each category and then assigned these page headers to the corresponding category. Now this works good thus far but I see the page headers on the second page and beyond. Which is the part I’m not sure how to do with GP. Are you suggesting I use CSS display:none; to eliminate the header on category archive pages 2+? What’s the code for this and where should it go?

    Alternatively, For each category can I use the description field and then do a display:none; to remove the description for pages 2+?

    Any help will be appreciated.

    #526557
    Tom
    Lead Developer
    Lead Developer

    For the Page Header, you can do this:

    .paged .page-header-content {
        display: none;
    }

    For the standard category description, you can do this:

    .paged .page-header {
        display: none;
    }
    #527388
    surfing

    After adding the recommended CSS code I am noticing when I view page source, the category description is still within the page but not displayed. I don’t think a CSS based approach will work for what I trying to do. I need for the category description to not be present when viewing page source. I am trying to eliminate duplicate content from my site and using CSS will not fix the problem. Google will still index this content.

    How can something like the following be accomplished using the WordPress title and WordPress category description?
    1) WP Title and WP category description are displayed on the first page of a category archive.
    2) On subsequent pages (paginated archive pages) only the WP Title is displayed with the word “Archives” without the quotes added within the H1 tag. To see what I mean please look at this category page on Yoast then go to page two. These two pages on Yoast show what I describe above.

    I’m guessing a PHP based approach is required. After looking around on wordpress.org, I think I will need to make a modification to this code, but not sure how to do it within GP theme.

    	<?php if ( have_posts() ) : ?>
    		<header class="page-header">
    			<?php
    				the_archive_title( '<h1 class="page-title">', '</h1>' );
    				the_archive_description( '<div class="taxonomy-description">', '</div>' );
    			?>
    		</header><!-- .page-header -->

    If you have a better idea please let me know.

    Any help will be appreciated.

    #528170
    Tom
    Lead Developer
    Lead Developer

    Give this function a try:

    add_action( 'wp', 'tu_remove_paged_category_description' );
    function tu_remove_paged_category_description() {
        $page = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
        if ( 1 !== $page ) {
            remove_action( 'generate_archive_title', 'generate_archive_title' );
        }
    }
    #528212
    surfing

    Thanks for the reply. Where should I insert this function? Within Hooks or a particular PHP page?

    #528226
    Tom
    Lead Developer
    Lead Developer
    #528310
    surfing

    I used Code Snippets and inserted the code but get a white screen below the site header. Any idea what I should do next?

    #528316
    Tom
    Lead Developer
    Lead Developer

    That’s strange. I just adjusted the code above, can you try it instead?: https://generatepress.com/forums/topic/custom-intro-for-each-category-archive-for-first-page-only/#post-528170

    #528423
    surfing

    Thanks for your help Tom.

    It looks like I had conflicting code in a hook. Once I removed all the hooks and used the first code you sent it worked. The second try didn’t work. Below is the code which worked

    add_action( 'wp', 'tu_remove_paged_category_description' );
    function tu_remove_paged_category_description() {
        $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
        if ( 1 !== $page ) {
            remove_action( 'generate_archive_title', 'generate_archive_title' );
        }
    }

    I need one more small adjustment. How can the Category title be displayed on pages 2+ with the word Archives appended? Here is a summary of it all in one place:

    1) WP Title and WP category description are displayed on the first page of a category archive.
    2) On subsequent pages (paginated archive pages) only the WP Title is displayed with the word “Archives” without the quotes added within the H1 tag.

    For example if the category is Dogs and there is a Dogs description. Then the first page will show the title Dogs and the Dogs description. The subsequent pages will show Dogs Archives for the title without showing the description.

    #528809
    Tom
    Lead Developer
    Lead Developer

    You can try adjusting the function to this:

    add_action( 'wp', 'tu_remove_paged_category_description' );
    function tu_remove_paged_category_description() {
        $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
        if ( 1 !== $page ) {
            remove_action( 'generate_archive_title', 'generate_archive_title' );
            add_action( 'generate_archive_title', 'tu_custom_paged_archive_title' );
        }
    }
    
    function tu_custom_paged_archive_title() {
        ?>
        <header class="page-header">
            <h1 class="page-title">
                <?php the_archive_title(); ?> Archives
            </h1>
        </header>
        <?php
    }
    #528911
    surfing

    Thanks! Works great!

    #529326
    Tom
    Lead Developer
    Lead Developer

    You’re welcome 🙂

    #1618204
    David

    Hi Tom,

    What about if I am using an Elements Header on my Archive Category pages and showing the description.

    But then the code above does not prevent the description from not showing on pages 2, 3, 4, ….

    I can not use php in an Element Header, so any ideas?

Viewing 15 posts - 1 through 15 (of 23 total)
  • You must be logged in to reply to this topic.