Simple output from parent category - php

I am working with this code in template page for WP.
<div id="content" role="main">
<?php
$query = array(
'post_type' => 'post',
'category_name' => 'jewellery-design',
'orderby' => 'date',
'order' => 'DESC'
);
$featured_home = new WP_Query( $query );
if( $featured_home->have_posts() ) {
?>
<div class="row">
<?php while ( $featured_home->have_posts() ) : $featured_home->the_post();?>
<span class="cat"><?php the_category(); ?>
<div class="col-3">
<a href="<?php the_permalink(); ?>">
<div class="featured-home-img" <?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' style="background-image: url(%s);"', $image_src[0] );
}?>>
<div class="blog-info-content">
<h3><?php the_title(); ?></h3>
<div class="obsah"><?php the_content(); ?></div> </span>
<p class="postmetadata">
Posted in <?php the_category(', ') ?>
<strong>|</strong>
<?php edit_post_link('Edit','','<strong>|</strong>'); ?>
</span>
</div>
</div>
</a>
</div>
<?php
endwhile;
?>
</div>
<?php
}
wp_reset_postdata();
?>
</div>
And I am getting output like this:
But, i´d like to have output like this:
Can someone tell me how can I do it? Or may I use some plugin for Wordpress to make it easier for me?
Thank you.

I might think about sorting them into groups first, then re-looping over the sorted groups. I am a little rusty on Wordpress so I have set it up essentially, but all the functions/methods need to return values, not echo, so you will need to adjust that:
<?php
# Create base function
function getItemsByCategoryType($category)
{
$query = array(
'post_type' => 'post',
'category_name' => $category,
'orderby' => 'date',
'order' => 'DESC'
);
# Get query
$featured_home = new WP_Query($query);
# Just stop if empty
if(!$featured_home->have_posts())
return false;
# Set a default
$array = array();
while($featured_home->have_posts()){
$featured_home->the_post();
$thumbnail_id = get_post_thumbnail_id();
$image_src = ($thumbnail_id)? wp_get_attachment_image_src($thumbnail_id,'normal-bg')) : false;
# You are looking to store the category title as the key
# That will isolate each group
$array[the_category()][] = array(
'thumb_id' => $thumbnail_id,
'image' => $image_src,
'permlink' => the_permalink(),
'title' => the_title(),
'content' => the_content(),
'categories' => the_category(', '),
'edit_mode' => edit_post_link('Edit','','<strong>|</strong>')
);
}
wp_reset_postdata();
# Send back stored data as an array
return $array;
}
# To use, get the data from your function
$Items = getItemsByCategoryType('jewellery-design') ?>
<div id="content" role="main">
<?php if(!empty($Items)): ?>
<div class="row">
<?php foreach($Items as $categories => $rows): ?>
<span class="cat"><?php echo $categories ?>
<?php foreach($rows as $row): ?>
<div class="col-3">
<a href="<?php echo $row['permlink'] ?>">
<div class="featured-home-img"<?php if(!empty($row['image'][0])) echo ' style="background-image: url('.$row['image'][0].');"' ?>>
<div class="blog-info-content">
<h3><?php echo $row['title'] ?></h3>
<div class="obsah">
<?php echo $row['content'] ?>
</div>
<p class="postmetadata">Posted in <?php echo $row['categories'] ?><strong>|</strong><?php $row['edit_mode'] ?></p>
</div>
</div>
</a>
</div>
<?php endforeach ?>
</span>
<?php endforeach ?>
</div>
<?php endif ?>
</div>
NOTE: Just to reiterate, I am not up to speed on WP so you need to take the idea of what I am proposing and fixing it, specifically when it comes to returning values vs allowing the WP functions to echo content.
EDIT:
You want an array to come out of the function similar to:
Array (
[ Fibre Collection ] => Array (
[ 0 ] => Array (
[ title ] => FIBRE_BRACELET
[ content ] => etc...,
etc...
)
),
[ Tetragon Collection ] => Array (
[ 0 ] => Array (
[ title ] => Test2
[ content ] => etc...,
etc...
),
[ 1 ] => Array (
[ title ] => TETRAGÓN_BRACELET
[ content ] => etc...,
etc...
)
)
)

I found a solution to the output I needed. If this helps anyone :)
$cat_args=array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 2
);
$categories=get_categories($cat_args);
foreach($categories as $category) {
$args=array(
'showposts' => -1,
'category__in' => array($category->term_id),
'caller_get_posts'=>1
);
$posts=get_posts($args);
if ($posts) {
echo '<div class="test"><p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
foreach($posts as $post) {
setup_postdata($post); ?>
<p><?php the_title(); ?></p>
<p><a href="<?php the_permalink() ?>" <?php the_content(); ?></a></p>
<?php
} // foreach($posts
echo '</div>';} // if ($posts
} // foreach($categories
?>
The 'parent' => 2 means ID of parent category from which you want to get your posts.
Have a nice day.

Related

Query posts by ACF checkbox field

I cant seem to get facetwp to only display posts where an ACF checkbox field has a value of yes. My ACF checkbox value is stored in the post sidebar which I believe attaches it to the post itself (for lack of a more knowledgable way of explaining it)
There are lots of examples that are similar online, but I can't seem to modify any to work for me.
I have also tried with the ACF True/false field type, as it doesn't matter to me which type of field it is.
My latest attempt is
<div class="full-width-inner">
<div class="package-listings">
<div class="listings-table-row">
<?php
$type = get_field( 'is_this_educational', get_the_ID() );
$my_posts = new WP_Query([
'post_type' => 'packages',
'posts_per_page' => 3,
'orderby' => 'date',
'meta_query' => [
[
'key' => 'is_this_educational',
'value' => '1'
]
]
]);
if ( $my_posts->have_posts() ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<div class="package-wrapper edu_green_bg">
<?php if( get_field('add_flag', $post->ID)) { //Check if checkbox is checked ?>
<div class="flag orange-bg"><p><?php the_field( 'flag_text', $post->ID ); ?></p></div>
<?php } ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="package-home-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<div class="result-info">
<div class="home-package-title">
<?php the_title(); ?>
</div>
<div class="home-package-excerpt">
<p><?php echo wp_trim_words( get_the_excerpt(), 20, '...' ); ?></p>
</div>
<div class="home-package-button orange-bg">
<button>Read More</button>
</div>
</div>
</div> <!-- package wrapper end -->
<?php
endwhile;
wp_reset_postdata();
}
?>
</div> <!-- listings table row end -->
</div> <!-- Package listings end -->
</div>
But I have also tried:
$type = get_field( 'is_this_educational', get_the_ID() );
$my_posts = new WP_Query([
'post_type' => 'packages',
'posts_per_page' => 3,
'orderby' => 'date',
'meta_query' => [
[
'key' => 'is_this_educational',
'value' => '"yes"'
]
]
]);
if ( $my_posts->have_posts() ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
And
<?php
$type = get_field( 'is_this_educational', get_the_ID() );
$my_posts = new WP_Query([
'post_type' => 'packages',
'posts_per_page' => 3,
'orderby' => 'date',
'meta_query' => [
[
'key' => 'is_this_educational',
'value' => 'yes',
'compare' => 'LIKE'
]
]
]);
if ( $my_posts->have_posts() ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
And lastly - which I think might be the closest of them all but gives me a critical error.
Sorry for all the code but just to demonstrate that I have been trying
<section class="full-width-package-filter full-width-section cream-bg">
<div class="full-width-inner">
<div class="package-listings">
<div class="listings-table-row">
<?php
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'is_this_educational',
'value' => '"yes"',
'compare' => 'LIKE'
)
)
));
if( $posts ) {
while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<div class="package-wrapper edu_green_bg">
<?php if( get_field('add_flag', $post->ID)) { //Check if checkbox is checked ?>
<div class="flag orange-bg"><p><?php the_field( 'flag_text', $post->ID ); ?></p></div>
<?php } ?>
<?php if ( has_post_thumbnail() ) : ?>
<div class="package-home-image">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
</div>
<?php endif; ?>
<div class="result-info">
<div class="home-package-title">
<?php the_title(); ?>
</div>
<div class="home-package-excerpt">
<p><?php echo wp_trim_words( get_the_excerpt(), 20, '...' ); ?></p>
</div>
<div class="home-package-button orange-bg">
<button>Read More</button>
</div>
</div>
</div> <!-- package wrapper end -->
<?php
endwhile;
wp_reset_postdata();
}
endif;
?>
</div> <!-- listings table row end -->
</div> <!-- Package listings end -->
</div>
</section>

ACF row to show thumbnails of recent posts

I'm trying to create a ACF flexible content row to display the most recent post thumbnails for a given category. However it keeps throwing a critical error and I'm not sure why.
<?php
$section_id = get_sub_field('section_id')
$categories = get_sub_field('categories');
$tags = get_sub_field('tags');
$postnum = get_sub_field('number_of_posts');
if (!is_array($categories)) {
$categories = array($categories);
}
$tags = get_field('my_tags_field');
if (!is_array($tags)) {
$tags = array($tags);
}
$args = array(
'post_type' => 'post',
'numberposts' => $postnum,
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'terms' => $categories
),
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$query = new WP_Query($args);
?>
<style>
</style>
<section class="post_row_with_thumbnails" id="<?php echo $section_id; ?>">
<div class="container-fluid">
<div class="row">
<?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post(); ?>
<div class="col">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ?>" class="project_pics">
<h5 class="posttitle"><?php the_title(); ?></h5>
<h6 class="postdate"><?php the_date(); ?></h6>
</a>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
</div>
</div>
</section>
I have tried substituting WP_Query() with get_posts() but it gives me the same critical error.
In line 2 the ; at the end of the line, in this part:
$section_id = get_sub_field('section_id') ; THE DOT AND COMMA IS MISSING
This is the reason for the critical error.

Trying to exclude featured posts from Wordpress post loop

Ive installed a featured post plugin "NS Featured Posts" which works quite nicely but has no known functionality for excluding those posts from another loop. Here's what I have so far:
<div id="all-news-block" class="container">
<div class="row">
<?php
$args = array(
'post_type' => 'post',
'posts_per_page'=>-1,
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
),
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="news-item-block col-md-4" role="article">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<a class="news-item-image-link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured-news-item-image'); ?>
</a>
<?php endif; ?>
<span class="news-item-date"><?php echo get_the_date('M d, Y'); ?></span>
<a class="news-item-title" href="<?php the_permalink(); ?>">
<h1><?php the_title(); ?></h1>
</a>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
</div>
Any help would be appreciated.
Since I cannot comment due to the lack of reputation, I will post this as an answer although I don't think is a final/suitable answer.
Focusing on the array you have in there:
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT LIKE',
),
I would rather change the third value (2) to this - as a regular php operator -:
'compare' => '!=',
Also, you could change the while loop in order to count after the number of featured posts you got.

post by taxonomy not showing at all in wordpress

Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</ul>
</div>

WP_Query orderby 'rand' not working

Trying to order some posts I'm displaying on a single custom post type page with random, but they aren't random at all. :/
<?php
// Grab the taxonomy term slug for the current post
$terms = get_the_terms( get_the_ID(), 'category-staff' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->slug;
}
$on_draught = join( ", ", $draught_links );
?>
<div class="container hidden-xs">
<div class="row">
<div class="col-sm-12">
<hr />
<h3 class="text-center">Other People At Our Great Resort</h3>
</div>
</div>
<div class="row">
<div class="col-sm-12 col-lg-10 col-lg-offset-1">
<div class="row staff-list">
<?php
// WP_Query arguments
$args2 = array (
'post_type' => 'staff',
'tax_query' => array(
array(
'taxonomy' => 'category-staff',
'field' => 'slug',
'terms' => $on_draught,
),
),
'nopaging' => false,
'posts_per_page' => '4',
'order' => 'DESC',
'orderby' => 'rand',
);
// The Query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) {
while ( $query2->have_posts() ) {
$query2->the_post(); ?>
<div class="staff staff-other col-sm-3 text-center">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php echo get_the_post_thumbnail( $_post->ID, 'large', array( 'class' => 'img-responsive img-circle img-staff' ) ); ?>
<h4><?php the_title(); ?></h4>
<?php if (get_field('staff_job')) { ?>
<p><?php the_field('staff_job'); ?></p>
<?php } ?>
</a>
</div>
<?php }
} else { ?>
<?php }
// Restore original Post Data
wp_reset_postdata(); ?>
</div>
</div>
</div>
</div>
<?php endif; // terms if statement ?>
Turns out it was something to do with WPEngine. They disable rand() from the server and it needs to be enabled manually.
Another solution may be to add this code before running the new WP_Query($args) function.
remove_all_filters('posts_orderby');
https://developer.wordpress.org/reference/functions/remove_all_filters/

Categories