Why is the_post_thumbnail always getting called? - wordpress

I'm developing a wordpress theme, and in my header.php I believe I have valid code, but for some reason, the thumbnail image is always shown in the code below.
The logic I'm trying to achieve is:
if this is the homepage, and the s3slider plugin has been installed
show the slider
else if this page has a featured image (the_post_thumbnail)
show the thumbnail
else
show a default image
My code block is:
if (is_front_page() && function_exists(s3slider_show()) ) {
//we're on the homepage, and s3Slider exists
s3slider_show();
} elseif ( has_post_thumbnail() ) {
//there is a featured image / thumbnail
the_post_thumbnail();
} else {
// the current post lacks a thumbnail
?><img alt="alt text" src="image.jpg" />
<?php
}
I can't for the life of me work it out, even though on the homepage the slider is shown, so is the_post_thumbnail() output.
Is it too late, and I've forgotten something fundamental?
I don't see why the_post_thumbnail will even be executed on the homepage if I've already entered the first if for the home / s3Slider combination.

function_exists() expects a string:
function_exists( 's3slider_show' )
Since your function s3slider_show doesn't return a string the first condition evaluates always to false.

Related

Wordpress Images Not Showing - URL for image shows as numbers

Our Wordpress site suddenly stopped showing all 'featured' images. They're just blank. When we look at the source code it shows url(896). We have tried selecting the image again (which shows fine in the backend) but on the frontend it's still blank with an invalid URL. Any idea how to fix that and what may have happened? TIA!
To update: this below code is what's being used to pull the featured images (with the incorrect URL):
<div class="hero homepage relative-block" style="background-image:url(<?php the_field('hero_background_image'); ?>);<?php $hero_aspect_ratio = get_field('hero_aspect_ratio'); if( $hero_aspect_ratio ) { echo "padding-top:".$hero_aspect_ratio."%;"; } ?>"></div>
EDIT:-
The following also isn't grabbing any content. Any ideas?
<div class="page-content">
<?php get_template_part('content-block-loop'); ?>
</div>
Your field hero_background_image returns attachment ID instead of the URL of the image. Use wp_get_attachment_url($id) with get_field() that returns the id, instead of the_field() that outputs the value to get the URL of the attachment/image:
<div class="hero homepage relative-block" style="background-image:url(<?php echo wp_get_attachment_url(get_field('hero_background_image')); ?>);<?php $hero_aspect_ratio = get_field('hero_aspect_ratio'); if( $hero_aspect_ratio ) { echo "padding-top:".$hero_aspect_ratio."%;"; } ?>"></div>
Possible cause: if you use some metabox/custom fields plugin as MetaBox, Toolset Types or the ACF, someone has probably changed the settings for the field, to return the ID of the attachment instead of the URL. So that might also solve the issue.

How to Change logo on the specific page of website in wordpress?

I m working on a website in wordpress.
I searched for a problem that i want a different logo in one page of my site using any plugin but i didn't find a satisfactory solution.
Kindly someone tell me how can i do this that main logo should be appear on the whole site but secondary logo should appear on one page of my site
if you know wordpress developement, you can use conditional tags. ref
You can use the is_page() method to check which page you are in and change the logo accordingly.
So, in the header or where ever your logo is displayed, you will do it like this:
<?php
//Returns true when the Pages displayed is either post ID = 42, or post_name is "about-me", or post_title is "About Me And Joe".
if(is_page( array( 42, 'about-me', 'About Me And Joe' ) ) )
{
//display the secondary logo
}
else
{
//display the main logo
}
Of course I'm assuming here that the page you want it to be different is a WordPress page
If it's a listing page or a single post page then you can use different methods of checking. All of which are described here.
Please insert this code to your header.php file.
$ss = $wp_query->post->post_title;
if($ss == 'your page name'){
echo '<img src="/your-new-logo.png" />';
}
else{
echo '<img src="/your-current-logo.png" />';
}

How do I exclude some code on posts and pages in wordpress?

I have a feature box on my header.php file, because of this the feature box gets displayed in every page and post page as well. What code do I need to keep this feature box only in the homepage and no place else?
You're looking for the is_front_page() function:
if ( is_front_page() ) {
// Show the feature box
} else {
// Do something else
}

WordPress Search goes to post page when you do a blank search

Yea so on my wordpress theme I've put together...
When you click the search button (with the search field empty) it takes you to the blog page. I have assigned the index.php as the blog page and made another page and assigned that as the home page.
Can anyone help? Its not a major problem but it is a little glitch I would like to get rid of.
Thanks.
Terry
I also faced the same problem, it is default given by wordpress.
but luckily I found something which helped me.
Add below in "Functions.php"
function SearchFilter($query) {
// If 's' request variable is set but empty
if (isset($_GET['s']) && empty($_GET['s']) && $query->is_main_query()){
$query->is_search = true;
$query->is_home = false;
}
return $query;}
add_filter('pre_get_posts','SearchFilter');
and then replace below line(line no 15) in search.php
<?php if ( have_posts() && strlen( trim(get_search_query()) ) != 0 ) : ?>
Might be it will help you too
For details read this : Customize empty search wordpress

targeting title in wordpress post

I am working on a wordpress plugin that modifies the title of a post. I only want to do this when I am viewing a single post. To be specific, I want to add a link beside the title, but for purposes of the question, I will be adding some arbitary text.
I started out by using the 'the_title' filter hook, and calling this function.
function add_button_to_title($title)
{
global $post;
if(is_single())
{
return $title.'googly googly';
}
return $title;
}
The problem is, the links on the side bar apparently also use 'the_title', as I saw my text showing up in the side bars as well, which led me to:
if(is_single() && in_the_loop())
But then, in my theme(and i suppose themes in general) there is a link to the previous post and next post, which also uses 'the title' filter. So finally I have:
if(is_single() && in_the_loop() && ($post->post_title == $title))
The last conditional basically makes sure that it is the title of the post that is being printed, not the title of the next or previous post. This works but I am not sure how well it will work given different themes...It seems like terribly hacked together. Any advice from wordpress gurus out there? I am worried that the title would be modified for other reasons and the conditional will fail.
Any help is appreciated!
Ying,
There isn't really a good solution except, as ShaderOp said, requiring theme modification. Your solution will work for the most part. The only exception is if the theme developer has changed the query in a page. I'd say this is probably a good enough solution that it'll cover more than 95% of the cases you'd run into.
I solved a similar issue by adding a check to see if the title being filtered matches the title of the post. This avoids the issue with other post titles on the page (in sidebar, menu) also getting filtered.
function add_button_to_title( $title ) {
global $post;
if( is_single() && $title == $post->post_title ) {
return $title . 'googly googly';
} else {
return $title;
}
}
Wouldn't it be easier to keep the original version of your add_button_to_title function, but instead of hooking it to a filter, call it directly from your single.php page in the appropriate place?
For example, somewhere in your theme's single.php, instead of this:
<h3 class="storytitle">
<?php the_title(); ?>
</h3>
Use this:
<h3 class="storytitle">
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php echo add_button_to_title(the_title('', '', false); ?>
</a>
</h3>
today I ran into a similar problem. the_title gets called several times accross the whole page (e.g., in the html-head, the menus, the sidebar). I followed a similar approach using conditionals and the post/page id.
Additionally, I added a boolean flag which is set to true using the 'the_content' filter. So the title gets changed until the content is displayed. This way, I ensure that sidebars/widgets are not affected (e.g. Thematic theme has a default widget with links to pages - here the other conditionals would not be helpful as get_the_id() would return an equivalent). This ONLY works if the theme uses sidebars on the right. I did not find a way yet how to hook in directly before the 'the_title' call for the page/post to enable the boolean flag.
function myplugin_adjust_title($title, $id) {
global $myplugin_title_changed;
if ($myplugin_title_changed) {
return $title;
}
if (in_the_loop() && is_page('myplugin') && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'myplugin_adjust_title', 10, 2);
function myplugin_adjust_title_helper_content($content) {
global $myplugin_title_changed;
$myplugin_title_changed = true;
return $content;
}
add_filter('the_content', 'myplugin_adjust_title_helper_content');

Resources