Wordpress category else-condition not working - php

I'm trying to setup a way to display related posts by category and exclude current one, but when a category has only one post, my current code to display anything else, is not working.
This is the code that I have so far. Any ideas why?
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content();?>
<?php $current_post_id = get_the_ID(); $post_cats = wp_get_post_categories( get_the_ID(), NULL ); ?>
<?php query_posts('showposts=8&orderby=desc&cat='.implode(',',$post_cats)); if (have_posts()) : ?>
<?php $i=0; while (have_posts()) : the_post(); if($current_post_id==get_the_ID()) continue; $i++; ?>
<span class="related-title"><?php short_title('...', 43); ?></span>
<?php endwhile;?>
<?php else : ?>
<p>Sorry! There is no more related posts in this category.</p>
<?php endif; wp_reset_query(); ?>
<?php endwhile;endif ?>
<div style="clear:both;"></div>
<?php get_template_part('includes/loop-tips');?>

Related

Post content not showing

I have this code that works perfect:
<?php
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('posts_per_page=5' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<span><?php the_date('m/d/y'); ?></span>
<button>Read More</button>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
I want to display an excerpt of the post content between the date and button. So I added <p><?php the_content(); ?></p>. Didn't work. I tried <p><?php get_the_content(); ?></p>. Still nothing.
I tried putting the <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> loop around all of the HTML and just the paragraph. Both yielded nothing.
How do I get the excerpt to display?

Manually show specific posts on homepage

To loop and show all WordPress posts we use:
<?php
if( have_posts() ) :
while( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endwhile;
endif;
?>
And to show only the first recent post we use:
<?php
if( have_posts() ) :
if( have_posts() ) : the_post(); ?>
<p><?php the_content(); ?></p>
<?php
endif;
endif;
?>
My question is, can I manually select what posts I want to show on my homepage without doing a loop? For example let's say I want to only show post1, post3, and post6 on my homepage, can I do that?
Without using while loop because the post contain only one post.
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=>40'); ?>
<?php if (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endif;?>
<?php get_footer(); ?>
Try this code
<?php /*
Template Name: Query Single Post
*/
get_header(); ?>
<?php query_posts('p=40'); ?>
<?php while (have_posts()) : the_post(); ?>
<h4><?php the_title(); ?></h4>
<?php the_content(); ?>
<?php endwhile;?>
<?php get_footer(); ?>
The one I picked had an ID of 40. So, I set p=40. On the page I chose to use this on, I selected the "Query Single Post" post template. Clicked save and refreshed my page.
You need to use pre_get_posts filter.
Code:
<?php
add_action('pre_get_posts', function($query){
if ( !$query->is_home() or !$query->is_main_query() )
return false;
# And here you can use any parameters from [WP_Query Class](https://codex.wordpress.org/Class_Reference/WP_Query)
# For example
$query->set('post__in', [10, 15, 16]);
});

Wordpress posts under content

How should look my code in order to display a preview of a categories with main main page?
Now its is
<?php $posts = get_posts ("category=2&orderby=date&numberposts=3"); ?>
<?php if ($posts) : ?>
<?php foreach ($posts as $post) : setup_postdata ($post); ?>
<div>
<?php the_title(); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
and below this
<?php if(have_posts()): ?>
<?php while(have_posts()): the_post(); ?>
<div class="pageContainer"><?php the_content(); ?></div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?><div>Sorry, posts are not found.</div>
<?php endif; ?>
This leads to ignore second part...
Sorry for my english.
<?php
// Category posts
$posts = get_posts("category=2&orderby=date&numberposts=3");
if ($posts){
foreach ($posts as $article){
echo '<div><a href="'.get_permalink($article->ID) ?>" rel="bookmark">'.
$article->post_title.'</a></div>';
}
}
// Current page content
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="pageContainer">
<?php the_content(); ?>
</div>
<?php endwhile;
else:
echo '<div>Sorry, posts are not found.</div>';
endif;
?>

query_post and have_posts issue

I have 20 posts and i need to display text after every 5 posts. So i have asked the query in stackoverflow and i have get the solution. I have tried the query in my site. My code is,
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
echo hai;
elseif($p==10):
echo fine;
endif;
?>
<?php
$p++;
?>
<?php
endwhile;
?>
It works fine when echoeing "Hai" and "Fine" after 5 posts.
BUt while replacing the code in the below format. The posts are not fetching correctly. Please anyone help me. I need to add my own category id 3 after 5th post and category id 4 after 10th post
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
if (have_posts()) :
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
elseif($p==10):
echo fine;
endif;
?>
<?php
$p++;
?>
<?php
endwhile;
?>
inplace of echo fine; i need to add the same code used above in place of echo hai;
Use wp_reset_query().
wp_reset_query() restores the $wp_query.
<?php
query_posts( array(orderby=>post_date, order=>desc) );
$p = 1;
while ( have_posts() ) : the_post();
?>
<div class="post">
<?php the_title(); ?>
<?php the_post_thumbnail(); ?>
</div>
<?php ($p%5 == 0) ? ($p/5): ""; ?>
<?php if($p==5):
if (have_posts()) :
wp_reset_query();
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title();
?>
<?php endwhile; ?>
<?php endif;
elseif($p==10):
wp_reset_query();
if (have_posts()) :
wp_reset_query();
query_posts( array(cat=>3, orderby=>post_date, order=>desc) );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
endif;
$p++
?>
<?php
endwhile;
?>

Wordpress: Check posts have comments allowed

I have a page that displays certain posts from a certain category, in this case category 33 (Tutorials) and it currently outputs the title, post excerpt and permalink to the posts in this category:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?. I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
try this one
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post();?>
<?php if(comments_open()){ ?>
<div class="news-row">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<div class="newsimagebox">
<?php //$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'thumbnail');
$images = the_post_thumbnail();?>
<?php echo $images;?>
</div>
<?php endif; ?>
<div class="news-content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt();?></p>
<div class="readmore">Read More</div>
</div>
</div>
<?php } ?>
<?php endwhile;?>
<?php endif; //wp_reset_query(); ?>
Thanks

Categories