Essential Grid not showing up in Wordpress posts - php

Well...the problem is that ... Ess. Grid is not showing up in posts for a one page. It's a custom made template. Maybe there is something wrong with my post loop. Maybe there is something wrong with my jquerys.
The loop
case 'gallery':
$a_div_st = '<div id="';
$cat_name = $category->name;
$a_div_st_e = '" class="container-fluid">';
$a_div_end = '</div>';
$g_args = array(
'category_name' => 'gallery',
'post_type' => 'post',
'orderby' => 'date',
'order' => 'ASC',
);
$gallery = new WP_Query($g_args);
if($gallery->have_posts()){
echo $a_div_st.$cat_name.$a_div_st_e;
while($gallery->have_posts()){
$gallery->the_post();
echo '<h1>';
the_title();
echo '</h2>';
the_content();
}
wp_reset_postdata();
echo $a_div_end;
}
break;
Sorry for the unorganized code. I have never made WP templates.

Related

Is it possible to get Elementor Style of post content in $post->post_content?

What I am trying to get?
I'm creating an Elementor custom widget. which will output a list of posts of a specific post type. Thus far everything is ok. I'm getting all I need. But the issue is, I have to output post content with its Elementor customization(created with Elementor). I've used WP_Query and get_posts(). all I get is raw HTML without any Elementor customization classes 😓. Please check the image
What I've done?
public function wp_post_list($slug_name,$order='ASC',$orderby='date'){
$args = [
'post_type' => $slug_name,
'posts_per_page' => -1,
'orderby' => $orderby,
'order' => $order,
];
return $all_posts = new \WP_Query($args);
}
public function post_list($slug_name,$order='ASC',$orderby='date'){
$args = [
'post_type' => $slug_name,
'posts_per_page' => -1,
'orderby' => $orderby,
'order' => $order,
];
$all_posts = get_posts($args);
return $all_posts;
}
public function skin_time_line($slug){
$st = $this->get_settings_for_display();
$order = $st['post_order'];
$orderby = $st['post_orderby'];
$posts = $this->post_list($slug,$order,$orderby);
$all_posts = $this->wp_post_list($slug,$order,$orderby);
echo '<div class="time_line">';
foreach($posts as $post){
echo '<div class="time_line_post">';
echo '<div class="tl_content">'.$post->post_content.'</div>';
//Getting just the HTML without any class
echo '</div>';
}
echo '</div>';
//or With WP_Query
echo '<div class="content">';
$all_posts = $this->wp_post_list($slug,$order,$orderby);
if ($all_posts->have_posts()) :
while ($all_posts->have_posts()) : $all_posts->the_post();
the_content(); // Getting just the HTML without any class
endwhile;
endif;
echo '</div>';
echo '</div>';
}
Please help! Any kind of suggestion or documentation would be appreciated.
Try to use
echo Elementor\Plugin::instance()->frontend->get_builder_content_for_display(get_the_ID());
Used to render and return the post content with all the Elementor elements.
https://github.com/elementor/elementor/blob/9aadba35a9ad52cd5e9cbef55a5761f47403491c/includes/frontend.php#L1020-L1025
if you want only the content you can use
echo Elementor\Plugin::instance()->frontend->get_builder_content(get_the_ID());

Wordpress how to display hierarchy of pages with a WP Query-Parent->Child Relationship

I'm currently trying to fetch all my pages from different post types and display them on a single page with a WP Query but I wanted to display them in such a template in the HTML instead of it all coming as a big list of course I'm not entirely sure if this is even possible
Parent
Child
Parent
Child
Parent
Parent
Parent
This is what I'm using so far
$args = array(
'post_type'=> 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$result = new WP_Query($args);
?>
<section>
<h2><?php echo $post_type; ?></h2>
<?php
while ($result->have_posts()) {
$result->the_post();
$post = get_post();
?>
<ul>
<li class="">
<?php echo the_title(); ?>
</li>
</ul>
<?php
}
wp_reset_postdata();
?>
</section>
<?php }
I tried using wp_list_pages but I'm not sure how to use that for different post types and also I would like to have limited posts per page not display everything
WP_Query is not the right tool for the job!
Wordpress has special functions for what you're looking for:
get_pagesDocs
wp_list_pagesDocs
"I tried using wp_list_pages but I'm not sure how to use that for different post types"
Well, that's why docs are here for!
For example, wp_list_pages accept an argument called post_type which lets you specify which "post type" you're interested in, and its default value is the wordpress "page" type. If you want to change it to your custom post type then you could use something like this:
$args = array(
'post_type' => 'your_custom_post_type'
);
wp_list_pages($args);
"also I would like to have limited posts per page not display everything"
Again, there is docs for that!
if you use get_pages for example, then there is an argument for limiting the number of pages shown on the page and that's called number. The number argument is an integer and represents the number of pages to return and its default is 0(all pages).
Using get_pages also you could specify your custom post type to query. The argument for that is called post_type and its default value is the wordpress "page" type.
$args = array(
'post_type' => 'your_custom_post_type',
'number' => 20
);
$your_pages = get_pages($args);
foreach ($your_pages as $page) {
echo $page->post_title;
}
If you really need/want to use WP_Qery then you could do it like this:
WP_Qery + get_pages
$args = array(
'post_type' => 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$results = new WP_Query($args);
if ($results) {
while ($results->have_posts()) {
$results->the_post();
echo "<h3>" . get_the_title() . "<h3>";
$sub_pages_args = array(
'child_of' => get_the_ID(),
);
$sub_pages = get_pages($sub_pages_args);
echo "<ul>";
foreach ($sub_pages as $sub_page) {
echo "<li>";
echo $sub_page->post_title;
echo "</li>";
}
echo "</ul>";
}
}
wp_reset_postdata();
Which will output this:
Parent Title
Child Title
Parent Title
Child Title
Parent Title
Parent Title
Parent Title
Fully tested and works on wordpress 5.8.
$pages = get_pages();
if ($pages) {
foreach ($pages as $page) {
if ($page->post_parent == 0) {
echo $page->post_title . ' — ' . $page->ID;
$sub_pages = get_pages(array('child_of' => $page->ID));
foreach ($sub_pages as $sub_page) {
echo ' — ' . $sub_page->post_title . ' — ' . $sub_page->ID;
}
}
}
}

My php-wordpress if while loop only runs the first time

My code had a typo!
But im not allowed to delete this so here you have some info on ways you could have done a better job then me ;)
The problem is as follows, My query is skipping a part of my loop.
i have a query that makes a anchor with the title and thumbnail of the post.
it runs the query fine for the first post except for the second post it does not load in the thumbnail, but it does show the title, and the title is only mentioned after the thumbnail, There is no small image that resembles it cant be found either. My first question posted on here so apolagies for misplacing items in the wrong sections.
<?php
// WP_Query arguments
$args = array (
'post_type' => array( 'klantcase' ),
'post_status' => array( 'publish' ),
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
);
// The Query
$klantcases = new WP_Query( $args );
// The Loop
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post();
echo "<a href=".get_the_permalink().">";
echo get_the_post_thumbnail( null, $size = 'post-thumbnail');?><br><?php
echo the_title();?><br><?php
echo "</a>";
}
} else {
echo "no posts found";
}
// Restore original Post Data
wp_reset_postdata();
Now there is probably many ways to improve this loop but as mentioned im very new to all of this. That said i'd love to hear how you guys would resolve this issue.
Define empty variable on top of while loop.
And in loop concatenate your html with that empty variable.
For Example.
$output = '';
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post();
$output .= '<a href="'.the_permalink().'">';
$output .= the_post_thumbnail() .'<br>';
$output .= the_title();
$output .= '</a>';
}
} else {
echo "no posts found";
}
echo $output;
You don't need to echo the_title() - it already echoes out, so I'm guessing that's where part of the problem is coming in. You can also make your loop a little more simple.
You don't need the 'post_status' argument, as publish is default.
In your loop, let's use WP's built in echo functions: the_permalink(), the_title() and the_post_thumbnail(). You don't have to pass any arguments to the the_post_thumbnail() because you are just calling the default in your code.
<?php
// WP_Query arguments
$args = [
'post_type' => ['klantcase'],
'nopaging' => TRUE,
'order' => 'ASC',
'orderby' => 'menu_order',
];
// The Query
$klantcases = new WP_Query( $args );
// The Loop
if ( $klantcases->have_posts() ) {
while ( $klantcases->have_posts() ) {
$klantcases->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?><br>
<?php the_title(); ?>
</a>
<?php
}
} else {
echo 'no posts found';
}
// Restore original Post Data
wp_reset_postdata();

WordPress - Merging two complex queries into one

I have two fairly complex WordPress queries that I need to combine into a single query.
In my first query, I am getting Wordpress Posts by the date they are published. Each day of posts is contained inside a container, with the date showed above the posts as a title. It also only hides any posts that are in the past. Only posts from today or the future are shown.
In my second query, I am getting WordPress Posts from a Custom Taxonomy. For each term in the taxonomy, posts are displayed inside a container utilizing the slug of that term as a class, and displaying the name of the term as the title.
What I need to accomplish and what I need help with, is as follows.
I need the first query exactly as it is now, however, in the first query, where it says "// Need code here to display posts by taxonomy term //", I need to integrate the second query so that where posts are being output for that day, its also listing the posts by the term like in the second query.
Both queries function perfectly on their own, but I am having trouble with implementing a single query that utilizes the functionality from both queries to do what I need to do.
Here are my two queries:
1st Query:
<?php
// First we get yesterdays date
$year = date("Y"); $month = date("m"); $yesterday = date("d", strtotime("-1 day"));
// Then we construct a query to get items from the calendar, either published or scheduled for a future date so that they appear on the calendar. We use date query to hide events that have already passed by querying after the date we got before. We use yesterdays date to ensure that TODAYS post are still obtained
$query = new WP_Query(array('post_type' => 'calendar',
'post_status' => 'publish,future',
'orderby' => 'post_date',
'order' => 'DESC',
'date_query' => array(
array('after' => array(
'year' => $year,
'month' => $month,
'day' => $yesterday
)
)
)
));
// This is a special loop that gets posts by day and encases each days worth of posts in a container and shows the date of those posts. jQuery will be applied to this
if ($query->have_posts()) {
echo '<div class="day-posts">';
while ($query->have_posts()) {
$query->the_post();
echo '<div class="day">';
the_date('l jS F Y', '<div class="title"><div>', '</div>'); //Formats date, before echo, after echo
echo '<div class="posts clearfix">';
echo '<div class="post">';
the_title('<div class="title">', '</div>');
echo '<div class="content">';
// Need custom code here to display posts by taxonomy //
the_content();
echo '</div></div></div></div>';
}
echo '</div>';
}?>
2nd Query:
<?php
$post_type = array('calendar');
$tax = 'event-category';
$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$args = array(
'post_type' => $post_type,
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => - 1,
'caller_get_posts' => 1
); // END $args
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
echo '<div class="' . $tax_term->slug . '">';
echo '<div class="title">' . $tax_term->name . '</div>;
while ($my_query->have_posts()) : $my_query->the_post();
?>
<?php the_title();?>
<?php the_content();?>
<?php endwhile; echo '</div>'; } wp_reset_query(); } } ?>
I hope I have done a good job of explaining what I am trying to do. If I have not, or there are questions, please ask and I'll answer as quick as I can.
Ok here you go (I dont have posts setup like your query so i didn't test it but i believe this what you are looking for):
// Setup the basic structure:
$args = array(
'post_type' => 'calendar',
'post_status' => 'publish,future',
'orderby' => 'post_date',
'order' => 'DESC',
'date_query' => array(
array(
'after' => array(
'year' => $year,
'month' => $month,
'day' => $yesterday
)
)
),
'posts_per_page' => -1,
'caller_get_posts' => 1
);
// For dynamic taxonomy term create a bootstrap tax_query
$tax = 'event-category';
$tax_terms = get_terms($tax, array('orderby' => 'id', 'order' => 'ASC'));
if ($tax_terms) {
$args['tax_query'] = array(
array(
'taxonomy' => $tax,
'field' => 'slug',
)
);
// Append the slugs to the tax_query terms
foreach ($tax_terms as $tax_term) {
$args['tax_query'][0]['terms'][] = $tax_term->slug;
}
}
// Your One and only QUERY
$my_query = new WP_Query($args);
For $tax_term->slug and $tax_term->name you could use get_the_terms() within the query loop like:
$terms = get_the_terms($post->ID, $tax);
$terms = array_slice($terms, 0);
$term = $terms[0];
echo $term->slug;
echo $term->name;
EDITED
See code comments for explanation/clarification
if ($query->have_posts()) {
$_tax = false;
$same_date = array(); // collection of same dates
$same_date_bool = true; // determine previous post date exist
echo '<div class="day-posts">';
while ($query->have_posts()) {
$query->the_post();
$date = get_the_date('l jS F Y');
$same_date_bool = true;
// Checking if post is displayed from the current date
// then this post should be appended inside to previous
// opened div.day tag
if (!empty($same_date) && in_array($date, $same_date)) {
$same_date_bool = false;
}
// Collecting all dates
$same_date[] = $date;
// If post is not in same date
// create new div element
if ($same_date_bool) {
echo '<div class="day">
<div class="title">';
echo $date;
echo '</div>';
}
// If post is not in same date
// create new div element
if ($same_date_bool) {
echo '<div class="posts clearfix">';
}
echo '<div class="post">';
the_title('<div class="title">', '</div>');
echo '<div class="content">';
$terms = get_the_terms($post->ID, $tax);
// Note: I'm assuming your post will
// always have single term
$terms = array_slice($terms, 0);
$term = $terms[0];
if ($term) {
$_tax = true;
echo '<div class="' . $term->slug . '">';
echo '<div class="title">' . $term->name . '</div>';
}
the_content();
if ($_tax) {
echo '</div>';
}
echo '</div></div>';
// If post is in same date
// append closing div element for previous "div.day" element
if (!$same_date_bool) {
echo '</div></div>';
}
}
echo '</div>';
}
Hope this helps now. Also I have replaced the caller_get_posts with ignore_sticky_posts because caller_get_posts has been deprecated since v3.1

Wordpress Custom fields display file URL

I have the following code and need to show THREE things from my custom post type called fact-sheet.
The Title
The summary (fact_sheet_summary)
A file upload URL (fact_sheet_pdf_link)
I can get the first two to work but no idea how to do the third.
Basically my output should be...
The Title
The Summary paragraph
Click here to download as PDF
Any ideas how I can query to list all of these post type results? Is there a better way to do this rather than what I have below? The main problem is, I can't get the URL of the uploaded file.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'fact-sheet',
'order' => 'ASC',
));
if($posts)
{
foreach($posts as $post)
{
echo '<span class="fact-sheet-title">' . get_the_title($post->ID) . '</span><br />';
echo '<p><span class="fact-sheet-summary">' . the_field('fact_sheet_summary') . '</span></p>';
}
}
?>
I think It's better to use query_posts() as you can use The Loop default structure.
As for the custom fields (Using ACF Plugin), you're using the_field(), which automatically echoes the retrieved field value. You can use, in other hand, the get_field() function, which just returns the value of the field.
You could do something like this:
// Query all posts from 'fact-sheet' post_type
query_posts(array(
'numberposts' => -1,
'post_type' => 'fact-sheet',
'order' => 'ASC',
// Getting all posts, limitless
'posts_per_page' => -1,
));
// Loop throught them
while(have_posts()){
the_post();
echo '<span class="fact-sheet-title">' . get_the_title() . '</span><br />';
echo '<p><span class="fact-sheet-summary">' . get_field('fact_sheet_summary') . '</span></p>';
// Echos the link to PDF Download
echo '<p>Click here to download as PDF</p>';
}
// Once you're done, you reset the Default WP Query
wp_reset_query();
In case you might need further explanation about wp_reset_query(), check this out.
Can you try this? It's slightly modified from the manual of the WP Codex (not much)
<ul>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$args = array(
'post_type' => 'attachment',
'post_mime_type' => array('application/pdf'),
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<li>';
the_attachment_link( $attachment->ID, true );
echo '<p>';
echo apply_filters( 'the_title', $attachment->post_title );
echo '</p></li>';
}
}
endwhile; endif; ?>
</ul>
If it works, maybe it's better to modify a working sample to your needs - by adding your custom fields. Just my thoughts :-)

Categories