profmustamar Posted October 19, 2012 Share Posted October 19, 2012 Hello, I am new here. I am using WordPress, and I want to show 5 items from Search Engine (bing), related to my blog content. I want to change "This+Is+Search+Keyword" with my post title ($title or the_title()), so every single post in my blog will have 5 related bing results. Here the code I am using. Can somebody help me? <?php function DisplayTitle($title){ $title = the_title(); //this is my post title// $title = str_replace(' ', '+', $title); return ($title); } $FixTitle = DisplayTitle($title); $rss = new DOMDocument(); $rss->load('http://www.bing.com/search?q=This+Is+Search+Keyword&format=RSS'); // <<<<<< I want to change "this is keyword" with "my post title"// $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 5; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $date = date('l F d, Y', strtotime($feed[$x]['date'])); echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />'; echo '<small><em>Posted on '.$date.'</em></small></p>'; echo '<p>'.$description.'</p>'; } ?> Quote Link to comment Share on other sites More sharing options...
profmustamar Posted October 19, 2012 Author Share Posted October 19, 2012 I have been trying with this: $rss->load('http://www.bing.com/search?q='.$FixTitle.'&format=RSS'); But it doesn't work. Can somebody help? Quote Link to comment Share on other sites More sharing options...
TOA Posted October 19, 2012 Share Posted October 19, 2012 But it doesn't work. Can you elaborate? Do you have error reporting turned on? Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 19, 2012 Share Posted October 19, 2012 First, echo that string, then go visit the url. What do you see? Quote Link to comment Share on other sites More sharing options...
profmustamar Posted October 19, 2012 Author Share Posted October 19, 2012 I got error when Echo it first. btw, Please check in my blog: http://caracepathamils.com/cara-mendapatkan-anak-perempuan/ You will see under the comment form, "Related post to... (post title)". There should be the results from this code. <h2>Related Posts to <u><?php the_title();?></u>:</h2> <?php function DisplayTitle($posttitle){ $posttitle = the_title(); //this is my post title// $posttitle = str_replace(' ', '+', $posttitle); return ($posttitle); } $FixTitle = DisplayTitle($posttitle); $rss = new DOMDocument(); $rss->load('http://www.bing.com/search?q=This+Is+Keyword&format=RSS'); // <<< I want to change "this is keyword" with "my post title"// $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 5; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $date = date('l F d, Y', strtotime($feed[$x]['date'])); echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />'; echo '<small><em>Posted on '.$date.'</em></small></p>'; echo '<p>'.$description.'</p>'; } ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 19, 2012 Share Posted October 19, 2012 No, I'm not going to your blog, especially if you won't even help us help you. You get an error? Great!!! WHAT WAS IT? Quote Link to comment Share on other sites More sharing options...
profmustamar Posted October 19, 2012 Author Share Posted October 19, 2012 Okay. After changing to this: $rss->load('http://www.bing.com/search?q='.$FixTitle.'&format=RSS'); I got no result. (Check attachment) Then after I echo it first, I got this: Parse error: syntax error, unexpected T_ECHO in /home/mustamar/public_html/caracepathamils.com/wp-content/themes/twentyten/single.php on line 33 Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 19, 2012 Share Posted October 19, 2012 Then your syntax is wrong. You didn't post your new code. We can't see your screen. We can't see your code. We can't see your errors. You have a basic syntax error, look at the code you added. Are you using an IDE with syntax highlighting? It would be obvious. Quote Link to comment Share on other sites More sharing options...
profmustamar Posted October 19, 2012 Author Share Posted October 19, 2012 I change to this: $rss->load('http://www.bing.com/search?q='.echo $FixTitle.'&format=RSS'); I got syntax error, unexpected T_ECHO. <h2>Related Posts to <u><?php the_title();?></u>:</h2> <?php function DisplayTitle($posttitle){ $posttitle = the_title(); /** this is my post title */ $posttitle = str_replace(' ', '+', $posttitle); return ($posttitle); } $FixTitle = DisplayTitle($posttitle); $rss = new DOMDocument(); $rss->load('http://www.bing.com/search?q='.echo $FixTitle.'&format=RSS'); /* <<< I want to change "this is keyword" with "my post title" */ $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 5; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $date = date('l F d, Y', strtotime($feed[$x]['date'])); echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />'; echo '<small><em>Posted on '.$date.'</em></small></p>'; echo '<p>'.$description.'</p>'; } ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 19, 2012 Share Posted October 19, 2012 Well, that's not how you use echo. That's clearly not valid PHP. You need to echo the entire URL you are trying to load, and do it on it's own line. Quote Link to comment Share on other sites More sharing options...
profmustamar Posted October 19, 2012 Author Share Posted October 19, 2012 I found the solution. Thank you, anyway. <h2>Related Posts to <u><?php the_title();?></u>:</h2> <?php function DisplayTitle($posttitle){ $posttitle = the_title('','',false); /** this is my post title */ $posttitle = str_replace(' ', '+', $posttitle); return ($posttitle); } $FixTitle = DisplayTitle($posttitle); $rss = new DOMDocument(); $rss->load("http://www.bing.com/search?q=".$FixTitle."&format=RSS"); /* <<< I want to change "this is keyword" with "my post title" */ $feed = array(); foreach ($rss->getElementsByTagName('item') as $node) { $item = array ( 'title' => $node->getElementsByTagName('title')->item(0)->nodeValue, 'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue, 'link' => $node->getElementsByTagName('link')->item(0)->nodeValue, 'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue, ); array_push($feed, $item); } $limit = 5; for($x=0;$x<$limit;$x++) { $title = str_replace(' & ', ' & ', $feed[$x]['title']); $link = $feed[$x]['link']; $description = $feed[$x]['desc']; $date = date('l F d, Y', strtotime($feed[$x]['date'])); echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />'; echo '<small><em>Posted on '.$date.'</em></small></p>'; echo '<p>'.$description.'</p>'; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.