How to retrieve Posts in WordPress into a custom HTML Table? - php

My question is How can I create two rows with 2 columns <td> in a Dynamic way, receiving posts from the query_post()!
I spent sometime using the query_post() in WordPress to create this :
I made a drastic mistake and this is the snippet:
http://pastebin.com/cp6RSTQQ
this gives me the recent posts of a category called watch using the offset in the query_post()
in this way:
<?php
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC','category' => 'watch', 'orderby' => 'post_date','offset' => 0 );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<td class="leftBoxes">
<div class="imgMargin"> <?php the_post_thumbnail(); ?> </div>
<br>
<div class="boxScrollsBlogsView">
<h2><?php the_title(); ?> </h2>
<P>
<?php the_excerpt(); ?>
</P>
<h3> ReadMore</h3>
</div>
</td>
<?php endforeach; ?>
After I wanted to go forward, I realized that I can't do pagination in the way I wrote th code!
I have looked at this QUESTION on wordpress website, but I can't understand how he is doing it honestly!

Just deleted the the right box and I changed the code to look like this
<?php
$args = array( 'posts_per_page' => 4, 'order'=> 'DESC','category' => 'watch', 'orderby' => 'post_date','offset' => 0 );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<td class="leftBoxes">
<div class="imgMargin"> <?php the_post_thumbnail(); ?> </div>
<br>
<div class="boxScrollsBlogsView">
<h2><?php the_title(); ?> </h2>
<P>
<?php the_excerpt(); ?>
</P>
<h3> ReadMore</h3>
</div>
</td>
<?php endforeach; ?>
and it creates one TD after another eventually it goes to the new line for making rows

Related

Show recent blog posts, but exclude current post

I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>

Modification of category template in WordPress using PHP

I am having some trouble building the category template i want. Here is an example of one of my categories: http://transcorrect.bg/pi-de/category/versicherungen/
I want this field http://prntscr.com/k2d5xg to show posts only from the current category. I am attaching the code i am using right now, but it is only for specific category. What i want is to be on archive.php file as a code, that takes dynamically the category ID.
Minimal example of code
<div class="row">
<div class="headline-box" style="margin-bottom:10px;">Weitere Artikel</div>
<?php
// get the top-category posts
$the_query = new WP_Query(array(
'category_name' => 'Versicherungen',
'posts_per_page' => 10,
));
if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div id="list-news-home" class="col-lg-12" style="padding-top:10px;padding-bottom:10px;">
<img src="http://transcorrect.bg/pi-de/wp-content/uploads/2018/07/arrow1.png" style="padding-right:20px;"><?php the_title(); ?>
[ mehr ]
</div>
<hr style="margin-top:50px;margin-bottom:10px">
<?php endwhile; ?>
<?php endif; ?>
</div>
I can't figure out how to get it so if someone has made something similar i will be thankful to know how :)
I have worked it out.
If anyone needs similar thing i am posting how i converted the code i posted earlier.
<div class="row">
<div class="headline-box" style="margin-bottom:10px;">Weitere Artikel</div>
<?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(); ?>
" style="color:#00315C">
" style="float:right"> [ mehr ]
<?php endwhile; ?>
<?php endif; ?>

Break up posts per page in two sections

I have a custom post type called 'videos' and I am using the Advanced Custom Fields plugin with a select field to use as a filter to assign where the post part will display on my page. I have two columns, one called "our work' and one called 'featured films'. I need each section to display the latest 4 posts but when I change posts_per_page it affects the total number, is there a way to limit it to just 4 per query? Sub question is it ok to have the same query run two times? My code is :
<div class="triple ourWork col-sm-6">
<h2>Our Work</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'posts_per_page'
=> 4) ) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); /* start the loop */ ?>
<?php if( get_field('labeled_as') == 'our work' ): ?>
<div class="col-sm-6" id="">
<?php global $post;
$gethref = $post->post_name;
?>
<div class="holder" style="background-image: linear-gradient(0deg,rgb(38, 38, 42, .5),rgb(38, 38, 42, .5)), url(<?php echo the_field('screenshot'); ?>);"><span class="play"><?php the_title(); ?></span></div>
<p><?php echo the_field('issue_short_description'); ?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
<div class="triple featuredFilms col-sm-6">
<h2>Featured Films</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'posts_per_page' => 4 ) ) ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); /* start the loop */ ?>
<?php if( get_field('labeled_as') == 'featured film' ): ?>
<div class="col-sm-6" id="">
<?php global $post;
$gethref = $post->post_name;
?>
<div class="holder" style="background-image: linear-gradient(0deg,rgb(38, 38, 42, .5),rgb(38, 38, 42, .5)), url(<?php echo the_field('screenshot'); ?>);"><span class="play"><?php the_title(); ?></span></div>
<p><?php echo the_field('issue_short_description'); ?></p>
</div>
<?php endif; ?>
<?php endwhile; ?>
</div>
You want to use meta queries for acf fields. https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
I personally would use 2 variables for the 2 loops for debugging, etc.
$work_loop = new WP_Query(
array(
'post_type' => 'videos',
'posts_per_page' => 4,
'meta_key' => 'labeled_as',
'meta_value' => 'our work'
)
);
This will fetch only posts that have that label. The way you are doing it currently, you get 4 results then filter for the label. So if 3 are in the other label, you will only show 1 result. This gets you 4 that will show. Just change the meta value for the second loop and you should be good to go.

Loop inside content-single

I try to build an option for the user to highlight "related posts" to be shown at the bottom of a single post, so I put this code at the bottom of "content-single.php":
<div class="related-post">
<?php
$args = array( 'post__in' => array(38328, 38359, 21321) );//it's going to be the user choice, for example, I put three id's
$related_query = new WP_Query($args);
if ($related_query):
echo '<div class="row">';
while ($related_query->have_posts()): $related_query->the_post();?>
<div class="col-sm-4">
<?php
if (has_post_thumbnail()):
the_post_thumbnail();
endif;
?>
<?php the_title() . '<br/>'; ?>
</div>
<?php endwhile;
echo '</div>';
wp_reset_postdata();
?>
</div>
But in some posts, I get 8 diefferent posts, in some posts I don't get any posts (though I put only three id's in post__in).
I guess the problem is that I try to loop inside the main loop.
I tried with query_posts and with get_posts, doesn't work.
Any help will be appreciate!
I think that is related to the missing post_type argument. Yet you need some corrections in your loop structure:
<div class="related-post">
<?php
$args = array( 'post_type' => 'your_post_type', 'post__in' => array(38328, 38359, 21321) );
$related_query = new WP_Query($args);
if( $related_query->have_posts() ){ ?>
<div class="row"><?php
while( $related_query->have_posts() ){ $related_query->the_post();?>
<div class="col-sm-4"><?php
if( has_post_thumbnail() ) the_post_thumbnail();
the_title();?>
</div><?php
}
wp_reset_postdata();?>
</div><?php
}?>
</div>
UPDATE
Please try to add 'posts_per_page' => 3 to the args array.
I have a solution to this post. Hope so you are wrong with the query post.
<div class="related-post">
<?php
$args = array(
'post_type' => 'your_post_type',
'post__in' => array(38328, 38359, 21321),
'post_status'=>'publish'
);
$related_query = new WP_Query( $args );
?>
<?php if ( $related_query->have_posts() ) : ?>
<div class="row">
<?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
<div class="col-sm-4">
<!-- do stuff ... -->
the_post_thumbnail();
the_title();
</div>
<?php endwhile; ?>
</div>
<?php endif; wp_reset_postdata(); ?>
</div>
Hope so it helps you to fetch out your Output Required.
Make sure you have post in the ID that you have mentioned in the post_in section.
Well, I manage to solve this by adding this 'ignore_sticky_posts' => 1 to the arguments. thanks #Pieter Goosen

Post Type Error

I added a code in my php and i don't know what is wrong, it shows only 2 posts but I got 10 posts in my WordPress can someone check my code?
<?php
$args = array(
'post_type' => 'post',
'category_name' => 'appetizers',
'post_status' => 'publish'
);
$Loop = new WP_Query( $args );
?>
<div class="row">
<?php while ( $Loop->have_posts() ) : $Loop->the_post();?>
<a href="<? the_permalink(); ?>">
<div class="col-sm-3">
<?php the_post_thumbnail(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php the_field('price'); ?>
</div>
</a>
<?php endwhile; ?>
</div>
add 'posts_per_page' => -1 in $args.
May be it is picking up the number of posts from the 'Setting -> Reading' option in back end.

Categories