I have a while loop that gets all pages of a certain category on index.php which is my posts page.
However, I want to change the code below, so if it is the homepage, it does not run this loop but displays some text.
This code the loop works:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
?>
In this code, nothing loads on any page except the homepage:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC' ,
'paged'=>$paged
);
if ( is_home() ) {
echo 'Welcome!';
} else if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
You need to wrap your query and loop in your is_home() condition
if ( !is_home() ) { // This is not the home page
// Add your query and loop here
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
} else { // This is the homepage
echo 'Some text here';
}
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $the_query->the_post();
if ( is_home() ) {
echo 'Welcome!';
}
else{?>
<h2><a href="<?php echo get_permalink(); ?>"><?php the_title();
}
<?php endwhile; endif; ?>
Related
Why the nested post loop for post_type => 'personalne' does not return the nuber of posts specified in 'posts_per_page' parameter' ?
Nested loop seems to work ok ('posts_per_page') for 2 first post_types i.e. 'konsorcjum' and 'grupy' but somehow if I specify for instance 3 as 'posts_per_page' for 'personalne' it does not return 3 posts -> it returns 0 posts i.e. nothing.
This is the link to the code: https://wtools.io/paste-code/b1Gi
This is the PHP code:
$args = [
'post_type' => 'konsorcjum',
'where' => 't.post_status="Publish"',
'orderby' => 'position',
'order' => 'ASC',
'posts_per_page' => 100
];
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
$params = array(
'post_type' => 'grupy',
'where' => 't.post_status="Publish"',
'orderby' => 'position',
'order' => 'ASC',
'posts_per_page' => 2
);
$group_query = new WP_Query( $params );
// The Loop
while ( $group_query->have_posts() ) :
$group_query->the_post();
$nazwa_grupy = get_the_title();
// NAZWA GRUPY:
echo get_field( "nazwa_grupy");
$params666 = array(
'post_type' => 'personalne',
'where' => 't.post_status="Publish"',
'orderby' => 'position',
'order' => 'ASC',
'posts_per_page' => 1
);
$person_query = new WP_Query( $params666 );
// The Loop
while ( $person_query->have_posts() ) :
$person_query->the_post();
if( have_rows('dane_osoby') ):
while( have_rows('dane_osoby') ): the_row();
echo the_sub_field( "imie_i_nazwisko", $person_query->post->ID);
endwhile;
endif;
// Koniec pętli while dla PERSONALNE:
endwhile; wp_reset_postdata();
// Koniec pętli while dla GRUPY:
endwhile; wp_reset_postdata();
// Koniec pętli while dla KONSORCJUM:
endwhile; wp_reset_postdata();
Try below code and let me know if its works.
$args = [
'post_type' => 'konsorcjum',
'where' => 't.post_status="Publish"',
'orderby' => 'position',
'order' => 'ASC',
'posts_per_page' => 100
];
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
$params = array(
'post_type' => 'grupy',
'where' => 't.post_status="Publish"',
'orderby' => 'position',
'order' => 'ASC',
'posts_per_page' => 2
);
$group_query = new WP_Query( $params );
// The Loop
while ( $group_query->have_posts() ) :
$group_query->the_post();
$nazwa_grupy = get_the_title();
// NAZWA GRUPY:
echo get_field( "nazwa_grupy");
$params666 = array(
'post_type' => 'personalne',
'where' => 't.post_status="Publish"',
'orderby' => 'position',
'order' => 'ASC',
'posts_per_page' => 1
);
$person_query = new WP_Query( $params666 );
// The Loop
while ( $person_query->have_posts() ) :
$person_query->the_post();
if( have_rows('dane_osoby') ):
while( have_rows('dane_osoby') ): the_row();
echo the_sub_field( "imie_i_nazwisko", $person_query->post->ID);
endwhile; wp_reset_postdata();
endif;
// Koniec pętli while dla PERSONALNE:
endwhile; wp_reset_postdata();
// Koniec pętli while dla GRUPY:
endwhile; wp_reset_postdata();
// Koniec pętli while dla KONSORCJUM:
endwhile; wp_reset_postdata();
Im trying to display specific product categories in an ACF repeater field.
This keeps outputting all product categories.
How do I go to output single category per repeater field?
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_category_ids = get_sub_field('project_category');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'terms' => $product_category_ids
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' ); ?>
<?php
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
<?php endwhile; ?>
</ul>
This worked for me:
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_cats = get_sub_field('product_category');
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_cats,
'operator' => 'IN',
'field' => 'slug'
),
)
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endwhile; ?>
</ul>
I am currently putting this array into my table of contents. To create something like this...
<?php
$args = array(
'post_category' => 'live',
'post_status' => 'publish',
‘order’ => ‘ASC’,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_field('venue');
the_excerpt();
endwhile;
wp_reset_postdata();
?>
When I add a new custom post the information is displaying on the same line.
I want each post to start on a new line.
Use tag div for record in 1 one.
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
$count = 1;
while ( $loop->have_posts() ) : $loop->the_post();
echo "<div class='item item-".$count."'>";
echo the_field( 'venue' );
the_excerpt();
echo "</div>";
$count++;
endwhile;
wp_reset_postdata();
?>
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo the_field( 'venue' );
the_excerpt();
echo PHP_EOL;
endwhile;
wp_reset_postdata();
?>
In case of HTML
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
?>
<div class="list">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="list-item">
<?php echo the_field( 'venue' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>
So I have a custom Taxonomy 'Sectors'.
I am trying to display all the single posts in 'Sectors' in a list, just a basic list.
I currently do have it so, it will display the category, then a sub category, then the list from there.
I also have this, which lists all posts - (NOT TAXONOMY) :
<?php
$uncat = get_cat_ID('uncategorised');
$args = array(
'posts_per_page' => 5,
'category__not_in' => array($uncat)
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<div class="contentfaq">
<h3><?php the_title(); ?></h3>
<span class="entry-date-orange"><strong><?php echo get_the_date(); ?></strong></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>
<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
is there a way, to use this code below, to ONLY get items from the taxonomy 'sectors'?
For this to work, you will need to get all the terms belonging to your taxonomy, and then use a tax_query to query the correct posts. For effeciency, we will only get the term ids and not the complete objects
(NOTE: The code is untested and requires PHP 5.4+. Also, just confirm the taxonomy name)
$term_ids = get_terms( 'SectorCategories', ['fields' => 'ids'] );
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = [
'post_type' => 'sectors',
'posts_per_page' => 5,
'paged' => $paged,
'tax_query' => [
[
'taxonomy' => 'SectorCategories',
'terms' => $term_ids,
]
],
// REST OF YOUR ARGUMENTS
];
$loop = new WP_Query( $args );
EDIT
For older PHP versions, use the following code
$term_ids = get_terms( 'SectorCategories', array( 'fields' => 'ids' ) );
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = array(
'post_type' => 'sectors',
'posts_per_page' => 5,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'SectorCategories',
'terms' => $term_ids,
)
),
// REST OF YOUR ARGUMENTS
);
$loop = new WP_Query( $args );
EDIT 2
A very basic loop for the above query would look something like this
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
the_title();
}
next_posts_link( 'Next posts', $loop->max_num_pages );
previous_posts_link( 'Previous posts' );
wp_reset_postdata();
}
I am loading in variable products via a custom WP_Query
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'date',
'order' => 'desc'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="product-node cat-beast-balls">
<?php wc_get_template_part( 'content', 'single-product' ); ?>
</div>
<?php endwhile;
}
wp_reset_postdata();
This seems to work fine. However, I am using ajax to reload the products but with a different loop such as this one.
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'price',
'order' => 'asc'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="product-node cat-beast-balls">
<?php wc_get_template_part( 'content', 'single-product' ); ?>
</div>
<?php endwhile;
}
wp_reset_postdata();
I can notice however that between 'asc' and 'desc' the order is flipped, so at least that's working. My problem is that the orderby value seems to make no difference. How can I make it so that the loop changes whether or not the products are ordered by date or price?
Thanks all!
Try this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 100,
'product_cat' => 'beast-balls',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc'
);