• How do I pass the contents of $the_slug array to the $post_name variable in the loop?

    $tags = get_the_tags();
    foreach ($tags as $tag){
    	
     global $post;
     $the_slug = $tag->slug; //contains 10ish words that associate with my permalinks: welcome, home, about, contact, etc
    	
     $post_id = 'welcome';
     $post_name = $the_slug; //fails to populate here
     $queried_post = get_post($post_name); //if changed to $post_id works but only 'welcome' post
     $excerpt = $queried_post->post_excerpt;
     $excerpt = apply_filters('the_content', $excerpt);
     $excerpt = str_replace(']]>', ']]>', $excerpt);
     echo $excerpt . "\n\n";
     }

    Thanx for looking,
    Sam

    • This topic was modified 6 years, 3 months ago by Galaxy_High. Reason: Formatting
Viewing 5 replies - 1 through 5 (of 5 total)
  • This looks very similar to your other question.
    You need to look up the functions you are using in the reference, so that you get the parameters correct.
    https://developer.www.ads-software.com/reference

    Thread Starter Galaxy_High

    (@galaxy_high)

    Yep, I’ve got so far and don’t understand why I can’t pass an array containing strings to a variable using a loop. That’s why I’m asking for help.
    You might as well said, “The answers are here: google.com

    To answer (I think) your need:

    ” don’t understand why I can’t pass an array containing strings to a variable using a loop”

    global $post;
    $tags = get_the_tags();
    if($tags){
    	$post_name = '';
    	foreach ($tags as $tag){
    		$the_slug = $tag->slug;
    		$post_name .= $the_slug; 
    	}
    }
    echo $post_name;

    but looking at your code, I can’t quite figure out what you are trying to accomplish. What exactly is your end result wanted to be?

    Moderator bcworkz

    (@bcworkz)

    I think your problem is the use of get_post(). You cannot use this function to get posts by title or slug. Only an ID or a complete post object will work. To get a post by title, you can use get_page_by_title(). This is obviously meant for pages, but you can pass different post types to get regular posts or custom posts types. This function generates a SQL query to get posts by post_title, not slug (post_name). Thus in your case, you should get the tag name to match against post titles, not the tag slug. This is assuming tag names and post titles do indeed correlate, since matching slugs does not mean matching titles.

     $the_name = $tag->name;
    	
     $post_title = $the_name;
     $queried_post = get_page_by_title( $post_title, OBJECT, 'post'); //gets _post_ by title

    Going back to tugbucket’s statement in your other topic, this seems very convoluted since tags have their own description field you can use to output what is currently in post excerpts. This would be way more expedient and efficient, requiring many fewer SQL queries be made. If the volume of excerpts that would need to be copied to tag descriptions is what is holding you back, a one time script to automate the process wouldn’t be too difficult to develop. It wouldn’t be a lot different than the code you have now. You get all tags instead of post tags, and then get the related post excerpt like I did above; and finally, set the tag description instead of outputting the excerpt.

    I think this code will help you
    global $get;
    $arr= get_the_tags();
    if($arr) {
    $get_name= ”;
    foreach($arr as $ar){
    $the_slug = $ar->slug;
    $get_name .= $the_slug;
    }
    }
    echo $get_name;

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Passing array data to a variable’ is closed to new replies.