ACF and sidebar(WordPress) - php

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.

Related

Wordpress: Get featured image url of home page in another page

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');

Home.php using for blog content can't retrieve page title - Wordpress

Hi I want to do the blog part in my wordpress theme, I set up using static front page et blog page to display latest post.
So in my home.php I have :
<?php get_header(); ?>
<section class="header--page">
<div class="header--img">
<?php the_post_thumbnail() ?>
</div>
<div class="container">
<h1><?php the_title() ?></h1>
</div>
</section>
<?php get_footer(); ?>
But instead of getting title from page and featured image from page, It display those from the first posts, any idea ?
I try on my page.php and front-page.php, both are returning correct title and image
So things like the Posts page function a little differently than a standard page. Most likely, your the_title() function is returning the title of the first post to be displayed.
What you'll need to do is this instead:
<h1><?php echo get_the_title(get_option('page_for_posts')) ?></h1>
What needs to happen is you need to grab the ID of the Posts page, and use that ID to find its title.
So you'll want to use get_the_title() instead, because the_title() doesn't allow you to pass an ID of a page or post. get_the_title() accepts an ID as a parameter.
The page's ID that you specified as your Posts Page in the WP Settings > Reading can be grabbed by using get_option('page_for_posts').
So putting it all together, the code snippet above will get the title of the Posts Page, and echo it on-screen. (get_the_title() doesn't automatically echo it's returned value like the_title() does, hence the addition of the echo.

How Set Image URL As Featured Image?

I have article with title "POST-TITLE" and I create image title same with post title, "POST-TITLE.jpg".
All image I put in my hosting, like this:
http://www.example.com/img/CATEGORY/POST-TITLE.jpg
In mytheme post-single.php, I have this code:
<img src="http://www.example.com/img/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ''; } ?>/<?php the_title(); ?>.jpg" onError="this.src='http://cerpenkoran.net/img/dot.jpg';" title="Thumb for <?php the_title(); ?>" >
My answer:
What code I add the image url code above, so it as Featured Image?
OR how to combine this fix URL:
http://www.example.com/img/<?php foreach((get_the_category()) as $category) { echo $category->cat_name . ''; } ?>/<?php the_title(); ?>.jpg
with this Featuread Image code:
<?php if (has_post_thumbnail( $post->ID )) : ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo $image[0]; ?>" />
<?php endif; ?>
Are you asking how to add a Featured Image to your WordPress website? If so, then follow the below instructions:
Firstly, you are going to need to add the relevant Theme Support inside your functions.php file. To achieve this, go into your functions.php file and enter the following code:
<?php
function my_theme_name_support(){
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'my_theme_name_support');
?>
Make sure that the above code is placed after the opening <?php tag (at the top of the functions.php file) but before the closing ?> tag (at the bottom of the functions.php file.)
You will see that there are two my_theme_name_support entries. You can change the wording of these to something more appropriate. Just make sure they match.
Now, head into your WordPress Dashboard and go to create a New Page or New Post. You should now be able to see an entry, entitled 'Featured Image'. As you scroll the page, you should see it appear on the right hand side. Simply select the link and add the required image.
This, alone, does not output the Featured Image. To output your Featured Image, you now need to enter the following code into the relevant file(s) on your server:
<?php the_post_thumbnail('thumbnail'); ?>
By following the above steps, you should now be able to dynamically call your Featured Images from your WordPress Dashboard and output them to whichever pages you want.
For more information, on Featured Images, check out this WordPress Codex Article >>>
Answer Extension
If you want the same image to appear for every Blog Post, so that you do not have to manually add a Featured Image to every Blog Post, you could:
Upload the image to WordPress, via their Media Uploader.
Then head to the single.php Blog Post Template File and enter the following code:
<img src="POST-TITLE.jpg" alt="post-title" height="42" width="42">
Be sure to make the following modifications, to the above code:
src: This is the link to the actual image. Therefore, change 'POST-TITLE.jpg' to the name of your desired image. Be sure to include the correct image format (.jpg, .png etc).
alt: This is a brief description of the image. No coding required here. Plain text will do.
height and width: Insert the correct dimensions. The dimensions are in pixels.
Please Note ...
single.php is the default Blog Post Template File. There is a chance that there are additional Blog Post Template files too. If this is the case, you will need to add the code to these other Template Files too.
You may find this WordPress Article, helpful, when working with Template Files.

Manually insert a WordPress plugin inside custom theme's header.php file

All right, so I have an issue I'm trying to work through involving WordPress plugins. I have found a gallery plugin that I like--has pretty much everything I need--but the problem is that placing the shortcode in the content editor causes it to be placed below a banner image that I have (this banner image is stored in my custom theme's header.php file and it has to go there because of the layout of the site I'm working on).
What I want to do is call the plugin inside my header.php before this banner image...but I am having trouble figuring out what I need to do in order to accomplish this. I read about the need to potentially remove the register_activation_hook or activate_pluginname actions when I manually call the plugin in my header.php file...do I need to somehow incorporate these or create functionality similar in my header.php file?
This is what I'm aiming for in my header.php file (I am only looking to load this gallery plugin for the home page, hence the check to see if the page id is the home page id):
<div id="Container_Body" class="clearfix">
<div id="topbox">
<?php
$id = get_the_ID();
$hpid = get_option('page_on_front');
if($id == $hpid)
{
code to display plugin
}
?>
<div class="GalleryPlugin">
<?php code to display gallery plugin ?>
</div>
<div class="giantBanner" style="background: rgb(0, 141, 175);">
<img title="Main-Header-Banner" src="<?php bloginfo('template_directory'); ?>/images/Market-Crowd.jpg" alt="Market Crowd">
</div>
</div>
Check this
<?php
if(is_home() || is_front_page()) // this will check for homepage its a wp check
{
// echo the plugin shortcode here like
echo do_shortcode('[gallery]'); // this will be any shortcode
}
?>

show post thumbnail navigation link in in multiply pages

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'); ?>

Categories