Inserting HTML anchor target="_blank" into a PHP echo statement - php

This is my first time using stackoverflow, so I apologize if I did anything incorrectly. The title says it all: I need to insert a target="_blank" (or make two of these six links open in a new window/tab) into this php echo statement without removing any of the php code. The second and fourth list items are the links I'm trying to change. I'm using TomatoCart if that helps at all.
<?php
echo '<li ' . ($osC_Template->getGroup() == 'index' && $osC_Template->getModule() == 'index' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link(FILENAME_DEFAULT, 'index'), $osC_Language->get('home')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link('http://www.acehardwaretexas.com/files/circular/index.html'), $osC_Language->get('promos')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link(FILENAME_INFO, 'articles&articles_id=1'), $osC_Language->get('services')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link('https://www.acehardware.com/acerewards'), $osC_Language->get('rewards')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link(FILENAME_INFO, 'articles&articles_id=2'), $osC_Language->get('hours')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'contact' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . osc_link_object(osc_href_link(FILENAME_INFO, 'contact'), $osC_Language->get('contact_us')) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>';
?>

You could run str_replace over the osc_link_object function and replace <a with <a target="_blank"
For example, the second link could be
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span>' . str_replace("<a ", "<a target='_blank' ", osc_link_object(osc_href_link('http://www.acehardwaretexas.com/files/circular/index.html'), $osC_Language->get('promos'))) . '<span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .

Replaced link objects with anchor tags.
<?php
echo '<li ' . ($osC_Template->getGroup() == 'index' && $osC_Template->getModule() == 'index' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="index">home</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="http://www.acehardwaretexas.com/files/circular/index.html">promos</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span<a target="_blank" href="articles&articles_id=1">services</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="https://www.acehardware.com/acerewards">rewards</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'articles' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="articles&articles_id=2">hours</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>' .
'<li ' . ($osC_Template->getGroup() == 'info' && $osC_Template->getModule() == 'contact' ? 'class="navVisited"' : null) . '><span class="navLeftHook"> </span><a target="_blank" href="contact">contact_us</a><span class="navHoverDownHook"> </span><span class="navRightHook"> </span></li>';
?>

Related

How to change text Home to icon Home on WordPress?

I want to edit the breadcrumbs of website (MH Themes). It only is change the 'text Home' (in echo line) to 'icon Home' (<i class="fa fa-home" aria-hidden="true"></i>). I have try a ten times but I can't.
<?php
/***** Breadcrumbs *****/
if (!function_exists('mh_magazine_breadcrumb')) {
function mh_magazine_breadcrumb() {
if (!is_home() && !is_front_page()) {
global $post;
$mh_magazine_options = mh_magazine_theme_options();
if ($mh_magazine_options['breadcrumbs'] == 'enable') {
$item_counter = 1;
$delimiter = '<span class="mh-breadcrumb-delimiter"><i class="fa fa-angle-right"></i></span>';
$before_link = '<span itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem">';
$before_title = '<span itemprop="name">';
$close_span = '</span>';
echo '<nav class="mh-breadcrumb" itemscope itemtype="http://schema.org/BreadcrumbList">' . $before_link . '' . $before_title . esc_html__('Home', 'mh-magazine') . $close_span . '' . '<meta itemprop="position" content="' . $item_counter . '" />' . $close_span . $delimiter;
$item_counter++;
This is what's creating the Home text:
esc_html__('Home', 'mh-magazine')
That needs to be changed into the icon you want to display.

combination php and html code have error - inside hr tag in all li tag except last li tag

this is my code :
$output .=
'<li class="recentcomments recent-comment">
<div class="wbrc-header">
<span class="wbrc-header-avatar">' . get_avatar( $comment, 50/*avatar_size*/ ) . '</span>
<div class="wbrc-header-AD">
<span class="wbrc-header-author">' . $author . '</span>
<span class="wbrc-header-date">' . $date . '</span>
</div>
</div>
<div class="wbrc-body">
<span class="wbrc-body-comment">' . $comment_text . '</span>
</div>'.
if( ($key + 1) != $comments->count() ) {
echo '<hr>';
} .
'</li>';
in this line - i want add "hr tag" in all "li tag" - except last li - but this line have error - what i must do?
if( ($key + 1) != $comments->count() ) {
echo '<hr>';
}
You need to move your condition outside the concatenation.
// here i am using ternary operator for your condition.
$condition = (($key + 1) != $comments->count() ? '<hr>' : '');
Then, you can use like that:
$output .=
'<li class="recentcomments recent-comment">
<div class="wbrc-header">
<span class="wbrc-header-avatar">' . get_avatar( $comment, 50/*avatar_size*/ ) . '</span>
<div class="wbrc-header-AD">
<span class="wbrc-header-author">' . $author . '</span>
<span class="wbrc-header-date">' . $date . '</span>
</div>
</div>
<div class="wbrc-body">
<span class="wbrc-body-comment">' . $comment_text . '</span>
</div>'.$condition. // change this part
'</li>';
Using IF condition with concatenation (.) will produce "syntax error, unexpected 'if'".

How to add a "active" class to a carousel first element

I'm developing a News website in Wordpress, and I need to show in a bootstrap carousel the first three posts, my problem is that I need to add the "active" class only at the first of the three elements, but really don't know how to. Here's my code:
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
?>
I've already tried a answer found on this site (this one):
$isFirst = true;
foreach ($recent_posts as $recent) {
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ": <strong>" .$recent["post_title"] . '</strong></a></div>';
$isFirst = false;
?>
but it just printed me the "active" words.
Thanks for your help
You need to set $i so that you can count how many times you have gone through the loop and do some logic with it, like in my example below. Instead of having two lines of code that are nearly identical like I have done below though, you should be able to do the if conditional right around the class active. I didn't do that so you could clearly see the conditional and the count of the loops through the array.
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
$i = 0;
foreach ($recent_posts as $recent) {
if ($i == 0) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
} else {
echo '<div class="item"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
$i++;
}
?>

Link in Wordpress shortcode adds an extra space at the end

I created a shortcode for tooltips we can use on our wordpress site. The shortcode is just a link and it displays a tooltip when you hover over the link although for some reason when using the shortcode it adds an extra space at the end of the link and I can't figure out how to get rid of it. You can see the issue on the page to our site at
http://stormable.com/hero-lore-tyrande-whisperwind/ If you look at any of the links inside the content you'll see an extra space after the link.
add_shortcode('tyrande', 'tyrande');
function tyrande( $atts, $content = null ) {
// Hero Meta Data
$HeroID = 337;
$Hero = Tyrande;
$hero = tyrande;
$franchise = warcraft;
$heroSubname = get_post_meta( $HeroID, 'hero-sub-name', true);
$health_gain = get_post_meta( $HeroID, 'health-gain', true);
$heroHealth = $health_gain*24+get_post_meta($HeroID, "health-lvl1", true);
$healthRegenGain = get_post_meta( $HeroID, 'health-regen-gain', true);
$heroHealthRegen = $healthRegenGain*24+get_post_meta($HeroID, "health-regen-lvl1", true);
$manaGain = get_post_meta( $HeroID, 'mana-gain', true);
$heroMana = $manaGain*24+get_post_meta($HeroID, "mana-lvl1", true);
$manaRegenGain = get_post_meta( $HeroID, 'mana-regen-gain', true);
$heroManaRegen = $manaRegenGain*24+get_post_meta($HeroID, "mana-regen-lvl1", true);
$attackSpeedGain = get_post_meta( $HeroID, 'attack-speed-gain', true);
$heroAttackSpeed = $attackSpeedGain*24+get_post_meta($HeroID, "attack-speed-lvl1", true);
$attackDamageGain = get_post_meta( $HeroID, 'attack-damage-gain', true);
$heroAttackDamage = $attackDamageGain*24+get_post_meta($HeroID, "attack-damage-lvl1", true);
extract( shortcode_atts( array( 'icon' => 'false'), $atts ) );
if($icon == 'true'){
$output = '<img class="hero-tt-icon" src="http://stormable.com/img/heroes/' . $hero . '/' . $hero . '-tt-icon.png" alt="' . $hero . ' Heroes of the Storm">';
}
$output .= '
<a class="tooltip"
href="http://stormable.com/heroes/' . $hero . '/">' . $Hero . '
<span class="hero-tooltip">
<span class="hero-tt-image"><img src="http://stormable.com/img/heroes/' . $hero . '/' . $hero . '-tt.png" alt="Heroes of the Storm ' . $Hero . '" /></span>
<span class="hero-tt-info">
<span class="hero-tt-name-info">
<span class="hero-tt-fran"><img src="http://stormable.com/img/icons/' . $franchise . '.png" alt="Heroes of the Storm ' . $franchise . ' Franchise" /></span>
<span class="hero-tt-name">' . $Hero . '</span>
<span class="hero-tt-subname">' . $heroSubname . '</span>
</span>
<span class="HeroStats">
<span class="HeroStats-title">Hero Stats at Level 25</span>
<span class="HeroStats-row">
<span class="HeroStat-left">Health</span>
<span class="HeroStat-right">' . $heroHealth . ' (' . $health_gain . ' / level)</span>
</span>
<span class="HeroStats-row">
<span class="HeroStat-left">Health Regen</span>
<span class="HeroStat-right">' . $heroHealthRegen . ' (' . $healthRegenGain . ' / level)</span>
</span>
<span class="HeroStats-row">
<span class="HeroStat-left">Mana</span>
<span class="HeroStat-right">' . $heroMana . ' (' . $manaGain . ' / level)</span>
</span>
<span class="HeroStats-row">
<span class="HeroStat-left">Mana Regen</span>
<span class="HeroStat-right">' . $heroManaRegen . ' (' . $manaRegenGain . ' / level)</span>
</span>
<span class="HeroStats-row">
<span class="HeroStat-left">Attack Speed</span>
<span class="HeroStat-right">' . $heroAttackSpeed . ' (' . $attackSpeedGain . ' / level)</span>
</span>
<span class="HeroStats-row">
<span class="HeroStat-left">Attack Damage</span>
<span class="HeroStat-right">' . $heroAttackDamage . ' (' . $attackDamageGain . ' / level)</span>
</span>
</span>
<span class="clear"></span>
</span>
</a>
';
return $output;
}
I will suggest you to use HEREDOC to generate HTML in PHP.
For quick solution add use preg_replace to remove unwanted space & new line from your output. Add below line just before your return $output statement.
$output = preg_replace('/^\s+|\n|\r|\s+$/m', '', $output);

PHP Navigation calling same <ul>

I am trying to integrate boostrap and I am facing something I just don't understand very well... PHP. Basically What I need is to add class="nav" to a ul and class="dropdown-menu" to the submenu ul but it's repeating twice the nav class. Here is the code. Thanks so much for the help!
<div class="navbar navbar-inverse">
<div class="navbar-inner">
<div class="navbar">
<!-- menu start -->
<ul class="nav" id="nav">
<li class="level0"> <a class="dropdown-toggle" href="#" class=""><span>Dashboard</span></a></li>
<li onmouseover="Element.addClassName(this,'over')" onmouseout="Element.removeClassName(this,'over')" class=" active parent dropdown level0"> <a class="dropdown-toggle" href="#" onclick="return false" class="active"><span>Sales</span></a>
<ul class="nav">
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Orders</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Invoices</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Shipments</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Credit Memos</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Transactions</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Recurring Profiles (beta)</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Billing Agreements</span></a></li>
<li class="level1"> <a class="dropdown-toggle" href="#" class=""><span>Terms and conditions</span></a></li>
<li onmouseover="Element.addClassName(this,'over')" onmouseout="Element.removeClassName(this,'over')" class=" parent dropdown last level1"> <a class="dropdown-toggle" href="#" onclick="return false" class=""><span>Tax</span></a>
<ul class="nav"> <!--Need to add .dropdown-menu here-->
<li class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Manage Tax Rules</span></a></li>
<li class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Manage Tax Zones & Rates</span></a></li>
<li class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Import / Export Tax Rates</span></a></li>
<li class="level2"> <a class="dropdown-toggle" href="#" class=""><span>Customer Tax Classes</span></a></li>
<li class="last level2"> <a class="dropdown-toggle" href="#" class=""><span>Product Tax Classes</span></a></li>
</ul>
</li>
</ul>
</li>
<!--THE PHP-->
<?php
public function getMenuLevel($menu, $level = 0)
{
$html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
foreach ($menu as $item) {
$html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
. 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
. (!$level && !empty($item['active']) ? ' active' : '') . ' '
. (!empty($item['children']) ? ' parent dropdown' : '')
. (!empty($level) && !empty($item['last']) ? ' last' : '')
. ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
. (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
. (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
. ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
. $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
if (!empty($item['children'])) {
$html .= $this->getMenuLevel($item['children'], $level + 1);
}
$html .= '</li>' . PHP_EOL;
}
$html .= '</ul>' . PHP_EOL;
return $html;
}
I can't test it well, since I don't have a space to put all those fields and some other parts, but I see you have a recursively called the function. This will cause a second 'id="nav"'.
public function getMenuLevel($menu, $level = 0)
{
/* declared id="nav" here */
$html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
foreach ($menu as $item) {
$html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
. 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
. (!$level && !empty($item['active']) ? ' active' : '') . ' '
. (!empty($item['children']) ? ' parent dropdown' : '')
. (!empty($level) && !empty($item['last']) ? ' last' : '')
. ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
. (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
. (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
. ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
. $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
if (!empty($item['children'])) {
/* this line calls the function again which will cause id="nav" to happen again. */
$html .= $this->getMenuLevel($item['children'], $level + 1);
}
$html .= '</li>' . PHP_EOL;
}
$html .= '</ul>' . PHP_EOL;
return $html;
}
One guess to fix it might be to better check what is $level, because I think your !$level might be giving a wrong answer.
public function getMenuLevel($menu, $level = 0)
{
$idnav = '';
if ($level === 0)
$idnav = 'id="nav"';
$html = '<ul class="nav" ' . $idnav . '>' . PHP_EOL;
foreach ($menu as $item) {
$html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
.....
One other idea could be a nested loop. Maybe something like below:
<?php
public function getMenuLevel($menu, $level = 0)
{
$html = '<ul class="nav" ' . (!$level ? 'id="nav"' : '') . '>' . PHP_EOL;
/*notice*/
while (!empty($item['children'])) {
foreach ($menu as $item) {
$html .= '<li ' . (!empty($item['children']) ? 'onmouseover="Element.addClassName(this,\'over\')" '
. 'onmouseout="Element.removeClassName(this,\'over\')"' : '') . ' class="'
. (!$level && !empty($item['active']) ? ' active' : '') . ' '
. (!empty($item['children']) ? ' parent dropdown' : '')
. (!empty($level) && !empty($item['last']) ? ' last' : '')
. ' level' . $level . '"> <a class="dropdown-toggle" href="' . $item['url'] . '" '
. (!empty($item['title']) ? 'title="' . $item['title'] . '"' : '') . ' '
. (!empty($item['click']) ? 'onclick="' . $item['click'] . '"' : '') . ' class="'
. ($level === 0 && !empty($item['active']) ? 'active' : '') . '"><span>'
. $this->escapeHtml($item['label']) . '</span></a>' . PHP_EOL;
}
/* here is the tricky part */
$html .= '</li>' . PHP_EOL;
$html .= $this->getMenuLevel($item['children'], $level + 1);
}
$html .= '</ul>' . PHP_EOL;
return $html;
}
?>
You can work out the details of the tricky part with trial and error to make the result end the list correctly.

Categories