• Resolved theatereleven

    (@theatereleven)


    I’m trying to implement tags on my pages, so pages show up in the tag clouds I have printing in various places on the site.

    The problem? If I tag a page, it doesn’t show up in the list. Only posts do. I’m using the following in my functions.php to enable page tags:

    // add tag support to pages
    function tags_support_all() {
    	register_taxonomy_for_object_type('post_tag', 'page');
    }
    
    // ensure all tags are included in queries
    function tags_support_query($wp_query) {
    	if ($wp_query->get('tag')) $wp_query->set('page_type', 'any');
    }
    
    // tag hooks
    add_action('init', 'tags_support_all');
    add_action('pre_get_posts', 'tags_support_query');

    My tag.php page has the following:

    <?php while (have_posts()) : the_post(); ?>
    
     <h2 class="category-title" id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>" style="color:#000;"><?php the_title(); ?></a></h2>
     <?php the_excerpt(); ?>
    
    <?php if ($wp_query->max_num_pages > 15) : ?>
      <nav class="post-nav">
    	<ul class="pager">
    	  <li class="previous"><?php next_posts_link(__('&larr; Older posts', 'roots')); ?></li>
    	  <li class="next"><?php previous_posts_link(__('Newer posts &rarr;', 'roots')); ?></li>
    	</ul>
      </nav>
    <?php endif; ?>
    
    <?php endwhile; ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter theatereleven

    (@theatereleven)

    This code in fuctions.php worked:

    // add tag support to pages
    function tags_support_all() {
    register_taxonomy_for_object_type('post_tag', 'page');
    }
    
    // ensure all tags are included in queries
    function tags_support_query($wp_query) {
    if ($wp_query->get('tag')) $wp_query->set('page_type', 'any');
    }
    
    // tag hooks
    add_action('init', 'tags_support_all');
    add_action('pre_get_posts', 'tags_support_query');
    
    // allows the tags to work in the normal category display
    add_filter('pre_get_posts', 'wpse_pre_get_posts');
    function wpse_pre_get_posts($q)
    {
        if( $q->is_main_query() && $q->is_tag() )
        {
            $q->set('post_type', array('post','page') );
        }
    }

    Thank you theatereleven, that’s exactly what I needed. I didn’t know about the pre_get_posts filter.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem with tagged pages not showing.’ is closed to new replies.