ACF not showing after running loops - php

In the below code, I call fields from Advanced Custom Fields plugin and only the first two show 'home_title' and 'home_content'. After these two I run two different loops to show the latest posts in a given category. After those loops run there are 4 more fields from ACF called. 'donate_title' , 'donate_content' , 'mission_title' , 'mission_content'. Which are not showing up (not pulling any content at all).
If I move these ACF before running the loops they all show up correctly. So I imagine there is a problem with these following the loops but cannot find the reason.
<div class="main-site">
<div class="home-title-1">
<?php the_field('home_title'); ?>
</div>
<div class="home-content-1">
<?php the_field('home_content'); ?>
</div>
<div class="home-boxes-cont">
<div class="box-left">
<?php
query_posts('cat=4&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<div class="bl-img">
</div>
<div class="bl-title">
<?php the_title(); ?>
</div>
<div class="bl-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<div class="box-middle">
<?php
query_posts('cat=5&posts_per_page=1');
while (have_posts()) : the_post(); ?>
<div class="bm-img">
</div>
<div class="bm-title">
<?php the_title(); ?>
</div>
<div class="bm-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<div class="box-right">
<div class="br-img">
</div>
<div class="br-title">
<?php the_field('donate_title'); ?>
</div>
<div class="br-content">
<?php the_field('donate_content'); ?>
</div>
</div>
</div>
<div class="mission-title">
<?php the_field('mission_title'); ?>
</div>
<div class="mission-content">
<?php the_field("mission_content"); ?>
</div>

In order to get custom field data from the original post after altering the global post data with your query_posts() calls, you need to reset your post data with the wp_reset_query() function. Place this function after each loop -
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; wp_reset_query(); ?>

You are altering the global wp_query variable. When you do that and the the result is not is_single() then you cannot pull any ACF settings any longer.
Either reset the wp_query to its original setting for the page or store the vars in an array before you make any wp_query changes, and retrieve them as needed later in the code.

While I agree with Scriptonomy, you should really just use get_posts(). This is exactly what this function is designed to do... custom loops outside the main loop. You should rarely ever need to modify the global wp_query variable.
If you still want to use the_permalink() and the_title() without passing a post id, then scroll down the page to the section labeled "Access all post data" and you'll see how to use setup_postdata() to make it easier.

Related

show custom data in Wordpress posts loop

I need to display some custom data in my category posts loop. I mean I want to create special div on my post template and I want to show data from this div in this posts loop. Can anyone help me? Thank you
<?php
if ( have_posts() ) :
query_posts('cat=7');
while (have_posts()) : the_post(); ?>
<div class = "item">
<div class="item_image"><?php the_post_thumbnail(); ?></div>
<div class = "item_title"><?php the_title(); ?></div>
<div class = "item_excerpt"><?php the_excerpt(10); ?></div>
<!-- here I want to display data from each post -->
<div class = "my_custom_data">custom data</div>
Show more...
</div>
<?php endwhile;
endif;
wp_reset_query();
?>
ACF has two powerful functions get_field() and the_field(). To retrieve a field value as a variable, use the get_field() function. This is the most versatile function which will always return a value for any type of field.
To display a field, use the the_field() in a similar fashion.
Now you need to get the name of the field e.g if it is 'custom_title'
<?php
if ( have_posts() ) :
query_posts('cat=7');
while (have_posts()) : the_post(); ?>
<div class = "item">
<div class="item_image"><?php the_post_thumbnail(); ?></div>
<div class = "item_title"><?php the_title(); ?></div>
<div class = "item_excerpt"><?php the_excerpt(10); ?></div>
<!-- here I want to display data from each post -->
<div class = "my_custom_data"><?php the_field('custom_title'); ?></div>
Show more...
</div>
<?php endwhile;
endif;
wp_reset_query();
?>

Wordpress: Retrieve post views in loop?

I have a loop which displays blog post Titles, Excerpts and a Read More link.
I want to also include the amount of views each blog post has in the loop.
I have tried using various view count plugins but none seem to work within a loop.
Would anyone know if there is a way to, say, retrieve this info directly from the database, pairing views with post IDs?
My loop is:
<div id="bloglist">
<?php query_posts('category_name=blog&showposts=6'); while (have_posts()) : the_post(); ?>
<div class="panel">
<div class="panel-wrapper">
<div class="blogstats">
<div>ID: <?php $postid = the_ID(); echo $postid; ?></div>
<div>Views: <?php echo_post_views($postid); ?> </div>
</div><!--BLOGSTATS--->
<div class="excerpt"><?php the_excerpt(); ?></div>
<a class="readmore" href="<?php the_permalink(); ?>">Read More</a>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div><!--BLOGLIST-->
I am currently unsuccessfully using the WP-post-view plugin
I suggest using this plugin
WP-PostViews
i attached a picture for change plugin setting
image WP-PostViews setting
good luck.

Wordpress: Echo PHP depending on page ID

Good afternoon, I'm trying to display posts using PHP in the template, but I'm using the same template to output different posts depending on the age ID.
I currently have this code which works...
<?php //GET MEMBERS ?>
<?php query_posts('category_name=members&orderby=date'); ?>
<div class="row-fluid">
<ul class="thumbnails">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="span4">
<div class="thumbnail">
<?php // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
?>
<div class="pad">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
But I need to output a different "category" depending on page ID... E.G
if page id is 7 echo "php script"
else page id is 13 echo "different php script"
Thanks, Brad
Wordpress has some built in functions. You can use get_the_ID() to return the ID number to use in your if statements.
Regards

Continuing a WordPress PHP Loop

I'm currently trying to develop a custom Wordpress theme, and on my homepage I need to add a second content block. I am using a plugin to do this, which simply requires me to add the following where I want the content block to be.
<?php the_block('Latest Products')?>
However when I add this it seems to have no effect which I believe is due to the formatting of my php. I'm fairly new to php, so any help is greatly appreciated.
My code is as follows - I've cut out the best part of the HTML. I think it's something to do with that 'endforeach' tag?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><?php the_title(); ?></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php the_block('Latest Products')?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
EDIT
Okay, so apparently it needs to be put outside the loop, however it still won't work. Any ideas?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><?php the_title(); ?></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php endwhile; endif; ?>
<?php the_block('Latest Products')?>
<?php get_footer(); ?>
This mostly depends on what the plugin is actually doing because your code syntax is correct.
If you are using the Multiple Content Blocks plugin and are using the latest Wordpress version 3.5.1 then I believe the plugin may not be compatible. I'd check the version compatibility of the plugin to your Wordpress install as this could be your issue.
EDIT:
The plugin works by applying a filter to the function the_content() so that is why it only works by declaring the_block() before the_content() function is called.
A solution could be to capture the output the_block() and use print it out later, as an example:
<?php
ob_start();
the_block('Latest Products');
$latest_products_contents = ob_get_contents();
ob_end_clean();
?>
<!-- Further down.. -->
<?php echo $latest_products_contents; ?>

Displaying threaded comments without wp_list_comments()

I am creating a custom comment layout, therefor I'm not using wp_list_comments(). My problem is that I can't seem to figure out for to effectively display replied comments in one another (threaded).
So far I have it so it only show the top most layer of comments.
<ol class="commentlist">
<?php foreach ($comments as $comment) : ?>
<?php if($comment->comment_parent == 0): ?>
<li class="comment">
<div class="main">
<div class="name">
<?php if(get_comment_author_url()): ?>
<?php comment_author(); ?>
<?php else: ?>
<?php comment_author(); ?>
<?php endif; ?>
</div>
<div class="text"><?php comment_text(); ?></div>
</div>
<div class="info">
<?= get_avatar($comment, $size = '90'); ?>
<div class="month"><?= comment_date('M'); ?></div>
<div class="day"><?= comment_date('dS'); ?></div>
<div class="year"><?= comment_date('Y'); ?></div>
</div>
<div class="clear"></div>
</li>
<?php endif; ?>
<?php endforeach; ?>
</ol>
I know want to display the comments that are replies within this comment.
I have seen your code, i think there is restriction for the child comments. you had condition where parent comments will displaying only.
You need to create one more class OR CSS for child comments and for that also need to run loop inside this FOREACH loop and displaying only that childes where Parent would be the
$comment->comment_parent
.
I think i am right if i get your prob.
Thanks.
I have figured out how to achieve threaded comments while using a custom layout.
Inside of your functions.php file place the following code:
<?php function comment_layout($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
// Your custom layout here
<?php } ?>
Now, inside your comments.php file simply places the following where you would like the comments to appear.
<?php wp_list_comments( array( 'callback' => 'comment_layout' ) ); ?>
What will this do is, loop through all the comments (in the correct order) and call your custom layout to display the comment.

Categories