I have a custom field called category_image which is an image field for categories.
I have another custom field called categories_to_show which is a taxonomy for the homepage to select which categories to show.
The categories_to_show is working fine, but I'm struggling to get category_image. If I print_r it's not in the array.
In my page-home.php file:
$context['categories_to_show'] = get_field('categories_to_show') ?? '';
In my page-home.twig file:
{% for c in categories_to_show %}
{{c.name}}
{{c.category_image}}
{% endfor %}
c.name works, c.category_image does not. But not suprising as it's not in the categories_to_show array:
Array ( [0] => WP_Term Object ( [term_id] => 4 [name] => Technology [slug] => technology [term_group] => 0 [term_taxonomy_id] => 4 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw ) [1] => WP_Term Object ( [term_id] => 1 [name] => Uncategorized [slug] => uncategorized [term_group] => 0 [term_taxonomy_id] => 1 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw ) )
Could anyone point me in the right direction?
It looks like your problem is that you have a WP_Term object, but you’ll want to have a Timber\Term object so that you can access the meta data more easily.
In PHP, you can use Timber::get_terms() to convert an array of term IDs or WP_Term objects into Timber\Term objects.
PHP
$context['categories_to_show'] = get_field('categories_to_show')
? Timber::get_terms( get_field('categories_to_show') )
: [];
Then, if you loop through your categories, you can access all methods that are available for Timber\Term.
You can use c.category_image to access the category_image meta value, but I’d use the meta() function for a more future-proof way.
Twig
{% for c in categories_to_show %}
{{ c.name }}
{{ c.meta('category_image') }}
{% endfor %}
Depending on what the meta value for your image contains, you might also have to convert it to a Timber\Image object first before you can work with it.
{# Full #}
{{ Image(c.meta('category_image')).src }}
{# Large size #}
{{ Image(c.meta('category_image')).src('large') }}
Pro tip – you might save some performance if you choose to return only the image ID for your image field instead of an image array in your ACF field settings.
Of course, if you prefer to work with the image array that ACF returns, that’s fine as well.
Related
I would like in the form builder controller to say this field belongs to this group then in the view/theme organize all of group1 into div1, group2 into div2, etc..
I tried something like this (creating sub forms) but that is not working; I am not able to display the group
$builder->add(
$builder->create('group1', FormType::class, array('inherit_data' => true))
->add('brand', TextType::class, array(
'label' => 'brand',
'invalid_message' => 'Enter a brand',
))
);
twig
{{ form_row(form.group1) }}
In case someone else is looking; my error was that I forgot to loop through
{% for row in form.group1 %}
{{ form_row(row) }}
{% endfor %}
As the title says - i have a single.php template which gets the category of the current post assigned.
$cat_post = get_the_category();
That function returns the category which is assigned to the post. After upgrading from 4.0.24 to 4.9.7 it stopped working. The only thing that comes to my mind is that the function is not supported but i haven't found something about this.
Any ideas?
you can use this
//get category
$category = get_the_category(get_the_ID());
//the above code will return an Array
Array
(
[0] => WP_Term Object
(
[term_id] => 2
[name] => video
[slug] => video
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 1
[filter] => raw
[cat_ID] => 2
[category_count] => 1
[category_description] =>
[cat_name] => video
[category_nicename] => video
[category_parent] => 0
)
)
// you can access this array like this
$catID = $category[0]->term_id;
//for term id
$catName = $category[0]->name;
//for category name
I think you are trying to get a category from the current post which is the custom post type.
As per the documentation of the following function, it doesn't work for the custom post type. You can see it on the link below.
Click here to see the Code Reference
For custom post type, you can use get_the_terms() method. Here is the link of the Code Reference of the function.
This is how I can start describing this, it's a very strange error that maybe something else but this is the first symptom I have.
I have a custom template[taxonomy-location_types.php] that shows, a custom post type [locations] by it's custom taxonomy[location-types].
If you visit the home page you will see a menu editorial, travel, etc.
Now if you look here on this taxonomy page
The menu is gone, there are other queries that aren't firing but I'm focussing on this as a primary issue.
I have generated output from nav-menu-template.php like so:
if ( $menu && !is_wp_error($menu) && !isset($menu_items) )
{
write_log($menu);
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
write_log($menu_items);
}
and I get this:
[31-Aug-2017 15:27:52 UTC] WP_Term Object
(
[term_id] => 2
[name] => Header Menu
[slug] => header-menu
[term_group] => 0
[term_taxonomy_id] => 2
[taxonomy] => nav_menu
[description] =>
[parent] => 0
[count] => 6
[filter] => raw
)
[31-Aug-2017 15:27:52 UTC] Array
(
)
[31-Aug-2017 15:27:52 UTC] WP_Term Object
(
[term_id] => 3
[name] => Footer One
[slug] => footer-one
[term_group] => 0
[term_taxonomy_id] => 3
[taxonomy] => nav_menu
[description] =>
[parent] => 0
[count] => 5
[filter] => raw
)
[31-Aug-2017 15:27:52 UTC] Array
(
)
So it sees the menu but for some reason return no items in either menu.
I have turned off all plugins and still get the same result, I have also eliminated all other queries on this page to see if there was conflict and this still this happens.
Any insight on this one would be great not sure where to look next for issues.
Chalk another one up for RTFM, I had a pre_get_posts hook altering the query and I didn't ensure that the query it was altering was just the $query->is_main_query() because strangely enough this alteration was getting into the nav_menu query.
query monitor is now my new best friend.
I have overridden the Sensio Generator Bundle for CRUD in order to better suit my needs.
What I would like to do is to be able to loop through the entity fields.
It is done by default in show.html.twig but not in new and edit views.
When I implement the same logic in new.html.twig.twig it doesn't work though it does for edit.html.twig.twig.
{#app/Resources/SensioGeneratorBundle/skeleton/crud/views/new.html.twig.twig#}
{% for field, metadata in fields %}
{% if field not in 'id' %}
{{ '{{ form_row(edit_form.' ~ field ~ ')}}' }}
{% endif %}
{% endfor %}
When running the generator, the error is: Variable "fields" does not exist in "crud/views/new.html.twig.twig" at line 9
Ok, in fact it is an issue in Sensio Generator Bundle.
In the file: sensio\generator-bundle\Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator.php the generateNewView function is missing a paramter. It is not passing the fields as opposed to generateShowView.
Here is the comparison:
protected function generateNewView($dir)
{
$this->renderFile('crud/views/new.html.twig.twig', $dir.'/new.html.twig', array(
'bundle' => $this->bundle->getName(),
'entity' => $this->entity,
'route_prefix' => $this->routePrefix,
'route_name_prefix' => $this->routeNamePrefix,
'actions' => $this->actions,
));
}
versus
protected function generateShowView($dir)
{
$this->renderFile('crud/views/show.html.twig.twig', $dir.'/show.html.twig', array(
'bundle' => $this->bundle->getName(),
'entity' => $this->entity,
'fields' => $this->metadata->fieldMappings,
'actions' => $this->actions,
'route_prefix' => $this->routePrefix,
'route_name_prefix' => $this->routeNamePrefix,
));
}
I'll try to post this as an improvement.
Im new to Symfony / Twig and am having problems passing object values to my twig templates.
Here is some of my controller code that shows the content of the object:
$prevArticles = $section->getArticles();
print_r($prevArticles);
die()
Displays:
Array
(
[0] => Imagine\NewsletterBundle\Entity\Article Object
(
[id:protected] =>
[title:protected] =>
[headline:protected] =>
[link:protected] =>
[image:protected] =>
[excerpt:protected] =>
[check:protected] =>
[attachment:protected] =>
[field1:protected] =>
[field2:protected] =>
[field3:protected] =>
[magazines:protected] =>
[top_logo_advert:protected] => /uploaded_images/cece0b1859ea2b1af95f1f274620ba77.jpg
[top_logo_alt:protected] => Picture of blomange
[top_logo_link:protected] => www.google.com
)
)
So then I pass my object to my twig template like so:
return $this->render('ImagineNewsletterBundle:Section:'.$builder->getTemplate(), array('prevArticles' => $prevArticles));
Then in my twig template I want to display the value of 'top_logo_advert' but its not working:
{% for article in prevArticles %}
{{ article.top_logo_advert }}
{% endfor %}
I get the error message:
Method "top_logo_advert" for object "Imagine\NewsletterBundle\Entity\Article" does not exist in ImagineNewsletterBundle:Section:build_advert.html.twig at line 62
You must access it via :
{{ article.topLogoAdvert }} or {{ article.getTopLogoAdvert() }}
Both solutions works. Next time, just reminder that properties like 'my_property_1' is converted into myProperty1 in the twig engine.