I'm working on a website. I'm using page.php to control all the pages even though they have different categories. All was ok on my local server (XAMPP), I could view all the pages, but when I uploaded online, the loops returned blank.
I decided to echo an "Hello" if page is true which worked fine. The main loop returns blank. Here is my code sample
<?php
//this parts works
if ( is_page( 10 ) ) {
echo "hello";
}
//This returns blank even when the category id and
if ( is_page( 10 ) ) {
$args = array(
'post_type' =>'page',
'posts_per_page' => 1,
'cat' => 5
);
$new_query = new WP_Query( $args );
while ( $new_query->have_posts() ) : $new_query->the_post();
echo '<div class="col-lg-4 col-sm-6">
<div class="post_box3">
<div class="post_cont boxeq">
<a href="' the_permalink(); '">
<h3>' the_title(); '</h3></a>
</div>
</div>
</div>';
endwhile;
wp_reset_postdata();
}
?>
Either echo something or terminate PHP for a moment
while ( $new_query->have_posts() ) : $new_query->the_post();
echo '<div class="col-lg-4 col-sm-6">
<div class="post_box3">
<div class="post_cont boxeq">
<a href="' . the_permalink() . '">
<h3>' . the_title() . '</h3></a>
</div>
</div>
</div>'
endwhile;
or
while ( $new_query->have_posts() ) : $new_query->the_post(); ?>
<div class="col-lg-4 col-sm-6">
<div class="post_box3">
<div class="post_cont boxeq">
<a href="<?php the_permalink(); ?> .">
<h3><?php the_title(); ?></h3></a>
</div>
</div>
</div>
<?php
endwhile;
If you haven't moved an exact copy of your website on the host there might be possibilities that the ID which in your case is 10 or the cat which in your cast is 5 is changed. Try using slugs instead.
<?php
if ( is_page( 'your-page-slug' ) ) {
echo "hello";
}
//This returns blank even when the category id and
if ( is_page( 'your-page-slug' ) ) {
$args = array(
'post_type' =>'page',
'posts_per_page' => 1,
'category_name' => 'your-category-slug'
);
$new_query = new WP_Query( $args );
while ( $new_query->have_posts() ) : $new_query->the_post();
'<div class="col-lg-4 col-sm-6">
<div class="post_box3">
<div class="post_cont boxeq">
<a href="' the_permalink(); '">
<h3>' the_title(); '</h3></a>
</div>
</div>
</div>'
endwhile;
wp_reset_postdata();
}
?>
Related
i have a problem when trying to show a current 4 post using WP_Query.
But, the older post (The very first post) also shown in the first loop.
This is my code:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '3'
);
$query = new WP_Query( $args ); //show 4 post
if ( $query->have_posts() ){
/* Start the Loop */
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="col-md-3 d-flex align-items-stretch">
<div class="card ">
<?php if (has_post_thumbnail()) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
?>
<img class="miniimg" src="<?php echo $featured_img_url; ?>" width="auto" height="200px">
<?php } ?>
<div class="card-body">
<h4><a href="<?php the_permalink(); ?>">
<?php the_title();
$post_date = get_the_date( 'j F Y' );
?>
</a></h4>
<p class="card-text"><?php the_excerpt();?></p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-primary">Read More</button>
</div>
<small class="text-muted"><?= $post_date; ?></small>
</div>
</div>
</div>
</div>
<?php
$counter++; }
}
?>
This is the result =>
How to fix this problem? is there any problem with my code?
Thankyou.
If specifying 'posts_per_page' => 3 gives back 4 posts, it is almost 100% sure that the first post is a sticky post. Use the option ignore_sticky_posts to ignore them.
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'ignore_sticky_posts' => 1,
);
...
A Sticky Post is the post will be placed at the top of the front page of posts. This feature is only available for the built-in post type post and not for custom post types.
Source # https://developer.wordpress.org/themes/functionality/sticky-posts/
I'm pretty sure you applied is sticky to your post from 2012.
To verify you can just add the following to your loop inside the while statement:
<?php //...
while( have_posts() ): the_post();
if( is_sticky() ):
echo 1;
else: echo 0;
endif;
endwhile;
//... ?>
Or go to you post in the admin console, and verify that you didn't check the "is sticky" checkbox on the right side in the publish panel.
<?php
$args = array( 'post_type' => 'movies');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?><div class="movie-content" >
<?php
echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">' ?>
<h2><?php echo the_title(); ?></h2>
<div><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="excerpt"><?php the_excerpt(); ?></div>
<?php
the_content();
echo '<div class="entry-content">';
echo '</div>';
echo "</div></a>";
endwhile;
?>
I want to create a loop for wordpress that returns each two posts inside its own div and alternating columns every new row (see example)... Im not experimented in php enough to make this happen. I dont manage to get it working appropiatly. And see how to make the last div to bee 100% width if it does not have another column.
I would appreciate your support to make this happen since I tried many things and still no luck. (im using visual composer bootstrap classes, it does work but not as expected.This is the example I want to create
This is my code:
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'inversion',
'category_name' => '',
'order' => 'DESC',
'orderby' => 'DATE'
);
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="vc_row">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); $i++; $imagen = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<?php if(($i % 2) == 2) : ?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
<div class="vc_col-sm-6 cont-der" >
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url(<?php echo $imagen; ?>)no-repeat; background-size:cover;">
</div>
</div>
</div>
</div>
<?php else : ?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-der" >
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url(<?php echo $imagen; ?>)no-repeat; background-size:cover;">
</div>
</div>
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
</div>
</div>
<?php endif; endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
[EDIT]Try this:
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'inversion',
'category_name' => '',
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="vc_row">
<?php
$float_class = '';
while ( $the_query->have_posts() ) :
$the_query->the_post();
if ( $the_query->current_post &&
$the_query->current_post % 2 === 0 ) {
$float_class = $float_class ? '' : 'vc_pull-right';
}
$imagen = get_the_post_thumbnail_url( get_the_ID(), 'full' );
?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-der <?php echo $float_class; ?>">
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url('<?php echo esc_url( $imagen ); ?>') no-repeat; background-size:cover;">
</div>
</div>
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
</div>
</div>
<?php endwhile; // end have_posts() loop ?>
</div><!-- .vc_row -->
<?php endif; // end have_posts() check ?>
<?php wp_reset_query(); ?>
I have created a Custom Post Type product and for this CPT I've also created a taxonomy with the name products_types.
Now on my overview page of all the products I would like to echo out the product type that was given to the product. But I keep getting bool(false).
My code:
<div class="row">
<?php
$loop = new WP_Query( array( 'post_type' => 'product') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-lg-3 work">
<div class="category">
<?php
$category = get_the_terms('product', 'products_types');
var_dump($category);
echo $category;
?>
</div>
<a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt="">
<div class="overlay">
<div class="overlay-caption">
<p><?php echo the_title() ?></p>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
Anyone can help me out here please?
You need to pass the Post ID or object in first parameter of get_the_terms(). Used get_the_ID() which return the post ID.
Example:
foreach (get_the_terms(get_the_ID(), 'products_types') as $cat) {
echo $cat->name;
}
How to print taxonomy terms of custom post type in WordPress loop?
<div class="row">
<?php
$loop = new WP_Query( array( 'post_type' => 'product') );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-4 col-lg-3 work">
<div class="category">
<?php
$terms = get_the_terms( get_the_ID(), 'products_types' );
if ( $terms && ! is_wp_error( $terms ) ) :
$category_links = array();
foreach ( $terms as $term ) {
$category_links[] = $term->name;
}
$categories = join( ", ", $category_links );
?>
<?php printf( esc_html( $categories ) ); ?>
<?php endif; ?>
</div>
<a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt="">
<div class="overlay">
<div class="overlay-caption">
<p><?php echo the_title() ?></p>
</div>
</div>
</a>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
I want to display a simple front page with 2 categories,
But when I enter 2 same code, the result is empty,
But one code, the result appears
I look like:
Title: chicken category
Content
Title: cow category
Content
I tried with a simple php code:
<h2 class="home1">10 Best Chicken</h2>
<div class="1"><?php query_posts('cat=9' . '&showposts=3'.'&paged='.$paged); ?></div>
<h2 class="home1">10 Best Cow</h2>
<div class="1"><?php query_posts('cat=10' . '&showposts=3'.'&paged='.$paged); ?></div>
How the best solution?
Don't use query_posts , use WP_Query instead
<?php get_header(); ?>
<?php if((is_home())&& ($paged < 1)) { ?>
<?php get_template_part( 'home-featured' ); ?>
<?php } ?>
<div class="wisata-konten">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php if((is_home())&& ($paged > 1)) { ?>
<h1><?php bloginfo('name'); ?> - <?php if ( get_query_var('paged') ) { echo 'Halaman '. get_query_var('paged'); } ?></h1>
<?php } ?>
<div class="row"><div class="boxer2">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="col-md-4"><?php get_template_part( 'thumb' ); ?></div>
<?php endwhile; ?></div></div>
<?php fastestwp_pagenavi(); ?>
<?php else : ?>
<h2>Not Found</h2>Sorry, but you are looking for something that isn't here.
<?php endif; ?>
<h2 class="home1">Lombok Open Trip</h2>
<?php $args = array( 'cat' => 9 , 'posts_per_page' => 3 , 'paged' => $paged );
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
?><h2 class="home1">Lombok Promo</h2><?php
$args2 = array( 'cat' => 10 , 'posts_per_page' => 3 , 'paged' => $paged );
$the_query2 = new WP_Query( $args2 );
// The Loop
if ( $the_query2->have_posts() ) {
echo '<ul>';
while ( $the_query2->have_posts() ) {
$the_query2->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
} ?>
<div class="wisata-testimoni">
<div class="container">
<div class="row">
<div class="col-md-12">
<div id="testimoni" class="carousel slide">
<div class="carousel-inner">
<?php fastestwp_comments(); ?>
</div>
</div>
<div class="tombol"><a class= "medium beli blue pull-right" href="<?php echo home_url() ; ?>/testimoni" >Lihat Semua Testimoni <span class="glyphicon glyphicon-thumbs-up"></span></a></div>
</div>
</div>
</div>
</div>
I'm fairly new with using WP shortcodes, and I've run into a problem. I have tried to make a shortcode, that shows 6 of my blog posts through a loop, but it doesn't work. When it loads, it just smashes the page. The loop code works in practice, just not with the shortcode.
The code
function myshort() { ?>
<?php
$args = array( 'post_type' => 'cases', 'posts_per_page' => 6 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-sm-6 wow fadeInUp" data-wow-delay="0.1s">
<a class="content" href="<?php echo get_permalink( $post->ID ); ?>">
<div class="image">
<?php the_post_thumbnail(); ?>
</div>
<div class="text">
<span class="date"><?php echo rwmb_meta( 'rw_stitle' ); ?></span>
<h3><?php the_title(); ?></h3>
<p><?php echo rwmb_meta( 'rw_sdesc' ); ?></p>
</div>
</a>
</div>
<?php endwhile;
}
add_shortcode('doitman', 'myshort');
So, my question is, how do I write this the right way?
The result should be returned as a value,
you could try something like this :
function myshort() {
ob_start(); ?>
<?php
$args = array( 'post_type' => 'cases', 'posts_per_page' => 6 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="col-sm-6 wow fadeInUp" data-wow-delay="0.1s">
<a class="content" href="<?php echo get_permalink( $post->ID ); ?>">
<div class="image">
<?php the_post_thumbnail(); ?>
</div>
<div class="text">
<span class="date"><?php echo rwmb_meta( 'rw_stitle' ); ?></span>
<h3><?php the_title(); ?></h3>
<p><?php echo rwmb_meta( 'rw_sdesc' ); ?></p>
</div>
</a>
</div>
<?php endwhile;
return ob_get_clean();
}
add_shortcode('doitman', 'myshort');