PHP make the whole container as link (not only the button) - php

im a total php illiterate so i wanted to make a change in the php file of my wordpress website. i have some cards which represent my services but the clickable link is only in the "read more..." phrase but i want to make the whole card be as a link.
below is the php code i think is associated with that:
<?php elseif( $style == 'style_5' ) : ?>
<h3><?php echo esc_html( $title ); ?></h3>
<?php else : ?>
<h4 class="no_stripe"><?php echo esc_html( $title ); ?></h4>
<?php endif; ?>
</div>
<?php } ?>
<?php echo wpb_js_remove_wpautop( $content, true ); ?>
<?php
if ( $link['url'] ) {
if ( ! $link['title'] ) {
$link['title'] = esc_html__( 'Read More', 'consulting' );
}
if ( ! $link['target'] ) {
$link['target'] = '_self';
}
if( $icon ){
$link['title'] = '<span>' . esc_html( $link['title'] ) . '</span>' . '<i class=" ' . esc_attr( $icon ) . ' stm_icon"></i>';
}
echo ' <a class="read_more" target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title'] . '</a>';
}
?>
<?php if( $style == 'style_3' ): ?>
</div>
<?php endif; ?>
<?php endif; ?>

In the html code, try to put the whole card inside of an 'a tag', this should make the whole card a link, and in the 'a tag' put your desired link in the href="". This should work!

Well, this is the link (from your code):
echo ' <a class="read_more" target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title'] . '</a>';
So, I would simply try to wrap that around the container of your "card" by splitting it up into...
echo ' <a class="read_more" target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title'];
...and:
echo '</a>';
The code of the container has to go between those two parts. Most likely that would be directly under the first line of the code you posted for the first part until the original position of the closing a tag. And since there is (non-php) HTML code in that section, that first line should be wrapped by php tags, like:
<?php echo ' <a target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title']; ?>
Probably the "read more" class whould be avoided in the link now.
So the complete code would be
<?php elseif( $style == 'style_5' ) : ?>
<?php echo ' <a target="' . esc_attr( $link['target'] ) . '" href="' . esc_url( $link['url'] ) . '">' . $link['title']'; ?>
<h3><?php echo esc_html( $title ); ?></h3>
<?php else : ?>
<h4 class="no_stripe"><?php echo esc_html( $title ); ?></h4>
<?php endif; ?>
</div>
<?php } ?>
<?php echo wpb_js_remove_wpautop( $content, true ); ?>
<?php
if ( $link['url'] ) {
if ( ! $link['title'] ) {
$link['title'] = esc_html__( 'Read More', 'consulting' );
}
if ( ! $link['target'] ) {
$link['target'] = '_self';
}
if( $icon ){
$link['title'] = '<span>' . esc_html( $link['title'] ) . '</span>' . '<i class=" ' . esc_attr( $icon ) . ' stm_icon"></i>';
}
echo '</a>'; } ?>
<?php if( $style == 'style_3' ): ?>
</div>
<?php endif; ?>
<?php endif; ?>
All this is based on the code you provided, there might be other stuff to consider, but you can try this.

Related

Does anybody know how to change the color depending on stringname?

I try to change the color of an order-status-span-element to color red, if the order status is "Pending payment".
As soon as the status changes to "completed", the span text color should switch to green.
Here is the clean div code:
<div class="order-status">
<span>Order Status</span>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo '<span id="order-status-value">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '<span>'; ?>
</div>
I tried to insert a proper if-statement, but I don't know which function or variable I need to insert.
This is the snippet is only a part of my orders.php ->(.../my-account/orders).
<div class="order-status">
<span>Order Status</span>
<?php elseif ( 'order-status' === $column_id ) : ?>
<?php echo '<span id="order-status-value">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '<span>';
if ( strcasecmp( wc_get_order_status_name( $order->get_status() ) == 0 ) ) :
echo "The if statement works!";
?>
<style type="text/css">
#order-status-value {
color: green;
}
</style>
<?php
else:
?>
<style type="text/css">
#order-status-value {
color: red;
}
</style>
</div>
</div>
This code doesn't work.
You could use the "style" HTML attribute to change the color :
<?php
$is_green = wc_get_order_status_name($order->get_status()) == 0;
echo '<span id="order-status-value" '
. ($is_green ? 'style="color:green"' : '')
. '>'
. esc_html( wc_get_order_status_name( $order->get_status() ) )
. '<span>';
Here is the solution I use at the moment - this one works perfectly.
<div class="order-status">
<span>Order Status</span>
<?php
elseif ( 'order-status' === $column_id ) :
$order_color_check = $order->get_status();
if($order_color_check=="completed") :
echo '<span class="order-status-value" style="color: green;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
else :
echo '<span class="order-status-value" style="color: red;">' . esc_html( wc_get_order_status_name( $order->get_status() ) ) . '</span>';
endif; ?>
</div>

List all the tags in a WordPress blog

I need to list all the tags with at least 1 published post in a WordPress blog.
The list is about the full blog and not related to the current post.
What code would you use?
<ul class="tag-manually">
<?php
$tags = get_tags('post_tag'); //taxonomy=post_tag
if ( $tags ) :
foreach ( $tags as $tag ) : ?>
<li class="tag-item-manually"><a class="tag" href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
$tags = get_tags();
$html = '<ul>';
foreach ( $tags as $tag ) {
if($tag->slug != "migliori"){
$tag_link = get_tag_link( $tag->term_id );
$html .= "<li><a href='{$tag_link}' class='{$tag->slug}'>";
$html .= "{$tag->name}</a></li>";
}
}
$html .= '</ul>';
echo $html;
This works.
I tried this and worked for me.
<?php
$tags = get_tags();
if ($tags) {
?><ul class="tags"><?php
foreach ($tags as $tag) {
echo '<li><a href="' . get_tag_link( $tag->term_id ) . '"
title="' . sprintf( __( "View all posts in %s" ), $tag-
>name ) . '" ' . '>' . $tag->name.'</a></li>';
}
echo '<li>View All<span class="arrow"></span>
</li>'; ?></ul>
<?php }?>
edit this code and work for me !!!
<ul class="tags">
<?php
$tags = get_tags('post_tag'); //taxonomy=post_tag
//echo "<pre>";
//print_r($tags);
//var_dump($tags);
if ( $tags ) :
foreach ( $tags as $tag ) : ?>
<li><a class="tag" href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" title="<?php echo esc_attr( $tag->name ); ?>"><?php echo esc_html( $tag->name ); ?></a></li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

How to add div's with variables into a wordpress function?

Hey all, I'm trying to add some div's with variables into a function. I know the html needs to be wrapped, what is the best way to do this? Here is the code I'm trying to add to a function.
function add_before_sidebar( ) {
<div class="acro-sidebar-preview">
<div class="acro-sidebar">
<div class="image-container">
<div id="profile-picture-preview" class="profile-picture" style="background-image: url(<?php print $picture; ?>)">
</div>
</div>
<h1 class="acro-username"><?php print $fullname; ?></h1>
<h2 class="acro-description"><?php print $description; ?></h2>
<div class="icons-wrapper">
<?php print '<img src="' . plugins_url( 'img/twitter.png', __FILE__ ) . '" >' ?>
<?php print '<img src="' . plugins_url( 'img/facebook.png', __FILE__ ) . '" >' ?>
<?php print '<img src="' . plugins_url( 'img/googleplus.png', __FILE__ ) . '" >' ?>
</div>
</div>
</div>
}
add_action( 'get_sidebar', 'add_before_siderbar' );
All of the above code is connected to this set of variables I have setup
<?php
$picture = esc_attr( get_option('profile_picture') );
$firstname = esc_attr( get_option('first_name') );
$lastname = esc_attr( get_option('last_name') );
$fullname = $firstname . ' ' . $lastname;
$description = esc_attr( get_option('user_description') );
$twitter = esc_attr( get_option('twitter_handler') );
$facebook = esc_attr( get_option('facebook_handler') );
$google = esc_attr( get_option('google_handler') );
?>
The code itself works great outside of the function. I'm familiar with writing a div with variable in a function but this is a bit more complex then I'm used to. Any help would be appreciated. Thanks in advance!
You can write like this:
function add_before_sidebar( ) {
?>
<div class="acro-sidebar-preview">
<div class="acro-sidebar">
<div class="image-container">
<div id="profile-picture-preview" class="profile-picture" style="background-image: url(<?php print $picture; ?>)">
</div>
</div>
<h1 class="acro-username"><?php print $fullname; ?></h1>
<h2 class="acro-description"><?php print $description; ?></h2>
<div class="icons-wrapper">
<?php print '<img src="' . plugins_url( 'img/twitter.png', __FILE__ ) . '" >' ?>
<?php print '<img src="' . plugins_url( 'img/facebook.png', __FILE__ ) . '" >' ?>
<?php print '<img src="' . plugins_url( 'img/googleplus.png', __FILE__ ) . '" >' ?>
</div>
</div>
</div>
<?php }
add_action( 'get_sidebar', 'add_before_siderbar' );

WordPress Adding tags to loop content

I am wrapping my head on wordpress loop at the moment, im trying to give tags to the respective content, so an H tag to the title, an p tag to the excerpt and so on...
The code i got so far is
<div id="<?php echo $page_id; ?>" class="container"><!-- begin container -->
<div id="postovi" style="display:none;">
<?php $custom_loop = new
WP_Query('showposts=5&category_name=Zanimljivosti&orderby=rand');
if ( $custom_loop->have_posts() ) : echo '<ul>'; while ( $custom_loop->have_posts() ) : $custom_loop->the_post(); echo '<li>' . get_the_title() . get_the_post_thumbnail($loop->post->ID, 'shop_catalog') . get_the_excerpt();'</li>'; endwhile; wp_reset_query(); echo '</ul>';endif;?> </div>
any suggestions apreciated :)
So your solution is :
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Zanimljivosti&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ) :
$custom_loop->the_post(); echo '<li><h2><a href="' . get_permalink() . '">' . get_the_title();
echo '</h2>' . get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
echo'<p>' . get_the_excerpt();'</p></a></li>';
endwhile; wp_reset_query();
echo '</ul>';endif;
?>

wordpress image isn't displaying where it supposed to

I'm trying to create a small wp plugin for my blog, but I've got the following problem.
The post image, isn't displaying in the right spot.
This is the proper HTML
<li>
<div class="projects">
<ul class="projects sticker">
<li><h2><?php the_title(); ?></h2></li>
<li><p>details</p></li>
</ul>
<img src="" />
</div>
</li>
This is how it's displaying now
<li>
<div class="projects">
<ul class="projects sticker">
<li><h2><?php the_title(); ?></h2></li>
<li><p>details</p></li>
</ul>
</div>
</li>
<img src="" />
Basically i have to put the img tag inside the list and div
Here is my code so far
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>'
. '<div class="projects">'
. '<ul class="projects sticker">'
. '<li>'
. '<h2>'
. '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'
. $recent["post_title"]
. '</a>'
. '</h2>'
. '</li>'
. '<li><p>details</p></li>'
. '</ul>'
. '<img src="'.the_post_thumbnail('thumbnail').'" />'
. '</div>'
. '</a>';
use this code, you have used extra <li></li>
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) .
'" title="Look '.esc_attr($recent["post_title"]).'" >'
.'<div class="projects">' .'<ul class="projects sticker">'
.'<li>' .'<h2>' . $recent["post_title"] .'</h2>' .'</li>'
.'<li><p>details</p></li></ul>'
.'<img src="'.the_post_thumbnail('thumbnail').'" />'
.'</div>' .'</a>';
}
You have an extra closing <li> at the end and the placement of the closing tag of the first <li> is improperly nested and <a href> opening & closing tag is misplaced as well. Also you could have solved this problem easier—possibly by yourself—if you format the code so humans it can more easily be read. Piling on a stack of instructions on one line like that will only cause confusion:
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>'
. '<div class="projects">'
. '<ul class="projects sticker">'
. '<li>'
. '<h2>'
. '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'
. $recent["post_title"]
. '</a>'
. '</h2>'
. '</li>'
. '<li><p>details</p></li>'
. '</ul>'
. '<img src="'.the_post_thumbnail('thumbnail').'" />'
. '</div>'
. '</a>'
;

Categories