Link is not appearing correct in wordpress - php

I am trying to get the link from the database in WordPress and user should direct to that link on a single click.
I have almost achieved everything but there is one weird problem I am facing.
In my custom php file I have this code.
<div style =" margin-bottom: 40px; "><div><li><a href="<?php echo $array_var ?>">
<?php echo get_the_post_thumbnail( $post_id, 350,'' ); ?></br><h2><?php the_title(); ?></h2></a><?php echo get_the_excerpt();?></li></div></div>
Here ahref tag is coming as "website custom page link/database-link". Whereas at other places it is coming only as link from the database (which is the correct case for me.)
I will explain it little more.
<?php $query = new WP_Query( array(
'post_type' => 'post',
'category_name' => 'Trending',
'posts_per_page' => 8
) );
$i=0;
while ($query->have_posts()) : $query->the_post();
$post_id= $query->post->ID;
$array_var=$link_array[$i];
**echo $array_var;**
?>
<ul>
<div style =" margin-bottom: 40px; "><div><li>**<a href="<?php echo $array_var ?>">**
<?php echo get_the_post_thumbnail( $post_id, 350,'' ); ?></br><h2><?php the_title(); ?></h2></a><?php echo get_the_excerpt();?></li></div></div>
<?php
$i++;
endwhile; ?>
</ul>
I have marked two places in the code in asterisk. Both have same code but behaviour is different. At first place I am getting link as, say, www.google.com and at second place I am getting it as /www.google.com.
I want it to be www.google.com only.
For reference, the corresponding page is www.coolfuzz.com, you can see that in the first row above every thumbnail is the correct link(which is not clickable btw) but thumbnail has incorrect link.
Please tell me how to correct this?

You should add the http:// to the beginning

Related

How to prevent PHP Code outputting the content in bold?

In my WordPress website, I have created a category template file (archive file) for showing all posts from a specific category. The code is working fine and I am getting the exact result what I need. However, there is some problem with my code (which I am unable to investigate).
The template file outputs 5 posts and the content of the first 2 posts is exactly what they are. However, the content of the remaining 3 posts appears in bold, which is strange.
Here is my code:
<section class="video_categories" id="content" style="float:left;">
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'sponsor-spotlight',
'posts_per_page' => -1
);
$obituary_query = new WP_Query($args);
while ($obituary_query->have_posts()) : $obituary_query->the_post();
$readMore = ' Keep On Reading...';
echo '<div class="advert-div" style="padding-bottom: 15px;">'; ?>
<h2 class="ad-title"> <?php echo '<a style="color: #333333;font-weight: 600;" href="' . get_the_permalink() . '">';?> <?php the_title(); echo '</a>'; ?>
</h2>
<?php
$post = get_post( $post->ID );
$content_arr = get_extended($post->post_content);
echo apply_filters('the_content', $content_arr['main']);
echo $readMore;
echo '</div><hr>';
endwhile;
wp_reset_postdata();
?>
</section>
As per my investigation, the problem lies in the following lines of code because it results in outputing <strong> </strong> tags which enclose the content:
$content_arr = get_extended($post->post_content);
echo apply_filters('the_content', $content_arr['main']);
Because when I do:
echo apply_filters('the_content', $post->post_content);
I do not see any <strong></strong> tag on the front end that encloses the content.
Here is the strange output of the code I am using above.
Any help will highly be appreciated!
Although there was no reason for the code to output <strong></strong> tags on the front end, so I practiced #Jayr recommendations by changing the status of each post which was showing in bold. The problem was within the content of those posts. I simply recreated new posts with the same content and my problem was resolved.
Thanks to #Jayr for his quick response!

How to display last published post?

I have a task to modify the code in one website so that it shows the latest post on the top of the page. The rest of the page is blog posts and this part of the code is fine. I'm just starting to learn PHP and have no clue what I need to do.
Here is the website so you can have a better understanding of what I need - https://yourbestbrace.staging.wpengine.com/
The post that is currently on top is not the last one published.
Also, the piece of the code that needs to be changed is placed in the header.php. Here it is ( full code screenshot since it wouldn't display the code properly, image part is missing - https://prnt.sc/oy6r8q):
$the_query = new WP_Query( array(
'posts_per_page' => 1,
'meta_key' => 'show_as_first',
'meta_value' => 1
));
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h1 class="new-home-title">Products Reviews of the Best Braces & Supports for You</h1>
<div class="header-post">
<div class="h-post-detail">
<p class="sub-title-home">Featured Review</p>
<h2> <?php the_title(); ?></h2>
<p><?php echo wp_trim_words( get_the_excerpt(), 22 ); ?></p>
Read More
</div>
<?php endif;

How to wrap echo div in <a href>

I'm running into two problems transferring a Wordpress archive page into a page template.
<?php $args = array( 'post_type' => 'casestudies', 'posts_per_page' => 12 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="casestudy">'.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>';
First I need to wrap the echo for the thumbnail with a link in order to trigger a css action:
'<a href="'.the_permalink().'" class="anchor-hover">';
When I add that a href line it only prints the link, and doesn't wrap the echo.
The a href should then print the title and excerpt over the thumbnail box:
'<span class="details"><div class="anchor-hover details-h3"><?php the_title(); ?></div>';
'<p class="desc"><?php echo get_post($post_id)->post_excerpt; ?></p></span></a>';
endwhile; ?>
<div class="clear"></div>
</div></div>
In wordpress, you have two type types of functions. One's that return the results, and one's that echo the results.
You are using the_permalink(); which is already echoing out it's result.
You need to be using get_the_permalink(); since you are trying to echo it in your template.
get_permalink worked for that a href, thank you, but how can I get php the_title() and php echo get_post($post_id)->post_excerpt to show up on the cursor hover?
This code should be working if you are in the loop
LINK
The method get in WordPress will give you the string only.
http://codex.wordpress.org/Function_Reference/get_the_excerpt
http://codex.wordpress.org/Function_Reference/get_the_title

PHP concatenation syntax error when using css for animation effect

In a Wordpress page template I have the following code in an effort to replicate a successful archive page:
<?php $args = array( 'post_type' => 'casestudies', 'posts_per_page' => 12 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="casestudy">'.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>';
echo '<span class="details"><div class="anchor-hover details-h3">'.the_title().'</div>';
echo '<p class="desc">'.get_post($post_id)->post_excerpt.'</p></span>';
endwhile; ?>
<div class="clear"></div>
I need to compress that into only one echo, in order to have the css animation work. I will also need to wrap the entire display starting with "echo" with this div:
<div<?php post_class('margin') ?> id="post-<?php the_ID(); ?>">
The title and excerpt are only supposed to show when the cursor is hovering over the thumbnail, but I can't get this line of code in without an unexpected syntax error.
I am able to get this to work on the archive for this post type so there should be some way to do it.
Am I asking too much of this kind of code? Is there a reason I can get the animation working on the archive page working but not this one? Thanks
you can't double parse php...
echo '<div class="casestudy">'.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>';
should be
echo '<div class="case study">'.get_the_post_thumbnail( $post->ID, '180,180' ).'</div>';
specifically:
<?php the_permalink() ?>
should be
.'the_permalink().'
It might help you to format your code a bit. It looks like there may be problems with single and double quotes, along with PHP tags inside strings. If you are creating large HTML template fragments, you may want to use something like ob_start() and ob_get_clean()
Here's just a quick example using your code:
<?php
$args = array( 'post_type' => 'casestudies', 'posts_per_page' => 12 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
ob_start();
?>
<div class="casestudy">
<a href="<?php the_permalink() ?>" class="anchor- hover">
<?php
echo get_the_post_thumbnail( $post->ID, '180,180' );
?>
</a>
</div>
<span class="details">
<div class="anchor-hover details-h3">
<?php
echo the_title();
?>
</div>
<p class="desc">
<?php
echo get_post($post_id)->post_excerpt;
?>
</p>
</span>
<?php
echo ob_get_clean();
}
?>
<div class="clear"></div>
Side note: you probably shouldn't put an inline element (span) inside a block level element (div).

Recent posts as thumbnails - want to show title below image

I have found this code for showing thumbnails as recent posts in a widget. It appears in a grid format which I like very much. I would like to add the title of the post below each thumbnail images. I can manage to get the title show by using the_title(); but then it does not stay as a grid but turns into a list. I would appreciate any help. Thanks
The css used is:
.attachment-thumbnail {
height:150px;
width:150px;
padding:5px;
background:#fff;
margin:5px 5px 0 0;
}
Code:
<?php
$my_query = new WP_Query('showposts=12&amp;amp;amp;orderby=rand');
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php
$attachments = get_posts( array(
'post_type' => 'attachment',
'number_posts' => 1,
'post_status' => null,
'post_parent' => $my_query->post->ID,
) );
if ($attachments) {
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php
$thumbnail_id = $attachments[0]->ID;
echo wp_get_attachment_image( $thumbnail_id );
}
endwhile;
}
wp_reset_query();
?>
It'd be helpful to know the CSS attributes that are being assigned to the title text when it's introduced to the page you're creating as that's likely what's turning your grid into a list. I would think the best way forward here is to wrap the thumbnail/title pairs in a div and to make those divs have the attribute "display: inline" so that the divs appear side by side rather than on top of each other.

Categories