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();
?>
Related
Sup ,I have added 2 posts in BO , and trying to display title of posts with php , but it doesn't work , should i add any any additional function to functions php or not?
Instead of post title it display - "Home" - page title . where i wrong?
Here is some code
<div class="container">
<div class="row"id="blog">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="col-md-5"id="blog-post">
<div class="post-title"><?php echo get_the_title($post_id); ?></div>
<div class="post-txt"><?php echo get_excerpt(); ?></div>
</div>
<?php endwhile;?>
<?php endif; ?>
</div>
</div>
you don't need $post_id in your get_the_title() function because you are in a loop. In which file is this code ? Maybe you are not in a query.
Try this before your if statement and show me what you get :
<?php
$queried_object = get_queried_object();
var_dump($queried_object);
?>
I suggest this code.
if ( have_posts() ) {
// Load posts loop.
while ( have_posts() ) {
the_post();
?>
<div class="post-title"><?php echo get_the_title(); ?></div>
<?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.
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
Hope someone can help me, I've been struggling for days on this trying to find the answer...
Basically, I have a wordpress site that has a slider (not a plug-in just open source code) which is called to using a 'get_template' but it displays the same three posts on every single page. I have different posts in different categories and want the slider to correspond on each separate page and echo the posts from each particular category.
<div id="slidorion">
<div id="slider">
<?php
query_posts( 'posts_per_page=3' );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="slide">"><?php the_post_thumbnail(); ?></div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div id="accordion">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="link-header"><?php the_title(); ?></div>
<div class="link-content">
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
here is a link to the site if you need to see it to totally understand what I mean and need to do...
http://www.kaijocreative.co.uk/footballnatter
Thanks!
You should alter your query adding cat or category to your query_posts( 'posts_per_page=3' ); according to what you exactly want
see Query_posts () and also have a look at WP_Query class
you need to use find out the category ids from each post, then use these ids in the
$category = get_the_category();
$post_catid= $category[0]->term_id;
$querystr='cat='.$post_catid.'&posts_per_page=3';
query_posts($querystr);
I've got a WP_Query loop that pulls the 4 most recent posts:
<?php
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=4');
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
<div class="recentpost">
<?php echo get_the_post_thumbnail ($_post->ID, 'small'); ?>
<div class="titlerecent">
<h1><?php the_title(); ?></h1>
</div>
</div>
<?php endwhile; ?>
What I want to do is modify this and style the first post in the loop different. I don't just want to add a CSS class to it, I want to wrap it in a totally different <div>.
So I want the first post to be wrapped in <div class="homefirstpost"> and then the remaining 3 post to be as above, inside the <div class="recentpost">.
What do I need to do?
I would do it like this:
<?php
$isfirst = false;
$featuredPosts = new WP_Query();
$featuredPosts->query('showposts=4');
while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
<?php if ( ! $isfirst ): ?>
<div class="homefirstpost">
<?php $isfirst = true; ?>
<?php else: ?>
<div class="recentpost">
<?php endif; ?>
<?php echo get_the_post_thumbnail ($_post->ID, 'small'); ?>
<div class="titlerecent">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
When your posts are wrapped in another container, you could use the :first-child selector for the first post. See Selectors for details.
When your HTML looks like this:
<div id="featuredPosts">
<div>First Post ...</div>
<div>Next Post ...</div>
<div>Next Post ...</div>
<div>Next Post ...</div>
</div>
you can use CSS along these lines:
div#featuredPosts > div:first-child {
some special styles;
}