PHP inserting variable into a link href and name - php

This is the code I am working with:
<?php
$rss = new DOMDocument();
$rss->load('http://hugeriver.wordpress.com/feed/');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
$item = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('encoded')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
);
array_push($feed, $item);
}
$limit = count($feed);
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 '<h2><a name="test">'.$title.'</a><span class="line"></span></h2>';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo '<p>'.$description.'</p>';
}
?>
I am stuck specifically with this line. I am trying to make it so the title is both the name of the ancor and the link (so when clicked it scrolls to the top). This is what I tried that doesn't work. Can anyone please show me what is wrong with my syntax?
echo '<h2><a name="'.$title'" href="#'.$title'">'.$title.'</a><span class="line"></span></h2>';

You're currently creating a link that targets itself.
If you want the link to go to the top of the document when clicked, simply link to '#':
<?php echo $title ?>
Also, name is deprecated on <a/> elements in HTML 5. Use id instead:
<?php echo $title ?>

Andre answer should solve your problem, if what you want to do is simply go to the top of the page. however if you want to go to a specific section that has id set to the value of $title then you can try this...
<?php echo $title ?>
This way when the link is clicked it will jump to the exact element with ID equal to title(which may or may not be at the top). I believe this is what you want to achieve.

<a href="#" ID="<?php echo htmlentities($title, ENT_QUOTES); ?>">
<?php echo htmlentities($title, ENT_NOQUOTES); ?></a>
Why is everyone forgetting htmlentities(), especially for attributes?
And why use $title as #target? When the $title is a variable with spaces and punctuation unfit for #target practice... Why not use an md5($title) since your generating the page dynamically? Like:
<a href="#" ID="<?php echo htmlentities(md5($title), ENT_QUOTES); ?>">
<?php echo htmlentities($title, ENT_NOQUOTES); ?></a>
and later on linking to it like this:
<a href="#<?php echo htmlentities(md5($title), ENT_QUOTES); ?>">
Go to <?php echo htmlentities($title, ENT_NOQUOTES); ?>!</a>

there are two missing dots.
try this:
echo '<h2><a name="'.$title.'" href="#'.$title.'">'.$title.'</a><span class="line"></span></h2>';

try this syntax
echo "<h2><a name=\"$title\" href=\"#$title\">$title</a><span class=\"line\"></span></h2>";
or
echo "<h2><a name='$title' href='#$title'>$title</a><span class='line'></span></h2>";
This is much cleaner and has less probability of missing a dot or closing/opening quote.

echo '<h2><a name="'.$title.'" href="#'.$title.'">'.$title.'</a><span class="line"></span></h2>';
You are missing dots after $title
Also, your links link to themself. You need to define a seperate anchor and then link to it.

Related

WP ACF echoes variable even if not wanted

I have strange problem, i've done the same thing before, many times.
But for some reason this one field just dosent want to co-operate whatsoever...
<?php if (have_rows('subfamilies_product_changes')) :
while ( have_rows('subfamilies_product_changes') ) : the_row();
$url = the_sub_field('subfamilies_product_changes_link');
echo "<div class='support-lahko'>";
echo "<div class='support-logo'>";
echo the_date( 'Y-m-d' );
echo "</div>";
echo "<div class='news-name-product'>";
echo "<h3 style='vertical-align: middle;'>" . the_sub_field('subfamilies_product_changes_title') ."</h3>";
echo "</div>";
echo "<div class='news-desc'>";
echo "</div>";
echo "</div>";
echo "<br>";
endwhile;
endif;
It seems, no matter where i put my <a> tags, this script is just going to go ahead and for some reason echo the link from $url = the_sub_field('subfamilies_product_changes_link'); into where ever it is defined. when i try to do something like <a href=' . $url . '> It will still just echo the url in <p> tags in seemingly random place.
Here is picture of what happens when i put $url inside href:
http://imgur.com/a/7AQsl
href stays empty, and wp just echoes url where it was defined. none of the text in image is a link.
I would just need to echo the url INTO the href, so i can iterate links into pdf files.
the_sub_field('subfamilies_product_changes_title')
produces text: Test Changes, as expected.
Short answer - use get_sub_field();
the_sub_field() outputs it immediately.
Glad that it helped - sometimes it is the smallest thing that ruins our day.

how can I style this php into rows of thumbnails with text under them?

So I'm trying to style this php with divs but it's not behaving how I'd like. I would like each thumbnail to have the catalog number appear under it. What's the best way to do this? I'm not super clear on how php and css interact yet.
$records = $result->getRecords();
foreach ($records as $record){
$photo = $record->getField("Photo");
$thumbnail = $record->getField("Thumbnail");
$cNum = $record->getField("Catalog_Number");
echo("<div id=\"image_catalog">"\);
echo ("<a href = \"");
echo ($photo);
echo ("\">");
echo ("<img src= \" ");
echo ($thumbnail);
echo (" \"></a>");
echo ("<font color=\"white\">");
echo ($cNum);
echo ("</font>");
echo ("</div>");
The cleanest way for your code is to use the Heredoc Syntax.
echo <<<CATALOGUE
<figure id="image_catalog">
<img src="$thumbnail" />
<figcaption>$cNum</figcaption>
</figure>
CATALOGUE; // No white space before Catalogue

Sharing content on Facebook not displaying correctly

One of the problems I have is that my title contains an "&" but it displays as "&" when I click share.
Another problem I have is that the code below does not recognize "< br >" or "\n".
How do I make it so it will recognize "&, ', -, etc.." as well as line breaks.
<?php
$gettitle = get_the_title();
$getcontent = get_the_content();
$content = substr($getcontent, 0, 20);
$content .= "<br>".$moretexthere;
$link = get_permalink();
$title=urlencode($gettitle);
$url=urlencode($link);
$summary=urlencode($content);
?>
<a onClick="window.open('http://www.facebook.com/sharer.php?s=100&p[title]=<?php echo $title;?>&p[summary]=<?php echo $summary;?>&p[url]=<?php echo $url; ?>','sharer','toolbar=0,status=0,width=548,height=325');" href="javascript: void(0)">Share</a>
Well, i use this as sharer url..
https://www.facebook.com/sharer/sharer.php?u=URLENCODEDURL
Use the urlencode PHP function.
"This function is convenient when encoding a string to be used in a
query part of a URL..."
http://php.net/urlencode
WordPress Example:
<?php echo urlencode(get_the_title()); ?>

Wordpress PHP get image gallerys URL? I got part of the code working

Hello I am trying to create the lightbox gallery in my WordPress, I managed to get most of the code working well for grabbing images out of gallery. However I don't know how to get their URL for their original files....
PHP
<?php $post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
$item = 0;
while ($array_id[$item] != ''){
if($item == 0){
echo wp_get_attachment_image($array_id[$item], 'medium');
}else{
echo "<a ref='".wp_get_attachment_image($array_id[$item])."'>";}
$item++;
}
?>
So my idea is to display IMG of a first image in the gallery which I have accomplished with the first ECHO, not for all other images I only want the links to the original files.
My final code needs to look like this:
<a rel="group1" href="image_big_1.jpg"><img src="image_small_1.jpg" alt=""/></a>
<a rel="group1" href="image_big_3.jpg"></a>
<a rel="group1" href="image_big_4.jpg"></a>
<a rel="group1" href="image_big_5.jpg"></a>
<a rel="group1" href="image_big_6.jpg"></a>
Also what would you suggest for rel='' I need this to be unique for every post with galleries I am not sure what to use I can't use TITLE because if they enter more then one gallery with same title there will be issue. I am guessing maybe some sort of (RANDOM) prefix defined by current_time?
Please help
Thank you
I found a solution and edited my code to work:
This is code to accomplish above stated:
<?php $post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
$item = 0;
while ($array_id[$item] != ''){
$url = wp_get_attachment_image_src($array_id[$item]);
if($item == 0){
echo "<a href='".$url[0]."'>".wp_get_attachment_image($array_id[$item], 'gallery-thumb')."</a>";
}else{
echo "<a href='".$url[0]."'</a>";}
$item++;
}
?>
Reference used: http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
Thank you all!

Get id value with PHP Simple HTML DOM

I have this scenario:
<div class="listing_title">
<strong>
<a href="http://www.mywebsite.com/dectails23291.html" id="url_id_1977">
Listing Title
</a>
</strong>
</div>
To get Listing Title, I have implemented this code:
$page = "http://www.mywebsite.com/listings.html";
$html = new simple_html_dom();
$html->load_file($pagina);
foreach($html->find('.listing_title') as $element)
echo $element->first_child()->plaintext . '<br>';
OUTPUT IS:
Listing Title
Now I need get id value
url_id_1977
preferably only "1977", clean of "url_id_", but I dont know do. Thanks in advance!!
Add this inside of your foreach loop:
echo end(explode('_', $element->find('a', 0)->id));
To get rid of the warning you could assign the id to a variable:
$id = explode('_', $element->find('a', 0)->id);
echo $id[2];
Or, if your anchor's id always starts with url_id_, just use str_replace():
echo str_replace('url_id_', '', $element->find('a', 0)->id);
try this
foreach($html->find('*[class=listing_title] a') as $element)
echo $element->id. '<br>';

Categories