WordPress.org

Ready to get started?Download WordPress

Forums

Custom taxonomy redirect to post or page (4 posts)

  1. lastafa
    Member
    Posted 4 years ago #

    Hello everybody, this is my first post, I've been using wordpress for about a year, trying to penetrate it's possibillities for realizing my web visions.

    What I'm up to right now is leveraging custom taxonomies for people to navigate through different properties and connections between various superfoods and herbs.

    I have one taxonomy called synergi (synergy in norwegian) which displays other foods that go weel together with the one described in the tagged post.

    say a synergy for cacao is vanilla, then clicking vanilla takes you to /synergy/vanilla. I modified the template to display the description and list posts (foods) tagged with the vanilla synergy

    now for my conundrum. Many of the foods that synergize well I actually carry on the site. So for something that synergizes with cacao beans, The link should take you to the cacao post for easy access.

    I could have the template look to see if any posts matches the synergy value (any whiz have some code suggestions for that?). But that would make a lot of duplicate content.

    So maybe in the term items list, replacing the urls of synergies that are identical to posts with the post permalink? Any ideas?

    I have thought about having the synergy pages go in detail how to combine different things. I would still need this fix for some cases.

    Feel free to have me elaborate or rephrase if something is unclear about my questions. This new area of wordpress usage and development is immensely exciting for the current and future of information presentation and structure.

  2. MichaelH
    Member
    Posted 4 years ago #

  3. lastafa
    Member
    Posted 4 years ago #

    Thank you Michael, now I know how to seperate out the individual synergies. Any ideas on how to check for the first post matching the word and return the permalink of that post instead of the synergy page?

    So far I have modified your code to display another link when a post title matches the synergy name, this is real dirty, and does not return correct urls as they have different categories, I'm almost there, need to do a query for the guid and return that instead of the hack.

    <?php
    //if a given taxonomy matches a post name, return the url of that post
      $taxonomy = 'synergi';
      $args=array(
        'orderby' => 'count',
        'order' => 'DESC'
      );
      $terms = get_terms($taxonomy,$args);
      $counter = 0;
      foreach ($terms as $term) {
    
        $counter++;
        if ($counter <= 5) {
    
      global $wpdb;
    if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $taxonomy . "'", 'ARRAY_A')) {
    	  echo '<a href="http://nyta.webfactional.com/' . $term->name . '">' . $term->name . '</a>, ';
    	  } else {
          echo '<a href="' . esc_attr(get_term_link($term, $taxonomy)) . '">' . $term->name.'</a> ';
        }
      }
    }
    ?>
  4. MichaelH
    Member
    Posted 4 years ago #

    Since you are using the $taxonomy = 'synergi'; to see if that is in a post name, I don't see why you have to do get_terms.

    <?php
    //if a given taxonomy matches a post name, return the url of that post
    $taxonomy = 'synergi';
    $args=array(
      'name' => $taxonomy,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic