In a wordpress website showing 3 post per row and almost unlimited rows due to ajax autoload for pagination.
What is the best way to add say for example a custom HTML code instead of post every 5th post?
You will need to add a count to the loop that is pulling the posts into the rows.
for example:
<?php $args = array(
'post_type' => 'posts',
'posts_per_page' => -1
);
$posts = get_posts( $args );
if ( $posts ) :
$temp_post = $post; ?>
<div class="row">
<?php $count = 1; foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<h1><?php the_title(); ?></h1>
<?php //This count adds the custom html mark usful if its only a sinle line of markup.
echo ( $count % 5 == 0 ) ? '<h2 class="custom-html-markup">Custom Markup</h2>' : ''; ?>
<?php // If the markup is longer than a single line.
if ( $count % 5 == 0 ) : ?>
<div class="custom-html-markup">
<?php the_excerpt(); ?>
</div>
<?php endif; ?>
<?php // This count closes and re-opens the rows
echo ( $count % 3 == 0 ) ? '</div><div class="row">' : ''; ?>
<?php $count++; endforeach; ?>
</div>
<?php $post = $temp_post;
endif; ?>
Related
I am using the following code to load all the posts from a custom post type, in this loop I show a title but I also want to show the categories (terms) that are connected to this particular post but I can't seem to make it work
Loop:
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I tried:
<?php echo $term->name; ?>
You need to use get_the_terms() for getting category
you can get this by below code...
you need to put second argument a custom post-type's category slug
you can refer this link https://developer.wordpress.org/reference/functions/get_the_terms/
<?php $args = array( 'post_type' => 'fotoalbum', 'showposts'=> '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php $i=1; ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if($i==1 || $i%3==1) echo '<div class="row">' ;?>
<div class="col-md-4">
<?php the_title();?><br/>
! HERE I WANT THIS POSTS CATEGORY !
<?php
$terms = get_the_terms( get_the_ID(), 'category-slug' ); // second argument is category slug of custom post-type
if(!empty($terms)){
foreach($terms as $term){
echo $term->name.'<br>';
}
}
?>
</div>
<?php if($i%3==0) echo '</div>';?>
<?php $i++; endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Topic kinda says it all. I need to only pull in the first two fields of an ACF repeater field.
Here is what I am trying to use but it is obviously not right:
<?php $args = [ 'posts_per_page' => 2, 'order' => 'desc']; ?>
<?php $ar = new WP_Query( $args ); ?>
<?php if( $ar->have_rows('prodImgs') ): while ( $ar->have_rows('prodImgs') ) : $ar->the_row(); ?>
<img src="<?php the_sub_field('prodImg'); ?>" alt="">
<?php endwhile; ?>
<?php endif; ?>
How am I supposed to do this?
<?php
// check if the repeater has data
if( have_rows('prodImgs') ) {
//counter
$i=0;
//loop through the rows
while( have_rows('prodImgs') ) {
the_row();
//check if 2 subfields have been shown
if ( $i > 1 ) { break; }
echo "<img src='" . get_sub_field('prodImg_sub') . "' alt='Lorem ipsum'>";
$i++;
}
}
?>
You are mixing apples and pears, WP_Query and ACF Repeater field. WP_Query returns the post data, whilst the ACF function have_rows( $repeater_field_name, $post_id ); checks whether there are any data in the custom field repeater that is on your page/post (or if you specify $post_id on the relevant post/page). More info at https://www.advancedcustomfields.com/resources/repeater/
<?php $rows = get_field('prodImgs'); ?>
<?php $i = 0; if ($rows):?>
<?php foreach($rows as $row): ?>
<img src="<?php echo $rows[$i][/* name of first field */]; ?>" alt="">
<?php echo $rows[$i][/* name of Second field */]; ?>
<?php $i++; endforeach; ?>
<?php endif; ?>
I'm using below code to filter the query on page and sort the post in ascending order by price. I'm getting desire output but my concern is , i want all price with 0 at last.
<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'orderby' => 'meta_value_num','meta_key' => 'price','order' => 'ASC'));
query_posts( $args );
?>
please help me guys...Thanks in advance
full code here:
<?php
/**
* The template for displaying Archive pages.
*
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* #package progression
*/
get_header('cars'); ?>
<?php get_template_part( 'page', 'vehicle-title' ); ?>
<div class="width-container">
<?php if ( have_posts() ) : ?>
<div id="page-title">
<h1 id="page-heading">
<?php if (is_tax('vehicle_type')) {
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$tax_term_breadcrumb_taxonomy_slug = $term->taxonomy;
echo '' . $term->name . '';
}
?>
</h1>
</div><!-- close #page-title -->
<div id="4-content-container" class="tax-vehicle-container">
<?php echo do_shortcode('[vehicle_searchform include="make,model"]') ?>
<?php
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'orderby' => 'meta_value_num','meta_key' => 'price','order' => 'ASC'));
query_posts( $args );
?>
<?php
/* Start the Loop */
$count = 1;
$count_2 = 1;
?>
<?php while ( have_posts() ) : the_post();
if($count >= 5) { $count = 1; }
?>
<div class="grid4column-progression <?php if($count == 4): echo ' lastcolumn-progression'; endif; ?>">
<?php
get_template_part( 'content', 'vehicle');
?>
</div>
<?php if($count == 4): ?><div class="clearfix"></div><?php endif; ?>
<?php $count ++; $count_2++; endwhile; ?>
<div class="clearfix"></div>
<?php show_pagination_links( ); ?>
</div><!-- close #content-container -->
<?php else : ?>
<?php get_template_part( 'no-results', 'inventory' ); ?>
</div><!-- close #content-container -->
<?php endif; ?>
<?php get_sidebar( 'vehicle' ); ?>
</div><!-- close .width-container -->
<?php get_footer(); ?>
If you've got a small number of posts, the quick and dirty thing to do would be to traverse the loop twice, suppressing zeros on the first pass and suppressing everything except zeros on the second pass:
for ($i=0, $i++, $i<=1) :
while (have_posts()) : the_post();
$show_non_zeros = ($i === 0) && ( get_post_meta( get_the_ID(), 'price', true) > 0);
$show_zero_price = ($i === 1) && ( get_post_meta( get_the_ID(), 'price', true) == 0);
if ($show_non_zeros || $show_zero_price) {
//write output
}
end while;
end for;
If you're looking for a faster / more elegant solution, you can usort() $posts between your call to query_posts and the loop.
hope this will be useful to someone.. i done this way
<?php
/* ADDED by for sorting ASCENDING order but call for price(ie price with 0) last */
function my_sort_custom( $orderby, $query ){
global $wpdb;
$orderby = " case when CAST(meta_value AS SIGNED) = '0' then 1 else 0 end ,CAST(meta_value AS SIGNED) ";
return $orderby;
}
add_filter('posts_orderby','my_sort_custom',10,2);
global $wp_query;
$args = array_merge( $wp_query->query_vars, array('meta_key' => 'price'));
query_posts( $args );
?>
Sorry, if I sound a bit naive..
I am a decent PHP developer and have been trying to learn wordpress recently.
Can anyone tell me or point to a tutorial that tells me the best way to show posts on a custom template with pagination ?
Since occasionally(or maybe most of the time, I'm not sure) you can get permalink conflicts and therefore unexpected 404 pages if you just make a custom query_posts() and then you add a pagination, with links to page-slug/page/2, page-slug/page/3, page-slug/page/n, I usually set the pagination as a $_GET parameter.
Here is an example code for that:
<?php
/*
Template Name: Custom Loop Template
*/
get_header();
// Set up the paged variable
$paged = ( isset( $_GET['pg'] ) && intval( $_GET['pg'] ) > 0 )? intval( $_GET['pg'] ) : 1;
query_posts( array( 'post_type' => 'post', 'paged' => $paged, 'posts_per_page' => 1 ) );
?>
<?php if ( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<div id="post-<?php echo $post->ID; ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<div class="post-excerpt">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="pagination">
<?php for ( $i = 1; $i <= $wp_query->max_num_pages; $i ++ ) {
$link = $i == 1 ? remove_query_arg( 'pg' ) : add_query_arg( 'pg', $i );
echo '<a href="' . $link . '"' . ( $i == $paged ? ' class="active"' : '' ) . '>' . $i . '</a>';
} ?>
</div>
<?php endif ?>
<?php else : ?>
<div class="404 not-found">
<h3>Not Found</h3>
<div class="post-excerpt">
<p>Sorry, but there are no more posts here... Please try going back to the main page</p>
</div>
</div>
<?php endif;
// Make sure the default query stays intact
wp_reset_query();
get_footer();
?>
This will create a custom Page template, called Custom Loop Template and will display the latest posts, 1 per page. It will have a basic pagination at the bottom starting from 1 to the maximum number of pages for that query.
Of course, that's just a pretty basic example, but it should be enough for you to figure the rest out.
Can't seem to find the right answer for what I thought would be trivial.
I have some categories arranged like this...
Parent Category 1
- Child Category 1
- Child Category 2
- Child Category 3
...and I have some posts that are in Child Category 2. I want my page to display all posts from the category I am currently in.
This is what I am doing right now:
<?php
query_posts('cat=2&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
As you can see I am having to manually specify the category (cat=2), but instead I want it to automatically detect the category I am already in and display the posts (that way if I'm in a different category it will display those posts).
Any suggestions?
Thanks in advance. (SO Community = Awesome Sauce).
try below code:
<?php
$current_cat_id = get_query_var('cat');
$showposts = 10;
$args = array('cat' => $current_cat_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="timeline">
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; ?>
</div>
If you are using category.php, you can omit the query_posts and it will auto fill in the posts for you.
<?php
// Start the loop.
$categories = get_categories('child_of=1');//Parents category id
foreach ($categories as $cat) {
$option = '<a href="/category/archives/'.$cat->category_nicename.'">';
$option .= $cat->cat_name;//parents sub category name
$option .= '</a>';
echo $option;
query_posts('cat=$cat->cat_ID&showposts=10');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content();?>
<?php endwhile; else: ?>
<?php _e('No Posts Sorry.'); ?>
<?php endif; }?>
<ul>
<?php
global $post;
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'category' => 1 );
// 1 is a cat id.
//
$myposts = get_posts( $args );
foreach( $myposts as $post ) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
try
$args = array( 'posts_per_page' => 5, 'offset'=> 0, 'cat' => 1 );
Try this, this is better solution for this and you can also use it to show Related Post by a category id...
Just call the function that mentioned below by using this line. Put this in your template or page.php/single.php file.
Call by put this line: related_post_title('enter cat id here..');
Here is the Function and put this in function.php file.
Related posts function:
function related_post_title($cat_id){
$cat = $cat_id;
// Check if it is page only
if ( is_page() || is_single()) {
$args=array(
'cat' => $cat,
'order' => DESC,
'orderby' => rand,
'post__not_in' => array($post->ID),
'posts_per_page' => 9999,
'caller_get_posts' => 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo ' <ul> ';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
}
If any query please let me know, i will help you...