I have a custom wordpress function that shows the number of featured recipes. The function works fine when showposts=-1 is in the query. However when I put 'showposts=5' only two of my posts are shown.
Below is my function
function pika_featured_recipes_shortcode() {
ob_start();
echo '<div class="featured-box-heading"> Featured Recipes </div>';
echo '<div class="featured-box">';
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5&post_type=cp_recipe&order=Desc&orderby=date' . '&paged=' . $paged);
while ($wp_query->have_posts()):
$wp_query->the_post();
$entry_id = get_the_ID();
$entry_link = get_permalink($entry_id);
$entry_image = get_post_thumbnail_id($entry_id);
$entry_title = get_the_title($entry_id);
$entry_description = get_post_meta($entry_id, '_cp_recipe_short_description', true);
$entry_excerpt = get_post_meta($entry_id, '_cp_recipe_excerpt', true);
$likes = get_post_meta( $entry_id, '_cp_likes', true );
if (get_field('featured-pika-recipe') == 'Yes') {
echo '<div class="featured-result-box item ">
<div class="box">
<div class="pika-featured-box-img">';
if(!empty($entry_image)) {
echo ''.wp_get_attachment_image($entry_image, 'cp_298_192').'';
}
echo' </div><!-- /.box-img -->';
echo'<div class="box-entry">
<h5 class="pika-featured-title"><a href="';
echo $entry_link;
echo '">';
echo $entry_title;
echo'</a></h5>';
echo $trimmed = wp_trim_words( $entry_description, $num_words = 15, $more = null );
echo'</div><!-- /.box-entry -->';
echo'</div>';
echo'</div>';
echo'<div style="clear:both"></div>';
}
endwhile;
wp_reset_query();
echo '</div>';
$output = ob_get_clean();
return $output;}
add_shortcode( 'pika_featured-recipes', 'pika_featured_recipes_shortcode' );
The problem seems to be with
if (get_field('featured-pika-recipe') == 'Yes') {
removing this and the number of posts appear fine. Any way i can resolve this?
maybe because only 2 posts ( of the last 5 posts that you actually call ) got
get_field('featured-pika-recipe') == 'Yes'
you can try to add this custom field directly to your query to get last 5 posts with this custom field with following code
'meta_key' => 'featured-pika-recipe',
'meta_value' => 'Yes'
Related
I know this question has been asked before. I checked through multiple answers on this site,
for example:
Wordpress loop with different bootstrap columns
https://wordpress.stackexchange.com/questions/222278/how-to-separate-posts-loop-in-to-two-columns/222281
... but I cannot work out how to integrate answers with my code (assuming that is possible).
I want to display a list of Categories and their related posts on a page.
The code I'm using works fine BUT displays the results in a single column down the page:
I want to split the display into 2 columns, like in the image below, if possible:
The code I'm using (currently placed in a new page template) is as follows:
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
get_footer();
?>
Wondering if anyone can help me to get this code to display loop results in 2 columns.
Many thanks.
UPDATE TO QUESTION
Karl, thanks for your answer. Your script works, but with a small problem:
The Categories/Related Posts display in 2 columns but a 'gap/space' appears in the middle of the display of data (see image below):
I added to your code slightly so I could display a custom field I inserted into each post. I'm not sure if this has caused the problem.
Altered code (changes are immediately after $query->the_post();):
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
?>
<div style="height:100px"></div>
<?php
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
echo "<div class='new-column'>";
$counter = 0;
foreach ( $categories as $category ) {
if($counter % 4 == 0 && $counter !=0){
echo "<div class='new-column'>";
}
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$customfieldvalue = get_post_meta($post->ID, "PDF", true);
?>
<p><a href="<?php echo $customfieldvalue; ?>" target="_blank"><?php
the_title(); ?></a></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
$counter++;
if($counter % 4 == 0){
echo "</div>";
}
} // End foreach
if($counter % 4 != 0){
echo "</div>";
}
get_footer();
?>
I've used bootstrap classes (row, col-6). Checked the size of categories array and used 2 variables - one as a counter and the other one to check if the column is first or second.
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
//get size of category
$catSize = sizeof($categories);
$j = 1;
$n = 1;
// Loop through categories
foreach ( $categories as $category ) {
if($n == 1){
echo '<div class="row">';
}
echo'<div class="col-6">';
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div></div>';
if($n == 1){
if($j == $catSize){
echo '<div class="col-6"></div>
</div>';
}
else{
$n = 2;
}
}
else{
echo '</div>';
$n =1;
}
$j++;
}
// Restore original Post Data
wp_reset_postdata();
} // End foreach
get_footer();
?>
Try this, I used a modulos operator "%" to group loops into 4, it will create new column every 4 loops. MAKE SURE YOU ADD CSS TO class new-column to arrange it like columns.
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
echo "<div class='new-column'">;
$counter = 0;
foreach ( $categories as $category ) {
if($counter % 4 == 0 && $counter !=0){
echo "<div class='new-column'">;
}
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
$counter++;
if($counter % 4 == 0){
echo "</div>";
}
} // End foreach
if($counter % 4 != 0){
echo "</div>";`enter code here`
}
get_footer();
?>
I'm working in Wordpress and implemented some PHP to display all categories on a page (by a shortcode). By clicking on a category it links to a new page displaying all posts of that very category.
How do I display only certain categories, for instance by id and/or name of the cateogry?
One post has two categories (a : A & B or A & C). So one post a always has one category A, and one category B or C.
Here's my code:
function swerft_categories( ){
ob_start();
$categories = get_categories();
echo '<div class="swerft_cat">';
foreach($categories as $category) {
echo '<div class="swerft_cat_single col-lg-3 col-md-3 col-sm-6 col-xs-12">';
echo '<div class="swerft_cat_single_inner">';
$thim_group_custom_title_bg_img = get_term_meta( $category->term_id, 'thim_group_custom_title_bg_img', true );
if ($thim_group_custom_title_bg_img) {
$image_id = $thim_group_custom_title_bg_img['id'];
if ($image_id) {
$post_thumbnail_img = wp_get_attachment_image_src( $image_id, 'full' );
echo '<img src="' . $post_thumbnail_img[0] . '" alt="' . $category->name . '" />';
}
}
echo '<h5>'. $category->name .'</h5>';
echo '<p>'. $category->description . $category->count . '<span> Seminare </span>' . '</p>';
echo '</div>';
echo '</div>';
}
echo '</div>';
return ob_get_clean();
}
add_shortcode( 'swerft_categories', 'swerft_categories' );
I tried this in the first few lines for example with no success:
function swerft_categories($args){
ob_start();
$args = array('hide_empty'=> 1,
'name' => 'B');
$categories = get_categories($args);
1) I want only one certain relation to be displayed. Let's say: only the relation of a : A & B
2) I want the count to only show the amount of posts based on the relation above.
3) By clicking on a category based on this relation, I want only those posts to be displayed of course.
I was able to find a solution for the above mentioned problem together with a colleague. See the code below, I hope it may help someone maybe; and thanks for anyone considering this task though ;)
// Function to only show individual seminars after click on category on category-page
function swerft_filter_posts_open_individual_seminare( $query ) {
$offene_seminare = isset($_GET['offene_seminare']) ? boolval($_GET['offene_seminare']) : false;
$individual_seminars_category_id = 91;
if($query->is_category() && $query->is_main_query()) {
if ($offene_seminare) {
$query->set( 'category__not_in', $individual_seminars_category_id );
} else {
$query->set( 'category__in', $individual_seminars_category_id );
}
}
}
add_action( 'pre_get_posts', 'swerft_filter_posts_open_individual_seminare' );
// Function to only count individual seminars in overview
function swerft_count_individual_seminars_in_category($category_id) {
$individual_seminars_category_id = 91;
$query_args = array(
'post_type' => 'post',
'category__and' => array($individual_seminars_category_id, $category_id)
);
$query = new WP_Query( $query_args );
$count = $query->found_posts;
return $count;
}
Hello there good programmers, I have 3 different sections in my webpage. I am using a wordpress loop to post all the post in wordpress. my problem is, i want to assign every post in the other sections or something conditions ?, for example.
For example i have 3 different sections
i have a section id named offer, i want to display the post that is limited only for my section offer page.
<section id = "offer">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category( '3' ) ) : ?>
<div class="post-cat-three">
<?php else : ?>
<div class="post">
<?php endif; ?>
</section>
next section is the section id named projects, i want to display a post that is intended for my projects sections only. and put in inside my section named
<section id="projects">
</section>
The last section is the contact section where i put my contact details and other informations, and i want it to be assigned in contact section.
<section id = "contact">
</section>
You can do it as #Omar Faruque says or do one single query, then store every section content on a variable and then do the rest of the markup, like this:
<?php
$args = array(
//Category Parameters
'category__in' => array(1, 2, 3), //Query only the posts on the categories with IDs 1, 2, 3
//Type & Status Parameters
'post_type' => 'post',
);
$query = new WP_Query( $args );
$category1_posts = "";
$category2_posts = "";
$category3_posts = "";
if($query->have_post()) {
while($query->have_posts()) {
$query->the_post();
$thumbnail_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) );
if(in_category(1)) {
$category1_posts .= '<div class="category1_post">';
$category1_posts .= '<h2>' . get_the_title() . '</h2>':
$category1_posts .= '<img src="' . $thumbnail_url . '">';
//Same for other elements you want here, the_excerpt for example
$category1_posts .= '</div>';
} else if (in_category(2)) {
$category2_posts .= '<div class="category2_post">';
$category2_posts .= '<h2>' . get_the_title() . '</h2>':
$category2_posts .= '<img src="' . $thumbnail_url . '">';
//Same for other elements you want here, the_excerpt for example
$category2_posts .= '</div>';
} else {
$category3_posts .= '<div class="category3_post">';
$category3_posts .= '<h2>' . get_the_title() . '</h2>':
$category3_posts .= '<img src="' . $thumbnail_url . '">';
//Same for other elements you want here, the_excerpt for example
$category3_posts .= '</div>';
}
}
}
wp_reset_query();
//Then just place the sections and echo the posts you have in your variables
?>
<section id="category1">
<?php echo $category1_posts; ?>
</section>
<section id="category2">
<?php echo $category2_posts; ?>
</section>
<section id="category3">
<?php echo $category3_posts; ?>
</section>
Best is make 3 different query for 3 section like below
<?php
/**
* The WordPress Query class.
* #link http://codex.wordpress.org/Function_Reference/WP_Query
*
*/
$args = array(
//Category Parameters
'category__in' => array(1, 2),
//Type & Status Parameters
'post_type' => 'post',
//Order & Orderby Parameters
'order' => 'DESC',
'orderby' => 'date',
//Pagination Parameters
'posts_per_page' => 10
);
$query = new WP_Query( $args );
if($query->have_post()):
while($query->have_posts()): $query->the_post();
// Wirte your html here
endwhile;
endif;
?>
I am trying to make a simple loop through my wordpress posts but when it comes to output the whole thing the loop puts it anywhere but not where I said, it should be. For example the loop adds breaks and but they are not in my posts. I had this problem once but dont know anymore what i have done to get over this problem.
MY LOOP:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 4
);
$loop = new WP_Query($args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$post_id = get_the_ID();
$content = the_content();
$headline = get_post_meta($post_id, 'headline', true);
$subtitle = get_post_meta($post_id, 'untertitel', true);
$class = wp_get_post_terms( $post_id, 'post_tag', $termsargs );
$bgimage = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo '<section class="section zutaten '.$class[0].'" style="background-image: url('.$bgimage.')">';
echo '<article class="zutaten-beschreibung">';
echo '<header>';
echo '<h2><strong>'.$headline.'</strong></h2>';
echo '<h3>'.$subtitle.'</h3>';
echo '<div class="zutaten-beschreibung-content">';
echo $content;
echo '</div>';
echo '</header>';
echo '</article>';
echo '</section>';
endwhile;
} else {
echo __('Fehler!');
}
wp_reset_postdata();
?>
RESULT:
What is going wrong here?
The function the_content() is printing the data. you should use get_the_content() instead.
I can see what's going wrong here. The echo $content; line is printing improper HTML. I know this because you've said in your caption there are many <p> tags where they should not be; and you're not (visibly) printing any <p> tags in this block of code.
Investigate what this method is returning: $content = the_content(); That should do the trick.
I want to create a slider but i can't get the post excerpt working. The title and permalink are working but the excerpt won't show... here is my code:
// Args
$args = array(
'cat' => $categories,
'post_type' => 'post',
'posts_per_page' => $amount,
);
// Our incrementing counter, leave this set to 0
$counter = 0;
// Query the posts based off of the parameters in the $args variable
$bs_posts = new WP_Query( $args );
// Our calculated limit of how many posts we have to work with, don't change this
$bs_posts_limit = count( $bs_posts->posts ) - 1;
// How many posts to display in each slide of the SlideDeck
$per_page = !empty($this->options['per-slide']) ? $this->options['per-slide'] : '2';
// Add "active" class to 1st slider item
$j=1;
// Carousel/Slider Html
echo "$tab<div" . (!empty($this->options['id']) ? ' id="slider-' . trim($thesis->api->esc($this->options['id'])) . '"' : '') . ' class="carousel slide'. (!empty($this->options['c-type']) ? ' ' . trim($thesis->api->esc($this->options['c-type'])) : '') .''. (!empty($this->options['class']) ? ' ' . trim($thesis->api->esc($this->options['class'])) : '') . "\" data-ride=\"carousel\">\n".
"$tab<h2 class=\"text-shadow\">". stripslashes($this->options['intro']) ."</h2>\n".
"$tab<div class=\"carousel-inner\">\n";
?>
<?php foreach( $bs_posts->posts as $bs_post ): ?>
<?php
// Variables
$slide_number = floor( $counter / $per_page ) + 1;
$slide_mod = $counter % $per_page;
$bs_post_ID = $bs_post->ID;
$bs_post_title = get_the_title( $bs_post_ID );
$bs_post_link = get_permalink( $bs_post_ID );
$bs_post_excerpt = get_the_excerpt( $bs_post_ID );
?>
<?php if( $slide_mod == 0 ): ?>
<div class="item <?php if($j <= 1) {echo 'active';} ?>">
<?php endif; ?>
<div class="single">
<?php if (has_post_thumbnail( $bs_post->ID )) the_post_thumbnail( 'thumbnail', array( 'title' => "#htmlcaption") ) ?>
<?php echo $bs_post_title;?>
<?php echo $bs_post_excerpt; ?>
Read More
</div>
<?php if( $slide_mod == ( $per_page - 1 ) || $counter == $bs_posts_limit ): ?>
</div>
<?php endif; ?>
<?php $counter++; $j++;?>
<?php endforeach; ?>
<?
echo "$tab</div>\n".
"$tab</div>\n";
?>
<?
I want to create a slider based on bootstrap carousel plugin. Actualy i want to appear 2-3 or more posts per slide as you see and I succeded that but the slide only shows the post title and the post permalink. The excerpt and image won't work so please help me
After looking at the docs, I see that get_the_excerpt() does not take a post id parameter. It must be used within the lop.
http://codex.wordpress.org/Function_Reference/get_the_excerpt
If you want to get the excerpt outside of the loop, you will have to get the full content of the post and truncate it yourself.