I have a loop through my custom post types.
I want to select 8 posts only, and print out the output into 2 columns, 4 posts each. I'm not really sure how to approach this. So far I have:
<?php
$args = array( 'post_type' => 'hh_event', 'posts_per_page' => 8 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="entry-date">';
$event_date = get_post_meta($post->ID, 'event_date', true);
echo date('M j',strtotime($event_date));
echo '</div>';
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
A simple solution would be to have your content stored in an array like so:
<?php
$args = array( 'post_type' => 'hh_event', 'posts_per_page' => 8 );
$loop = new WP_Query( $args );
$columns = array( '', '' );
$content = '';
$i = 0;
global $post;
while ( $loop->have_posts() ) : $loop->the_post();
$content = '<div class="entry-date">';
$event_date = get_post_meta($post->ID, 'event_date', true);
$content .= date('M j',strtotime($event_date));
$content .= '</div>';
$content .= get_the_title();
$content .= '<div class="entry-content">';
$content .= apply_filters( 'the_content', $post->post_content );
$content .= '</div>';
$columns[ ($i % 2) ] .= $content;
$i ++;
endwhile;
echo '<div class="column-left">' . $columns[0] . '</div><div class="column-right">' . $columns[1] . '</div>';
?>
So basically you have an array containing two empty strings. You then assign the content of each column to a variable named $content. You then append the value of that variable to the proper part of the $columns variable and increment the counter $i.
Then just echo the contents of each column to a proper wrapping <div> element, or however you are going to separate those into two columns.
Related
I want to create my own custom theme in Wordpress.
Before that, I created a custom post type and now I want to show them into an array.
For that, I use AJAX and at this time, there is no problem.
The problem comes when I generate the table in PHP, the values of the recovered post are put anywhere.
Here my table generator:
// Get paging system.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// Create post request arguments.
$args = [
'post_type' => 'wp-lightnovel',
'posts_per_page' => 5,
'paged' => $paged
];
if (isset($_POST['season'])) {
$season = $_POST['season'];
if ($season != '' && $season != 'all') {
$args['tax_query'] = [[
'taxonomy' => 'wp-lightnovel-release-season',
'field' => 'slug',
'terms' => $_POST['season']
]];
}
}
// Ask for posts.
query_posts($args);
$content = '<table>';
$content .= '<thead>';
$content .= '<tr><th colspan="2">The table header</th></tr>';
$content .= '</thead>';
$content .= '<tbody>';
while(have_posts()) {
the_post();
$image = get_the_post_thumbnail_url();
$releaseSeason = get_the_terms(get_the_ID(), 'wp-lightnovel-release-season')[0]->name;
$postTitle = the_title();
$content .= '<tr>';
$content .= '<td><div style="width: 100px"><img src="' . $image . '" /></div></td>';
$content .= '<td>' . $postTitle . '(<b>' . $releaseSeason . '</b>)</td>';
$content .= '</tr>';
}
$content .= '</tbody>';
$content .= '</table>';
echo $content;
die();
The problem is that the post title should be next to the release season but it's not the case as the render on the web:
I don't now is this happening.
If anyone has an idea ?
Thanks
The issue you are facing here is the following line;
$postTitle = the_title();
the_title() will echo the title of the post unless you set the 3rd parameter to be false as below [docs];
// 1st param = prepend | 2nd param = append | 3rd param = echo (bool true||false)
$post_title = the_title("", "", false);
You also have the option for get_the_title() [docs];
$post_title = get_the_title();
i have created shortcode to show all testimonial on one page.
i have created custom post type to add each testimonial.
can anybody tell me how to show all testimonial on single page .
this code is used to create sshortcode
below i m mentioning code :-
function fn_testimonials_block()
{
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC'
);
$the_query = new WP_Query( $args );
$pages = $the_query->posts;
//echo "<pre>";
//print_r($pages);
//echo "</pre>";
//exit;
$output = '';
$count = 1;
foreach($pages as $page) {
//-----------------
$author = $page->post_title;
$testimonial = $page->post_content;
$page_url = get_page_link( $page->ID );
$author_image = get_the_post_thumbnail_url( $page->ID, 'thumbnail' );
if ($count%2 == 1)
{
$output .= '<div>';
}
$output .= '<div>
<p>'.$testimonial.'<br>
<img src="'.$author_image .'"> <span>'.$author.', </span></p>
</div>';
if ($count%2 == 0)
{
$output .= '</div>';
}
$count++;
}
if ($count%4 != 1){
$output .= '</div>';
}
return $output;
}
I think you will have to pass 'posts_per_page' key in $args array. So your $args array will look like this
$args = array(
'post_type'=> 'testimonials',
'order' => 'ASC',
'posts_per_page' => '-1'
);
I am not sure about the $count variable you are using. I suppose you are trying to implement logic for pagination there but since your question is about showing all testimonials on single page, I won't get into that part.
Making changes in $args will return all testimonials objects.
I am adding filter for each tag in a specific category and getting all tags using below code now i am adding tabs to this system , the anchor tags are not working in echo
<?php
$dyno_cat = get_field( "category_slug" );
$query_args = array(
'category_name' => $dyno_cat,
'posts_per_page' => -1
);
$query = new WP_Query( $query_args );
$posttags = "";
while( $query->have_posts() ) {
$query->the_post();
if( get_the_tag_list() ){
$posttags = $posttags . get_the_tag_list('',',',',');
}
}
// Reset
wp_reset_postdata();
// Explode tags in array
$sortedtags = explode(',', $posttags);
// Sort array
asort($sortedtags);
// Remove duplicates from array
$sortedtags = array_unique($sortedtags);
// Remove the blank entry due to get_the_tag_list
$sortedtags = array_values( array_filter($sortedtags) );
// Wrap each tag in list element
echo '<ul class="nav nav-tabs" role="tablist">';
foreach ($sortedtags as $tagname) {
//echo '<li>' . $tagname . '</li>';
echo
'<li>
<a href="#'.$tagname.'" role="tab" data-toggle="tab">
'.$tagname.'
</a>
</li>';
}
echo '</ul>';
echo '<div class="tab-content">';
foreach($sortedtags as $tagname) {
echo '<div class="tab-pane" id="' . $tagname.'">';
$the_query = new WP_Query(array(
'post_status' => array('private'),
'posts_per_page' => -1,
'category_name' => $dyno_cat
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h1>';
the_title();
echo '</h1>';
endwhile;
}
echo '</div>';
?>
It seems posts are getting but the tabs are not working , check the attached screen
$tagname the tag name coming some thing like with below format ,
Americas
I have two loops running on my page one for getting one set of posts from a specific category and then further down getting posts from a custom post type but for some reason If I output both of them the second loop does not show and if I comment out the first the second one does show?
Im a bit confused as to why?
FIRST LOOP UPDATED
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li>'.get_the_title().'</li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
$post_query->reset_postdata();
}
?>
SECOND LOOP UPDATED
I didn't need one of the loops so I refined the code a bit further but still I am not having any luck with the second loop outputting.
<?php
$featureThumb = new WP_Query( array(
'post_type' => 'resources',
'meta_key' => 'file_upload',
'posts_per_page' => -1
));
while ($featureThumb->have_posts()) : $featureThumb->the_post();
echo '<div>';
if (has_post_thumbnail($post->ID)) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'homepage-thumb-thumb' );
echo '<img src="' . $thumb[0] . '" width="200" height="200" />' ;
};
echo '<p><a href="'. get_field('file_upload') .'" target="_blank" download>Click here to download as PDF</a></p>';
endwhile;
unset($featureThumb);
}
?>
It seems that you have three queries and 4 loops, not two.
Also, you have an extra } after the unset($featureThumb); that you should remove.
The three queries you posted in your question are:
$post_query, $loop and $featureThumb. At the last one your loop is incorect. It uses the object of the second one. Change at the third query in the loop, $loop with $featureThumb.
You need to use wp_reset_postdata() after the first two queries instead of wp_reset_query() as someone suggested and it should both work.
wp_reset_query() ensures that the main query has been reset to the original main query, while on the other hand wp_reset_postdata() makes sure that the global $post has been restored to the current post in the main query.
UPDATE:
If that does not work, if global $post object is not defined, try:
$post_query->reset_postdata();
$loop->reset_postdata();
UPDATE 2:
Your first query and loop should look like this:
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li>'.get_the_title().'</li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
$post_query->reset_postdata();
}
?>
You have to use wp_reset_query(); at the end of the first loop as well. So the first loop should be:
<?php
$post_query = new WP_Query(array( 'category_name' => 'email-content'));
if ( $post_query->have_posts() ) {
echo '<div class="tabs tabs_default">';
echo '<ul class="horizontal">';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo'<li>'.get_the_title().'</li>';
}
echo '</ul>';
while ( $post_query->have_posts() ) {
$post_query->the_post();
$title = strtolower(get_the_title());
$title = str_replace(' ', '-', $title);
echo '<div id="'.$title.'">';
echo '<div><button class="copy" id="'.$title.'" data-clipboard-text="'.get_the_content().'" title="Copy">Copy</button></div>';
echo '<div>'.get_the_content().'</div>';
echo '</div>';
}
wp_reset_query();
}
?>
I need to change some code from the WP-Bootstrap Wordpress Files.
In their code they remove the standard gallery code and replace it with their own which works fine accept it doesn't properly account for Rows.
function gallery_shortcode_tbs($attr) {
global $post, $wp_locale;
$output = "";
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
$output = '<div class="row-fluid"><ul class="thumbnails">';
foreach ( $attachments as $attachment ) {
$output .= '<li class="span3">';
$att_title = apply_filters( 'the_title' , $attachment->post_title );
$output .= wp_get_attachment_link( $attachment->ID , 'thumbnail', true );
$output .= '</li>';
}
$output .= '</ul></div>';
}
return $output;
}
could anyone advise on how to edit the above code to insert html code to close the row and start another after the 3rd item.. or any number i choose?
thank you in advance.
R
Take a look at the default WP gallery shortcode source, where a break is inserted after the specified number of columns, on lines 837-838:
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';