[Resolved] Add categories to Pages

Home Forums Support [Resolved] Add categories to Pages

Home Forums Support Add categories to Pages

  • This topic has 18 replies, 4 voices, and was last updated 7 months ago by David.
Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #770914
    Isaac

    Hi,

    I’m trying to use as few plugins as possible on my site.

    Is there any way I can add categories to pages using GeneratePress out of the box? (I’ve searched docs)

    I know there is a bazzilion plugins that will do this, but I’m trying to keep Plugin use to a bare minimum, so that my site remains lightweight.

    thanks

    #771270
    Leo
    Staff
    Customer Support

    Hi there,

    David provided some valuable suggestion here:
    https://generatepress.com/forums/topic/adding-categories-and-tags-taxonomy-to-pages/

    You can find lots of information on CPT in Google

    If you still prefer to use categories, then plugin is the way to go 🙂

    #771943
    André

    Isaac, this has worked for me:

    Using the Code Snippets plugin (which I already use), I added this code:

    function add_taxonomies_to_pages() {
     register_taxonomy_for_object_type( 'post_tag', 'page' );
     register_taxonomy_for_object_type( 'category', 'page' );
     }
    add_action( 'init', 'add_taxonomies_to_pages' );
     if ( ! is_admin() ) {
     add_action( 'pre_get_posts', 'category_and_tag_archives' );
     
     }
    function category_and_tag_archives( $wp_query ) {
    $my_post_array = array('post','page');
     
     if ( $wp_query->get( 'category_name' ) || $wp_query->get( 'cat' ) )
     $wp_query->set( 'post_type', $my_post_array );
     
     if ( $wp_query->get( 'tag' ) )
     $wp_query->set( 'post_type', $my_post_array );
    }

    Once added to Code Snippets, enable. That’s it. Categories will show for pages, no different to posts.

    I originally tried to add the code using GP > Elements, but couldn’t figure it out. But Code Snippets works great.

    #772152
    Leo
    Staff
    Customer Support

    Thanks Andre!

    #772741
    Isaac

    Thanks Andre.

    In the end I just went with a plugin.

    The plugin was really small, like 2.1KB so wouldn’t have bloated out my
    site and it was simple.

    Cheers

    #772749
    André

    Isaac, which plugin did you go with?

    #772805
    Isaac
    #1307503
    Whelan

    Hello

    I’m also looking for a lightweight way to use post categories for pages too. I’ve added the relevant php to code snippets as mentioned above.

    It’s working but the secondary navigation does not recognise the active category on a page. What am I missing?

    #1307937
    Tom
    Lead Developer
    Lead Developer

    Does the primary navigation recognize it? If not, I assume WordPress is doing something else internally to make that connection. Have you tried the plugin mentioned above?

    #1309152
    Whelan

    I’m using the same code as the mentioned plugin, in the same way that the plugin developer describes here:
    https://thewphosting.com/add-categories-tags-pages-wordpress/

    The primary navigation does recognise it, but I need it to work in the secondary nav. Is this possible?

    #1309690
    David
    Staff
    Customer Support

    Hi there,

    can you share a link to your site ? You may want to start a new topic where you can share the link privately using the Site URL field provided.

    #1311634
    Whelan

    OK thanks, I’ll start a new topic

    #1834325
    Marc

    Looking for same kind of feature: using categories for pages, just like they can be used for blog posts and Woo products.

    But more important: the categories need to be used in the permalink structure too – just as with posts and products.

    For example, if the categories are:

    = wall art
    == living room
    === canvas print (in this example the checked category)
    === fine art print
    === etc

    Then the slug need to become: /wall-art/living-room/canvas-print/page-title

    Does anyone know if a plugin like Add categories to pages makes that possible?

    #1834475
    David
    Staff
    Customer Support

    Hi there,

    to the best of my knowledge you cannot create the category slug permalink structure for Pages. But you can use Sub Pages to do the same thing:

    https://wordpress.org/support/article/pages/#organizing-your-pages

    #1834490
    Marc

    Thnx for the reply! 🙂

    Yes, I now about the parent child structure, creating a permalink structure/hierarchy.

    Wondering why WP created such a thing for posts, but not for pages…

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