I need to make the links open in a new window php - wordpress

Working with a Wordpress theme and currently the icons in the header open in the current window which will take traffic away from the site. I would like them to open in a new window/tab. I know target="_blank" will get the result I need, just not sure of the syntax.
foreach( $gdl_social_icon as $social_name => $social_icon ) {
$social_link = get_option($social_icon['name']);
if( !empty($social_link) ) {
echo '<div class="social-icon"><a href="' . $social_link . ' ">' ;
echo '<img src="' . $social_icon['url'] . '" alt="' . $social_name . '"/>';
echo '</a></div>';
}
}

If I understand correctly what you're after, all you need is
echo '...<a href="' . $social_link . ' " target="_blank">';

Replace 4 line:
echo '<div class="social-icon"><a target="_blank" href="' . $social_link . ' ">' ;

Related

How to add nofollow to frontpage only? (Woocommerce products, internal links)

Currently it add nofollow at all products across website, how to make it at is_front_page() only?..
function woocommerce_template_loop_product_title() {
echo '<p class="name product-title ' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '">' . get_the_title() . '</p>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
Change the value of your rel attribute. So, when the user is at the frontpage, rel will have a value of nofollow. Otherwise, the value will be "" empty.
<?php
function woocommerce_template_loop_product_title() {
// Add the $nofollow
$nofollow = "";
if(is_front_page() ){
$nofollow = "nofollow";
}
echo '<p class="name product-title ' . esc_attr( apply_filters( 'woocommerce_product_loop_title_classes', 'woocommerce-loop-product__title' ) ) . '"><a href="' . get_the_permalink() . '" rel='.$nofollow.'>' . get_the_title() . '</a></p>';
?>

How to show image url in wordpress foreeach loop?

I need to display sub categories from a particular category
<?php
$subcategories = get_categories('&child_of=22&hide_empty');
foreach ($subcategories as $subcategory) {
echo '<a href="' . get_category_link( $subcategory->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $subcategory->name ) . '" ' . '>' . $subcategory->name.'</a>';
echo '<p>'.$subcategory->description.'</p>';
}
?>
I tried this
echo '<img class='hline' src='bloginfo('template_url')/img/hline.png' alt=''>';
its not working
You need to look at the basics of strings and concatenation in PHP.
You open your string with single quotes so you can't then use single quotes for HTML attributes unless you escape them first (\'hline\'). Instead you should be using double quotes for those attributes.
Then you need to break out of your string in order to use your PHP function, bloginfo(). With that said it makes more sense to use get_template_directory_uri() instead (or stylesheet directory depending on your setup).
Complete example:
echo '<img class="hline" src="' . get_template_directory_uri() . '/img/hline.png" alt="" />';
Documentation: http://php.net/manual/en/language.types.string.php
I would use instead:
get_template_directory_uri()
And I like to write it like this but that depends internally on you.
<img class="hline" src="<?php echo get_template_directory_uri();?>/img/hline.png" alt="" />';

Wordpress category images not displaying

So I tried creating some code that will display an icon on a post depending on the category. I tested it and it just brings up the alt text, the img element gets displayed in the code and the directory is correct, but no image? Is there something I'm missing, something I'm not doing with the images? Thank you.
foreach((get_the_category()) as $category) {
echo '<img class="catimg" src="images/cats/' . $category->cat_name . '.gif" alt="' . $category->cat_name . '" />'; }
Try this if you created images folder yourself inside the wp-content folder.
foreach((get_the_category()) as $category) {
echo '<img class="catimg" src="'.content_url().'/images/cats/' . $category->cat_name . '.gif" alt="' . $category->cat_name . '" />'; }
If the images are in the theme folder then you can use this:
foreach((get_the_category()) as $category) {
echo '<img class="catimg" src="'.get_template_directory_uri().'/images/cats/' . $category->cat_name . '.gif" alt="' . $category->cat_name . '" />'; }

Get correct link to category - Wordpress - Genesis - News theme

if ( $cat ) echo '<a class="custom-cat-btn" href="#cat-' . $cat . '">More ' . get_cat_name($cat) . ' Posts</a>';
This gives me:
http://mydomain.com/#cat-127
But I get a 404 when I click on the link
Can anyone help me get the actual link please
Thanks
You can retrieve the category link with get_category_link
So,
if ( $cat ) echo '<a class="custom-cat-btn" href="' . esc_url( get_category_link( $cat ) ) . '">More ' . get_cat_name($cat) . ' Posts</a>';

Styling category names in wordpress

I am trying to style category names with different colours.
Here is my PHP:
<?php
foreach((get_the_category()) as $category) {
if (is_category('5') ) {
echo '<a class="featured" href="' . get_category_link( $category->cat_ID ) . '">' . $category->cat_name . '</a>';}
else {
echo '' . $category->cat_name . ' ';}
}
?>
It is not returning errors, but it is not working either...
What am I doing wrong?
Here is the page I'm working on:
http://216.172.178.12/~saracimi/eng/
and the snippet of code is relative to the rectangular boxes at the center of the page.
Thanks a bunch!
is_category will return true when you are on that category archive. you need to check the category id so try:
foreach((get_the_category()) as $category) {
if ($category->cat_ID == 5 ) {
echo '<a class="featured" href="' . get_category_link( $category->cat_ID ) . '">' . $category->cat_name . '</a>';
} else {
echo '' . $category->cat_name . ' ';
}
}

Resources