Support » Developing with WordPress » Use excerpt as tag description

  • Galaxy_High

    (@galaxy_high)



    Hello all,

    I’m trying to populate tag descriptions from post excerpt that share the same slug/permalink.

    So far I have this:

    $the_slug = 'name-of-slug';
    $args = array('name' => $the_slug);
    $my_posts = get_posts($args);

    Is there anyway to implement this in the first line?

    $the_slug = $tag->slug;

    Thanx for looking,
    Sam

Viewing 4 replies - 1 through 4 (of 4 total)
  • Joy

    (@joyously)

    I can’t tell if you want to find the tags or find the posts with a tag or both.
    What is the known?

    Here’s how to find tags: https://developer.wordpress.org/reference/classes/wp_term_query/
    You already know about get_posts.
    It doesn’t make sense to me to try to use a post excerpt for a tag description. Which post would you use? All (could be hundreds)?

    Galaxy_High

    (@galaxy_high)

    Thanx for getting back to me.

    I want my tag descriptions to automatically pull in the excerpt from posts with the same name.
    i.e.
    I have a post called UK, with a populated excerpt – permalink: mysite.com/uk
    I have a tag called UK but I’d like to have the description use the post excerpt

    $tag->slug populates all the relevant variables when var_dumped but wont produce the required result.

    $the_slug = 'the_slug_or_permalink'; //this works as standalone
    $the_slug = $tag->slug;               //this doesn't work but shows all the correct variables when dumped
    $args = array('name' => $the_slug);
    $my_posts = get_posts($args);
     $html .= '<b>ID: </b>' . $my_posts[0]->ID . "\n\n";
     $html .= '<b>Name: </b>' . $my_posts[0]->post_name . "\n\n";
     $html .= '<b>Excerpt: </b>' . $my_posts[0]->post_excerpt . "\n\n";
     $html .= '<b>Slug: </b>' . $tag->slug ."\n\n";

    I’ll try and neaten up my function and paste it here.

    Thanx again.

    • This reply was modified 4 months ago by  Galaxy_High.
    Galaxy_High

    (@galaxy_high)

    I haven’t coded for a few years and it’s taking longer than anticipated to get back on the ball.

    Here’s my function:

    
    function tags(){ 
     if (is_page('tag')){$tags = get_tags();}
     else{$tags = get_the_tags();}
     
     if (!$tags){echo "";}
     else{
      $html = '<ul class="tags">' . "\r\n\t" . '<li>Tags: </li>' . "\r\n\t" . '';
      foreach ($tags as $tag){
       $tag_link = get_tag_link($tag->term_id);
    
       global $post;
       
       //get tag_group_ids
       $tag_group_ids = get_option('tag_group_ids', array());
        $i = array_search($tag->term_group, $tag_group_ids);
       
       //get tag_group_labels and change to lowercase
       $tag_group_labels = get_option('tag_group_labels', array());
        $lower = array_map('strtolower', $tag_group_labels);
       
       //get tag description and change ascii
       $description = str_replace('"', "''", $tag->description);
        $tagdescription = str_replace("=", "\n", $description);
       
       //get post excerpt relating to tag slug - CURRENTLY TESTING
       $tagslug = $tag->slug;
        $args = array('name' => $tagslug);
         $excerpt = get_posts($args);
          $output = $excerpt[0]->post_excerpt;
          
       echo '<pre>';
       var_dump ($excerpt);
       echo '</pre>';
       
       $outputreplace = str_replace('"', "''", $output);
        $postexcerpt = str_replace("=", "\n", $outputreplace);
            
       //change speechmarks for $tag->name
       $tagname = str_replace('"', "''", $tag->name); 
    
       //CURRENTLY TESTING
       $the_slug = 'uk';
       $the_slug = $tag->slug;
       $argz = array('name' => $the_slug);
       $my_posts = get_posts($argz);
        $html .= '<b>ID: </b>' . $my_posts[0]->ID . "\n\n";
        $html .= '<b>Name: </b>' . $my_posts[0]->post_name . "\n\n";
        $html .= '<b>Excerpt: </b>' . $my_posts[0]->post_excerpt . "\n\n";
        $html .= '<b>Slug: </b>' . $tag->slug ."\n\r\n\r";
    
       echo '<pre>';
       print_r($tag->slug);
       echo '</pre>';
      
       //output
       $html .= '<li class="' . $lower[$i] . ' ' . $tag->slug . ' tag">' . "\n\t";
       $html .= '<a href="' . $tag_link . '"';
       $html .= ' title="' . $tagname . "\n\n";
       if (!empty($postexcerpt)){
        $html .= $postexcerpt . "\n\n";
        }
       elseif (!empty($tagdescription)){
        $html .= $tagdescription . "\n\n";
        }
       else{
        $html .= "No tag description or post excerpt yet." . "\n\n";
        }
       $html .= 'Tagged in ' . $tag->count . ' posts' . "\n\n";
       $html .= 'Catergory: ' . $tag_group_labels[$i] . "\n" . '">';
       
       $html .= $tag->name;
       $html .= ' <sup>[' . $tag->count . ']';
       if (!empty($outputreplace)||($description)){
        $html .= '[D]';
        }
       $html .= '</sup></a></li> '. "\n\n\t";
       }
      $html .= '</ul>';
      echo $html;
      echo "\r\n";
      }
     }
    tugbucket

    (@tugbucket)

    function tags(){ 
    	if (is_page('tag')){
    		$tags = get_tags(array('hide_empty'=>false));
    	} else {
    		$tags = get_the_tags();
    	}
    	if($tags){
    		foreach ($tags as $tag){
    			echo '<p>Tag Name: '.$tag->name.'</p>';
    			$page = get_page_by_path($tag->slug, '', 'post');
    			if($page){
    				echo '<p>'.get_the_excerpt($page->ID).'</p>';
    			}
    		}
    	}
    }

    something like that?

    But honestly, I’m not really sure the point of doing this versus just using tag descriptions.

    Same thing? https://wordpress.org/support/topic/using-excerpt-as-tag-description/

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.