Home > Mobile >  ACF and sidebar(WordPress)
ACF and sidebar(WordPress)

Time:04-05

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 >
<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?

CodePudding user response:

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   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