I have the following loop that utilises Advanced custom fields (ACF) - Everything displays except the content. I have tried various solutions with no success.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'sectors',
'orderby' => 'menu_order',
'order' => 'ASC'
));
global $post;
if($posts) {
?>
<?php
foreach($posts as $post) {
?>
<div class="content full sector">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
</div>
<?php
}
?>
<?php
}
wp_reset_postdata();
?>
Any help is greatly appreciated.
Thanks
When you use get_posts() to return post data, some post-related data is not available by default. One of these items is the_content(). This is resolved by calling an internal function setup_postdata(), with the $post array as its argument.
Solution: Add setup_postdata( $post ); within your foreach().
Read more in the Codex.
Related
I'm trying to use get_field to return a simple text field and it returns empty for some reason. A field itself is where it should be and there is text in it, so that part is all set. This PHP code is loaded via php snippet, post thumbnail for example, shows up perfectly. So everything works except for ACF field value.
<div class="your-class">
<?php
$args = array(
'post_type' => 'home_test',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query($args);
$brand = get_posts($args);
foreach ($brand as $post) {
setup_postdata($post);
$thumbnail = get_the_post_thumbnail_url($post->ID, 'full');
$homelinkvalue = get_field("home_brand_link");
if (!$thumbnail)
continue;
?>
<div>
<p><?php echo $homelinkvalue; ?></p><img src="<?php echo $thumbnail; ?>">
</div>
<?php
}
wp_reset_postdata();
?>
</div>
I think the issue is that you are mixing together a custom post loop (your foreach and setup_postdata()) but then are using functions like get_field(), which make use of the global post object. In this case, get_field() tries to lookup the field value by checking against the global $post, but it has not been properly set. See warning here about setup_postdata($post):
You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly.
You can implement this in your code with a slight modification:
global $post;
foreach ($brand as $currPost) {
$post = $currPost;
setup_postdata($post);
// Rest of code as normal
}
Or, since get_field() can accept a specific post as an argument instead of automatically using the global one, you could change:
$homelinkvalue = get_field("home_brand_link");
to:
$homelinkvalue = get_field("home_brand_link",$post->ID);
Side note: Normally, the recommended way to iterate posts is with the special "WP loop" pattern, something like:
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Do something -->
<?php endwhile; ?>
Using the above pattern automatically sets the global $post variable as it loops through, which allows devs to use functions like get_field() without having to worry about explicitly passing in a specific post; makes things a bit easier.
Try this one:
<div class="your-class">
<?php
$args = array(
'post_type' => 'home_test',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts) :
while($the_query->have_posts) : $the_query->the_post();
?>
<div>
<p><?php the_field( "home_brand_link" ); ?></p>
<img src="<?php the_post_thumbnail_url(); ?>">
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</div>
I use WP-PostRatings WP-PostViews plugins and was able to display pages using functions
<?php if (function_exists('get_highest_rated')): ?>
<ul><?php get_lowest_rated(); ?></ul>
<?php endif; ?>
<?php if (function_exists('get_most_viewed')): ?>
<ul><?php get_most_viewed(); ?></ul>
<?php endif; ?>
but they are displayed as plain text and I cannot edit them, add a thumbnail of the post and do other things is it possible to not display them as if using get_posts() like this
<?php
global $post;
$args = array(
'post_type' => 'post',
'orderby' => 'comment_count',
'order'=> 'ASC'
);
$myposts = get_posts( $args );
foreach( $myposts as $post ){ setup_postdata($post);?>
<li><?php the_title(); ?></li>
<?php } wp_reset_postdata(); ?>
but only for rating and views
Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.
Below is my example code that is dynamically get the posts.
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
The code above will works correctly but my question is, since this is not a default blog page or a category, how can I use the posts_nav_link() so that I can still access the rest of the pages? I tried to used it but it doesn't work unless if the current page is a category. Hope you guys can help me this.
If you giving you paging in your custom post type. then i think you can do very simple you have to use wordpress plugin like wp-pagenavi after then add your custom post type in this plugin in admin panel after then add
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
<?php
global $post;
$args = array( 'numberposts' => 4 );
$the_posts = get_posts( $args );
foreach( $the_posts as $post ){ ?>
//The Post Content Goes here...
?>
<div class="pagination">
<?php wp_pagenavi(); ?>
</div>
Without plugin you can use like this
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 3, 'cat' => '-10, -72&paged=' . $paged) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail(); ?> <?php the_title(); ?>
<span><?php the_time('d.m.y') ?></span>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php posts_nav_link(' — ', __('« Previous Page'), __('Next Page »')); ?>
I have a custom post type named "audio". To display all the posts in a page I wrote the code below, but it does not work.
<?php
$post_type = "audioes";//post type names
echo "djnbfj";
$post_type->the_post();
// The Query
$the_query = new WP_Query( array(
'post_type' => $post_type,
'orderby' => 'post_date',
'order' => 'DESC',
'showposts' => 1,
));
// The Loop
?>
<?php
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="issue-content">
<?php get_template_part('includes/breadcrumbs', 'page'); ?>
<?php if ( has_post_thumbnail() ) { the_post_thumbnail();}
get_template_part('loop-audio', 'page');
?>
</div><!--issue-content-->
<?php endwhile; ?>
</div> <!-- end #left_area -->
How can i got the posts type i want the custom one written above.
Is the post type called "audio" or "audioes"? Your $post_type variable is set to "audioes".
I'm not sure what the get_template_part lines or the echo line is doing, but the below code should display your post type. You also have the post type name as "audioes" in your code, but you state that the name is "audio". Try the code below, but if the post type is named "audioes" then you'll need to change that in my code below. You can wrap additional divs, spans, or other HTML tags around <?php the_post_thumbnail(); ?> and <?php the_content(); ?> to give them CSS styles is you wish to.
<?php query_posts(array('post_type' => 'audio', 'posts_per_page' => 1, 'orderby' => 'post_date', 'order' => 'DESC')); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="issue-content">
<?php the_post_thumbnail(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; wp_reset_query(); ?>