Advanced Custom Field not showing in template - wordpress

I'm racking my brain trying to think why this isn't working. Seems to be right to me but I must be missing something.
<?php
if( have_rows('cta_box') ):
while( have_rows('cta_box') ) : the_row();
echo get_sub_field('cta_header');
echo get_sub_field('cta_content');
endwhile;
endif;
?>
Am I doing something dumb?
Thanks

It's a group field, not repeater. This'd help.
So in your case you'd use it like this.
$cta = get_field('cta_box');
echo $cta['cta_header'];
echo $cta ['cta_content];

your code is proper, i think the issue is where you want to display like in the specific template or any where in the page, you just need to assign with the template or page or post whatever its will display.

Here is the code that got it working. Can someone explain? Is the code above this ACF effecting what is returned?
<?php while ( have_posts() ) : the_post();
$cta = get_field('state_call_to_action');
echo $cta['call_to_action_heading'];
echo $cta['call_to_action_body'];
endwhile; ?>

Related

How to print a wordpress query?

I have a while loop in my wordpress site. How to print its query details ?
while (have_posts()) : the_post();
I tried, but not working:
print_r($GLOBALS['wp_query']->request);
Add this in your functions.php then add ?debug=sql after the url, for example
http://someUrl.com/something?debug=sql
Or
http://localhost/localCopy?debug=sql
Also <?php echo $GLOBALS['wp_query']->request; ?> should work, just put it right after the loop if you didn't it before.

Wordpress Shortcode in custom field within custom post type

Hi Guys Need help with this very badly.
Need to add shortcode to output in the area circled in white in the picture below.
And the input area is under video description. And from my understanding ive have confirmed that the name for that text area is description_value.
I have looked through every documentation and tried all filters and do_shortcode variations to no avail. Please help i have spent 3 days non stop doing this. Puting the codes in my function.php and so many others. It still does not parse [shortcodes] it just displays text "[shortcodes]". please refer to picture below
Thank you.
This is outputing on the page. I have
<div class="describe-feat">[postexpirator]</div>
This is in a file called grid-gallery.js
<h2><%= item.title %></h2></div><div class="view-gallery">\
<div class="describe-feat"><%=item.desc%></div>\
<% if(item.imgnum){ %><span class="item-num"><%= item.imgnum %></span><% } %>\
This is in custom post editor in wordpress admin area
<textarea name="description_value" class="option-textarea">[postexpirator]</textarea>
https://www.dropbox.com/s/almn09e1dwmeywb/shortcodxe.jpg
It's because, you are missing do_shortcode function for parsing shortcode.
Assuming you just want to target a single value, you could just do this inside the loop.
<?php echo ( do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , true ) ) ); ?>
If your post has multiple values for that custom field, then you can set the above to false.. and loop over the array...
<?php $values = do_shortcode( get_post_meta( $post->ID , 'Your textarea Key Name' , false ) ); ?>
<?php if($values && is_array($values)) : ?>
<?php foreach( $values as $meta) : ?>
<p><?php echo $meta ?></p>
<?php endforeach; ?>
<?php endif; ?>
Just want to add that if you won't use the_post() no shortcode will work,
I had this issue when trying to enable shortcode on CutomFields on a new page type, and nothing worked until activating WP loop with the_post() .

Display specific post format with limit number of post

I create a loop in Wordpress with the condition like this:
Display a specific post format (example: video)
Limiting the post number. In this case, I only want to display 2 posts.
Here my code:
<?php $i = 1; if (have_posts()) : while (have_posts() && $i < 3) : the_post(); ?>
<?php get_template_part( 'content-video', get_post_format() ); ?>
<?php $i++; endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
I already have file name content-video.php.
Unfortunately, the loop does not work. Only displayed the first post, not a specific post (video post format) with template from content-video.php.
Appreciate for any help, or alternative code. Thanks in advance.
My remarks about your code:
I think you have bad wrapping for if else while statements. You are missing an endif at the end in the case your approach is correct.
Why use inside your code the i variable since you can customize any query in WP, especially for number of post using the parameter posts_per_page.
Why not to use the loop inside content-video.php and write only:get_template_part('content-video', get_post_format()); Like in single.php and loop-single.php the themes provided with wordpress installation (twenty themes) .
Good luck
the best way would be to get posts of "video" post format from a query, but I'm not sure how:)
As a workaround you could do this inside the loop:
if (!has_post_format('video'))
continue;

Wordpress search field affecting custom query

Just came across an issue with my Wordpress template that I can't figure out. I am using a custom built menu (done simply with a quick query_posts() call), but when searching for certain terms, my query is affected. Not a clue as to why.
Here's my menu code:
<?php $main_cats=explode(",",$options['main_cats']); ?>
<?php $myargs = array('post_type' => 'page', 'post__in'=>$main_cats,'order'=>'ASC'); ?>
<?php query_posts($myargs);
while ( have_posts() ) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_query();?>
This should end up with 4 menu items. However, when searching for a few "job" related items, it seems that the Job Manager plugin steps in (http://pento.net/projects/wordpress-job-manager-plugin/) and I get one menu result stating "This job doesn't exist". However, I don't understand how that plugin could possible affect my query.
Any thoughts?
This code assume that $main_cats has the right data, before testing this, check that the variable is correct:
<?php
$main_cats=explode(",",$options['main_cats']);
$menu_items=get_posts(array('post_type' => 'page', 'post__in'=>$main_cats,'order'=>'ASC'));
foreach($menu_items as $menu_item){ ?>
<li><?php echo $menu_item->post_title; ?></li>
<?php } ?>
Maybe Use native menu function to to create menu's, http://codex.wordpress.org/Function_Reference/wp_nav_menu
Don't use query_posts($myargs), query_posts() is meant for altering the main loop.
Use WP QUERY http://codex.wordpress.org/Class_Reference/WP_Query (or get_posts).
Try wp_reset_query(); before your query as well as after, like you have. I think that the plugin query is happening before your yours. There may be some carry over from that. Reseting before your query might give you a clean slate.

Displaying code snippets from WordPress custom fields

I'm trying to display code snippets entered into my custom field. An example code snippet entered into custom field - snippet-1
<?php
if (($wp_query->current_post + 1) < ($wp_query->post_count)) {
echo '<div class="post-item-divider">Post Divider</div>';
}
?>
If I try to display this code wrapped in <pre></pre> tags in my page template like
<?php if ( get_post_meta($post->ID, 'snippet-1', true) ) : ?>
<pre><?php echo get_post_meta($post->ID, 'snippet-1', true) ?></pre>
<?php endif; ?>
but it returns nothing to the template. I understand WordPress is filtering the snippet out as it sees as PHP code to execute. Is there a way just to print this out on the page as a code snippet?
Many thanks in advance
rob
Use htmlspecialchars() to escape your code.
Update
echo htmlspecialchars(get_post_meta($post->ID, 'snippet-1', true));

Resources