Joy
(@joyously)
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.
http://developer.ww.wp.xz.cn/reference
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?
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;