WordPress Excerpt Doesn't Format Link Correctly - php

I'm using Wordpress Avada theme and try to modify the 'Read more' link to always go to an external site.
I'm using the following code :
function my_read_more_link( $read_more ) {
$post = get_post();
$url = 'https:/www.websitename.com/';
$postslug = $post->post_name;
$link = $url . $postslug;
$excerpt = 'Read more at websitename.com!';
return $excerpt;
}
add_filter( 'avada_blog_read_more_link', 'my_read_more_link' );
In Theory this is working because the blog page shows the following:
<a href="https:/www.websitename.com/the-post-title-here">
Read more at websitename.com!
</a>
But this is formatted as text, not as a link. I've provided some screenshots for more clarity :
Screenshot from the blog page
https://imgur.com/a/Ztof7wB
Screenshot from the source code : (output)
https://imgur.com/a/YV7JACp
Any help is appreciated!

Related

Wordpress Outputting Excerpt as link not working correctly?

I need to output a custom 'read more' button for every post.
I use the following function that I've made:
function the_excerpt_more_link( $excerpt ){
$post = get_post();
$url = 'https:/www.vierenzestig.nl/';
$postslug = $post->post_name;
$link = $url . $postslug;
$excerpt = 'Lees meer!';
return $link;
}
This is working as expected and it outputs the following :
https:/www.vierenzestig.nl/interior-car-wash-and-detailing-service
When I try to wrap the above output inside an element. It also displays the home url from the website. Using this code (see the replacement of $link with $excerpt):
function the_excerpt_more_link( $excerpt ){
$post = get_post();
$url = 'https:/www.vierenzestig.nl/';
$postslug = $post->post_name;
$link = $url . $postslug;
$excerpt = 'Lees meer!';
return $excerpt;
}
This outputs the following :
An link with the text: 'Lees Meer'. But the link is navigating to the following :
https://HOMEURLHERE/https:/www.vierenzestig.nl/interior-car-wash-and-detailing-service
Why is the same use of code resulting in a different output of code?

Remove ‘category’ from header title on category pages in wordpress

I would like to remove the ‘Category’ from header title text on category pages in wordpress. Have tried several plugins that claim to remove these but don’t seem to work with my version.. (4.9.6)
Here a screenshot of how it looks now:
https://imgur.com/a/E2gdr2l
So would want to have only ‘News’ displayed there. Anyone knows a way of getting rid of the Category before it?
All the best and thanks in advance!
In Magazine Base theme:
The title is added from the file inc/hooks/header-inner-page.php using the function the_archive_title() like
the_archive_title('<h1 class="entry-title">', '</h1>');
to remove Category from the output you need to add the following filter in your functions.php
add_filter( 'get_the_archive_title', function ($title) {
if ( is_category() ) {
$title = single_cat_title( '', false );
} elseif ( is_tag() ) {
$title = single_tag_title( '', false );
} elseif ( is_author() ) {
$title = '<span class="vcard">' . get_the_author() . '</span>' ;
}
return $title;
});
Code credit: Here
That would fix the issue.
Just add a line into your functions.php (After WP5.5).
add_filter( 'get_the_archive_title_prefix', '__return_false' );
check in archive.php or category.php
it is loading from here
<h1 class="entry-title">Category: News</h1>
you can check and edit any of the above mentioned files.

acf field data in do_shortcode not populating

I am using the WordPress Advanced Custom Field plugin. I have a custom built download button and shortcode. Now I am using ACF fields to populate the download btn shortcode. Everything seems to be working and download button is displaying as expected. The only problem is that ACF fields are not populating in the shortcode but its showing outside the shortcode code.
<?php
if (get_field('dn_btn_url1')) {
echo do_shortcode( '[download_btn url="'.get_field('dn_btn_url1').'" filename="'.get_field('dn_btn_txt1').'"]' );
}
?>
The code without the if statement is working as expected but it doesn't hide the shortcode.
The get_field function gets the post id from the global $post object so when it's passed into the shortcode function, it seems like it can't get the right post id. Try putting your URL from the field into a variable so the URL is already passed through the function and into the shortcode function.
Try this:
<?php
if (get_field('dn_btn_url1')) {
$url = get_field('dn_btn_url1');
echo do_shortcode( '[download_btn url="' . $url . '" filename="' . $url . '"]' );
}
?>
You can do that by using sprintf function please try it once
<?php
if(get_fiedl('dn_btn_url1')) {
$url = get_field('dn_btn_url1');
$filename = get_field('dn_btn_txt1');
$shortcode = sprintf(
'[download_btn url="'.$url.'" ,
filename="'.$filename.'"]');
echo do_shortcode( $shortcode );
?>

read more link only when excerpt of content is available in wordpress

i am working on a homepage which includes post, sometimes the post is only with featured image and no content, and sometime there is post with both featured image and content. i want to show excerpt with the post with a readmore button when there is content available with the post. i have added a read more button but it even shows with the post which don't have any content. how can i show it only when there is excerpt available for content. here is the code for the excerpt div.
<div class="entry-excerpt">
<?php
if( strlen( $post -> post_excerpt . $post -> post_content ) > 0 ){
?>
<div class="excerpt">
<?php
if( is_user_logged_in () ){
the_excerpt();
echo 'Continue Reading';
}else{
$meta = meta::get_meta( $post -> ID , 'settings' );
if( isset( $meta['safe'] ) ){
if( !meta::logic( $post , 'settings' , 'safe' ) ){
the_excerpt();
echo 'Continue Reading';
}
}else{
the_excerpt();
}
}
?>
</div>
<?php
}
?>
</div>
the question is solved...
here if anyone want to need it in future.
just add this code in functions.php
<?php
function new_excerpt_more($more) {
global $post;
return '<a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>'; } add_filter('excerpt_more', 'new_excerpt_more'); ?>
<?php the_content( $more_link_text , $strip_teaser ); ?>
The $more_link_text sets the link text like "Read More". The second one, $strip_teaser, sets whether or not the "more" link should be hidden (TRUE) or displayed (FALSE). The default is FALSE, which shows the link text.
Source

Thumbnail to external link AND Thumbnail to post

Here is what I'd like to accomplish
Goal A: I want to hyperlink each thumbnail in index.php to their post.
Goal B: I want to define a hyperlink for each thumbnail in single.php to an external website.
You may ask why am I using thumbnails for single.php? The reason is because I want this layout:
And so far I understand that there are 3 methods to display images:
Insert image into the editor area along with the text, but the problem is I cannot float the image and text differently because all items within a post are assigned a p tag - am I wrong?
Custom fields should get the job done but it doesn't seem the most efficient way - or am I wrong?
Post Thumbnails should be the easiest way but see my problem below
I have the code to accomplish Goal A and B but they only work separately.
In other words, "Code 1" does not work if "Code 2" is present.
How can I resolve this issue? Or is there a better method accomplish my goal?
Code 1: Link thumbnails to external websites using custom field (single.php)
<?php $name = get_post_meta($post->ID, 'externalurl', true);
if( $name ) { ?>
<?php the_post_thumbnail(); ?>
<?php } else {
the_post_thumbnail();
} ?>
Code 2: Link thumbnails to the post (functions.php)
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );
function my_post_image_html( $html, $post_id, $post_image_id ) {
$html = '' . $html . '';
return $html;
}
is_single() function will help you achieve what you need. Try below code in functions.php and remove the additional code from single.php
function my_post_image_html( $html, $post_id, $post_image_id ) {
if ( is_single()) {
$name = get_post_meta($post_id, 'externalurl', true);
if( $name ) {
$html = '' . $html . '';
}
return $html;
}
else
{
$html = '' . $html . '';
return $html;
}
}
add_filter( 'post_thumbnail_html', 'my_post_image_html', 10, 3 );

Categories