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.
Related
I have number values on my most recent post that are placed via advanced custom fields. I want to be able to pull the data from a post into another page. this can be easily done via an ID:
https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
but what I cannot accomplish is having this pull from the most RECENT post. ACF support site make no mention of most recent.
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
// acf query in here. not included for brevity.
endif;
}
wp_reset_query();
?>
From the Codex on https://developer.wordpress.org/reference/functions/wp_get_recent_posts/ (with slight modifications):
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 1, // Number of recent posts thumbnails to display
'post_status' => 'publish' // Show only the published posts
));
foreach($recent_posts as $post) : ?>
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo get_the_post_thumbnail($post['ID'], 'full'); ?>
<p class="custom-class"><?php echo $post['post_title'] ?></p>
</a>
<?php
$custom_field = get_field('custom_field_name', $post['ID']);//for ACF fields
?>
<?php endforeach; wp_reset_query(); ?>
I'm hoping someone can help me with this. I'm new to php so it is very much learning on the job.
I am customising an existing Wordpress theme but I am facing a problem with the themes advanced search/results.
The theme has no way to order search results and I know that the default order for Wordpress is by date. Currently if I perform a search, the results are displayed in date order but I need the results to be price high to low.
The current code is as follows
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article id="post-0" class="post error404 not-found">
<h1 class="posttitle"><?php _e( 'Not Found', THE_LANG ); ?></h1>
<div class="entry">
<p><?php _e( 'Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG ); ?></p>
</div>
</article>
<?php endif; ?>
<div class="nvr-prop-container row">
<?php if( have_posts() ){ ?>
<div class="search-title twelve columns">
<h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4>
</div>
<?php
$nvr_idnum = 0;
$nvr_typecol = "nvr-prop-col";
$nvr_imgsize = "property-image";
?>
<ul id="nvr-prop-search" class="<?php echo esc_attr( $nvr_typecol ); ?>">
<?php
while ( have_posts() ) : the_post();
$nvr_idnum++;
echo nvr_prop_get_box( $nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace );
$nvr_classpf="";
endwhile; // End the loop. Whew.
?>
I then decided to try and sort the results so I created
$sort_properties = new WP_Query(array(
'post_type' => 'properties',
'meta_key' => $nvr_initial.'_price',
'meta_value' => $nvr_price,
'orderby' => 'meta_value_num date',
'order' => 'DESC',
));
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<article id="post-0" class="post error404 not-found">
<h1 class="posttitle"><?php _e( 'Not Found', THE_LANG ); ?></h1>
<div class="entry">
<p><?php _e( 'Apologies, but no results were found for the requested property archive. Perhaps searching will help find a related post.', THE_LANG ); ?></p>
</div>
</article>
<?php endif; ?>
<div class="nvr-prop-container row">
<?php if( $sort_properties->have_posts() ){ ?>
<div class="search-title twelve columns">
<h4><?php _e('Search Result', THE_LANG); ?> (<?php echo $wp_query->post_count; ?>)</h4>
</div>
<?php
$nvr_idnum = 0;
$nvr_typecol = "nvr-prop-col";
$nvr_imgsize = "property-image";
?>
<ul id="nvr-prop-search" class="<?php echo esc_attr( $nvr_typecol ); ?>">
<?php
while ( $sort_properties->have_posts() ) : $sort_properties->the_post();
$nvr_idnum++;
echo nvr_prop_get_box( $nvr_imgsize, get_the_ID(), 'element columns', $nvr_unit, $nvr_cursymbol, $nvr_curplace );
$nvr_classpf="";
endwhile; // End the loop. Whew.
?>
Now when I perform a search, the posts are sorted based on price which is fantastic but... now regardless of how I search all of the site posts are now being displayed.
I felt I was so close to finding a solution but I would very much grateful if someone cold advise me with this.
Kind regards
S
Have you tried to order only by the 'meta_val_num' without the 'date' like:
$args = array(
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => 'price',
);
$query = new WP_Query( $args );
Code from: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I think you have to add the Search Parameter and probably use the new meta_query structure:
$search_query = get_search_query();
$sort_properties = new WP_Query(array(
'post_type' => 'properties',
's' => $search_query,
'orderby' => 'meta_value_num',
'meta_query' => array(
'key' => $nvr_initial.'_price',
'value' => $nvr_price,
)
));
I remember having some trouble, not using "meta_query" => array() for multiple conditions.
I have some code that generates a Google Map to show a marker based on the location field supplied via a custom field named 'event_map'. It works great.
<?php
$location = get_field('event_map');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>
I now want to add a map that shows ALL of the markers from ALL of the posts from the 'tour-dates' custom post type. How would I go about doing this assuming all of these posts have a field called 'event_map' to draw location data from? I am stumped and any help appreciated.
Get the posts with such post type and loop on it then use the ACF API get_field (the second parameter let us define the post ID in which your custom field value belongs to).
<?php
$tours = get_posts( array(
'post_type' => 'tour-dates',
'posts_per_page' => -1
));
if( !empty($tours) ): ?>
<div class="acf-map">
<?php foreach($tours as $tour): ?>
<?php
$location = get_field('event_map',$tour->ID);
if( !empty($location) ): ?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
This query should get all tour-dates posts that have information in the event_map field.
$new_loop = new WP_Query( array(
'post_type' => 'tour-dates',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_map',
'compare' => 'EXISTS'
)
)
) );
I'm not sure what you're using to display the map, so I can't help beyond that.
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.
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(); ?>