WordPress.org

Ready to get started?Download WordPress

Forums

Tags for pages (7 posts)

  1. kbeatt
    Member
    Posted 4 years ago #

    I am testing out WP 3.0 for my new site and I am running into an issue. On the production site(WPMU 2.8.6a), I am using a plugin that allows me to apply tags to a page. I then use a query that finds all posts with the same tag as the page and lists them out on the page. You can see an example at the bottom of this page. In testing the 3.0 beta, the plugin I used before does not work in 3.0. Is that because I am supposed to be using a custom post type to create the editing page to allow tags or is there a "better" way I should be drawing the list of tagged posts for a particular page?

    Thanks

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    What i'd suggest, since you're using 3.0, is to register a new taxonomy called something similar to tags, but register to both pages and posts, so you could add tags to both the page and the posts..

    You'd then have the page template fetch the page tags, then grab all posts that have those tags.

    I'll give you an example, just gimme 10 mins or so to test some code..

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    Actually i've found a better way, just add post tag support to pages... :)

    Bear with me a few more minutes and i'll have an example.. :)

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    Firstly, register the post tag taxonomy for pages like so..

    add_action( 'init', 'register_tags_for_pages', 0 );
    function register_tags_for_pages() {
    	register_taxonomy_for_object_type( 'post_tag', 'page' );
    }

    Then update your page template to something along these lines...

    <?php
    /*
    Template Name: Show posts with page's tags
    */
    get_header();
    
    // Intercept the $post var now and get tags
    $page_tags = wp_get_post_terms( $post->ID , 'post_tag', array( 'fields' => 'ids' ) );
    
    // If $page_tags is not empty, we have tags
    if( !empty( $page_tags ) )
    	// Now we have tags, we re-define the main query to look for posts
    	query_posts(
    		// Array of parameters for query
    		array(
    			// Sets up paging
    			'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
    			// Gets only posts that have one of the tags found in the page
    			'tag__in' => $page_tags,
    			// Make sure we fetch posts
    			'post_type' => 'post',
    			// And that they're published
    			'post_status' => 'publish',
    			// Set the posts per page (you can remove or adjust this as appropriate
    			'posts_per_page' => 10
    		)
    	);
    
    // Most basic of loop examples, adjust this as necessary
    if( have_posts() ) :
    	while( have_posts() ) : the_post();
    
    		the_title();
    		echo '<br />';
    
    	endwhile;
    endif;
    
    get_footer();

    Works here, so let me know if you have any problems... ;)

  5. kbeatt
    Member
    Posted 4 years ago #

    Thanks! This worked. It's funny that the plugin I was using to do this was just updated to work in 3.0. Now I don't know if your way or the old way is better. I guess it's the same thing. The one difference is in my code. I just have to detect if the tagged page is a subpage of the parent and then there is no need to remember to select the proper page template.

    <?php if ($post->post_parent=="24")?>
    	<?php  //for use in the loop, list 60 post titles related to first tag on current post
      $backup = $post;  // backup the current object
      $tags = wp_get_post_tags($post->ID);
      $tagIDs = array();
      if ($tags) {
        $tagcount = count($tags);
        for ($i = 0; $i < $tagcount; $i++) {
          $tagIDs[$i] = $tags[$i]->term_id;
        }
        $args=array(
          'tag__in' => $tagIDs,
          'post__not_in' => array($post->ID),
          'showposts'=>60,
          'caller_get_posts'=>1
        );?>
    	<h1 class="entry-title"> Player posts </h1>
        <?php
    	$my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    
            <li><?php the_time('m-d-Y'); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?> </a></li>
          <?php endwhile;
    
        } else { ?>
          <li>No related posts found.</li>
        <?php }
      }
      $post = $backup;  // copy it back
      wp_reset_query(); // to use the original query again
    
    ?>

    Any opinions?
    Thanks for the help.

  6. Mark / t31os
    Moderator
    Posted 4 years ago #

    Use whatever suits you best.. :)

    NOTE: showposts is deprecated in favour of posts_per_page (anything you pass to showposts will get moved to posts_per_page anyway).

    The code i supplied is a little more refined and faster then what you have, but unless you're running an extremely busy site i very much doubt you'll notice any difference.

    NOTE: What i've supplied pretty much does what you have above.

    The one difference is in my code. I just have to detect if the tagged page is a subpage of the parent and then there is no need to remember to select the proper page template.

    Not sure i quite follow what you mean. Do subpages inherit the parent's page template?

  7. kbeatt
    Member
    Posted 4 years ago #

    Thanks for the note about showposts. I'll be updating that code :)

    I only have 2 templates. One for showing page comments and the default page template. I have one parent page for the roster(post_parent=24) and all player's pages are sub pages of the roster. And since all the new pages use the default template, I don't have to change anything.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags