I have two pages created Home and About page.
I have set a featured image in my home page.
I want to get the image url of whatever featured image is set in my home page and display it on my about page (dynamic).
I am using my own custom template.
If possible I want to achieve it this way:
<img src="<?php get_homepage_featured_image_url(); //Something like this in wordpress. ?>"
You have to use get_option to find the home page.
$home_id = get_option('page_on_front');
$home_thumb_id = get_post_thumbnail_id($home_id);
echo wp_get_attachment_image($home_thumb_id, 'large');
Related
I use ACF-plugin: https://wordpress.org/plugins/advanced-custom-fields/
There is a simple HTML file called sidebar.php.
This sidebar.php file has a place to display the image via ACF:
<figure class="Sidebar_Block">
<img class="ACF_Img" src=" <?php the_field('sidebar-latest') ?> "> // ****** place to display a picture ******
</figure>
In other files (home.php, category.php) i call sidebar.php via command
get_sidebar();
Image display only works on the home page (home.php)
And in the category.php file, displaying the image through ACF does not work.
The question is:
How to connect ACF to category.php and display image via WordPress admin?
The problem is that you saved sidebar-latest field only for your home page. I mean, it's attached to the home page. When you call the_field and you don't pass the page/post ID in the second argument, it will take the current one.
So for the homepage works because the sidebar image is saved for the home_page but not when you change page, in order to make it work, pass the home page's post ID to the second parameter:
<img class="ACF_Img" src="<?php the_field('sidebar-latest', $home_page_id); ?>">
So it will work on all page, also, remember to remove any extra space like my example.
I have cca 4500 posts in my Wordpress backend. Some posts doesn't have featured image. I was wondering is there any solution to bulk add featured image (placeholder if there is no "real" image)? I'm talking about backend, I now that I can do on front end but need way to add featured image in the backend if featured image is empty.
Can anybody help me with this?
You can do this using 3 ways.
Using plugin
https://wordpress.org/plugins/default-featured-image/
Or you can show dummy image to your code (when there is no featured image found)
check below code for front-end
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
<?php } ?>
AJAX in Admin (In custom form) - You can also achieve same using creating one option page in backend where you can give form with one image field, and on save you can update featured image if blank found using ajax.
On my wordpress website in the blog page , Some posts have featured images , On licking on these images they redirect to their path , I want them to redirect to the post link.
How to achieve that ?
In the wordpress "have_posts()" loop after "the_post();" (index.php in theme):
href="<?php the_permalink(); ?>"
In the custom loop:
href="<?php echo esc_url(get_permalink($single_post->ID)); ?>"
where $single_post is an element of posts array
On my Wordpress blog page (home.php) template, I'm trying to display the featured image set for that page at the top, then the content archive grid underneath. However, instead of displaying the featured image I set for the page, the first blog post's featured image is displayed instead. Additionally (related), if I remove the first blog post's featured image, it uses another post image on the content archives page. I'm guessing the two problems are related, but I'm not sure. Any help would be appreciated.
Here's the code I put in my functions.php
//Add featured images
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
global $post;
wp_reset_postdata();
echo the_post_thumbnail($post->id); /*you can use medium, large or a custom size */
}
WordPress ignores anything you set on the "Page for Posts". Instead of treating it like a page in the general sense, it treats it more like an archive of posts and the "page" is merely a URL. That said, you can access the ID of the page and then use that to get the title, featured image, or other custom fields you assign to that page:
add_action( 'genesis_before_content_sidebar_wrap', 'ws_post_image', 8 );
function ws_post_image() {
echo get_the_post_thumbnail( get_option( 'page_for_posts' ), 'large' ); // Or use whatever size you need
}
This says, "Get me the featured image on the page I set as the Blog page in Settings->Reading". The ID of that page is stored in your options table with the name page_for_posts.
I have created a navigation for my WordPress, the navigation gets the title of the pages and display them by title.
Now I want to change the contact title to an image(mail icon of course).
I have add a feature image in the contact page.
I can call the thumbnail with
<?php echo get_the_post_thumbnail(); ?>
The problem is that the thumbnail image only shows when I'm inside the contact page where the thumbnail is added.
I could add the image in every page that I create, but it must be another better way to do it.
This is my navigation code.
<ul id="nav">
<?php wp_list_pages('exclude=19&title_li=', 'sort_column=menu_order'); ?>
<?php echo get_the_post_thumbnail(); ?>
</ul>
As you see I am excluding page 19, so it wont show in the navigation.
But i can call the image with the get_the_post_thumbnail.
After some searching i found out that i wasn't that hard at all.
If i want to use the thumbnail from page 19 I just write
<?php echo get_the_post_thumbnail('19'); ?>