Wordpress : display ACF checkbox label on front end - wordpress

I have created chekboxes with ACF, now I can display the values ​​of the checkboxes on my front end but I would like to show the associated labels to.
Here is my code :
echo "<ul>";
$profession = get_field('profession');
foreach($profession as $key => $check){
echo "<li>".$check."</li>";
};
echo "</ul>";
Thanks a lot !

try this: http://www.advancedcustomfields.com/resources/functions/get_field_object/
You can return all the field's metadata with get_field_object().

if( get_field('field_name') ):
echo '<div class="class_for_display">';
echo 'Label:'; echo'<p>' .the_field('field_name').'</p>';
echo '</div>';
endif;
i use this code in function.php of child theme

Related

How to get parent page id and title of current page in wordpress?

Here is the my code, which is not working.
global $post;
echo get_the_title( wp_get_post_parent_id( $post->post->ID ) );
but this is not working.
thank you in advance.
For parent page id
$post->post_parent;
For current page title
$post->post_title;
For parent page id
<?php
echo wp_get_post_parent_id(get_the_ID());
?>
In Gutenberg:
wp.data.select('core/editor').getEditedPostAttribute('parent')
Hope will be helpful to someone
If you want i.e: create a link to the post parent:
<a href="<?= get_permalink($post->post_parent) ?>">
<?= get_the_title($post->post_parent) ?>
</a>
→ <?= the_title() ?>
which will result in i.e:
Latest News → Some news title
For Astra theme and for page template look.php I did this:
$post->post_parent; will nbot work cause in my case function is out of the loop. I run it via functions.php. $post->post_parent works perfectly when inserting it in page template, but not when editing theme function ;)
function add_script_before_header() {
$current = $post->ID;
$parent = $post->post_parent;
$grandparent_get = get_post($parent);
$grandparent = $grandparent_get->post_parent;
if ($root_parent = get_the_title($grandparent) !== $root_parent = get_the_title($current)) {
echo get_the_title($grandparent);
}
$after = $parent;
if ( is_page_template( 'look.php' ) ) {
echo $after . ' - ';
}
}

Wordpress grab page attributes

Is there a Wordpress function for grabbing the page attributes? I need to be able to check which templates are being used on which pages.
I have tried the get_post and get_pages but neither one outputs the page attributes.
Thanks in advance!
solution:
$ids= get_all_page_ids();
foreach ($ids as $id){
$meta = get_metadata('post', $id);
//var_dump($meta);
$template = $meta['_wp_page_template'][0];
echo $template;
echo "<br>";
}
Try using get_all_metadata. That will fetch all the meta records for a given object.
<?php
$post_id = 123;
$meta = get_metadata('post', $post_id);
echo $meta['my_custom_field_key'];
The docs are a good place to look: Function Reference « WordPress Codex
i.e.: Function Reference/get page template which
Displays the filename of the page template used to render a Page
(printed within an HTML comment, in this example) :
<?php echo '<!-- ' . basename( get_page_template() ) . ' -->'; ?>
And,
global $wp_query;
$template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
will give you the template file name. Use str_replace() to strip the .php from the end.
`

how to determine is_home in wordpress if query a category?

I need to query a category in home page. in index.php file I used this script
$all_featured_posts = query_posts(array('category_name'=>'featured-programs'));
Then in the header.php file I need to change the title
<title>
<?php
if ( is_home() ) {
echo 'My site name' ;
} elseif (is_404()) {
echo '404 Not Found';
} elseif (is_category()) {
echo ' Category' . wp_title('',0).' | My site name' ;
}
?>
The problem is when I query a category in the index file then the is_home return false ( Tried with is_front_page() also ) Then it alway show the title with the name of the category which I query.
How I can fix it? Thanks you!
I might be wrong, but I think because you use query_posts(), all your is_* functions change their values. And, well, because you do query a category, is_home() should return false.
What you can do to solve it, is use new WP_Query(), and get all the posts from it. This way, you will not be affecting the original WP_Query, and thus the is_* functions.
The code should look like this:
$query = new WP_Query('category_name=featured-programs');
while ( $query->have_posts() ) : $query->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();

Exclude the_post_thumbnail from gallery shortcode

I am using this code to have a simple gallery on the page:
<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?>
The problem now is that the end-user has to upload an image via the Media page before selecting this image as featured image.
I know this could be solved by adding the featured image's ID to the shortcode's exclude list, but how to get this ID automatically?
function exclude_thumbnail_from_gallery($null, $attr)
{
if (!$thumbnail_ID = get_post_thumbnail_id())
return $null; // no point carrying on if no thumbnail ID
// temporarily remove the filter, otherwise endless loop!
remove_filter('post_gallery', 'exclude_thumbnail_from_gallery');
// pop in our excluded thumbnail
if (!isset($attr['exclude']) || empty($attr['exclude']))
$attr['exclude'] = array($thumbnail_ID);
elseif (is_array($attr['exclude']))
$attr['exclude'][] = $thumbnail_ID;
// now manually invoke the shortcode handler
$gallery = gallery_shortcode($attr);
// add the filter back
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
// return output to the calling instance of gallery_shortcode()
return $gallery;
}
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
<?php $id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID ?>
<?php echo do_shortcode('[gallery exclude='.$id.' link="file" itemtag="div" icontag="span" captiontag="p" size="thumbnail" columns="4" ]'); ?>
How about?
echo do_shortcode('[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"]');

How to get the category title in a post in Wordpress?

Say I have a post called Hello World in Wordpress and I'm directly viewing this page, how would I go about finding the category of "Hello World" and displaying it?
Use get_the_category() like this:
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
It returns a list because a post can have more than one category.
The documentation also explains how to do this from outside the loop.
You can use
<?php the_category(', '); ?>
which would output them in a comma separated list.
You can also do the same for tags as well:
<?php the_tags('<em>:</em>', ', ', ''); ?>
To find the category id, category link and category name in wordpress using php, you can use below code:
$cat_id = get_cat_ID('Category Name');
$category_link = get_category_link($cat_id);
$category_name = get_cat_name($cat_id);
echo $category_name; // It will print your category name

Resources