Wordpress posts under content - php

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;
?>

Related

How do I get the link of a current WordPress post while it is in the Loop?

Below is my loop:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_post_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
Get <?php get_post_permalink(); ?> should display the link yet this is what is being rendered. It is not displaying the permalink of the post
None of the other answers are correct. get_the_permalink() (you can use get_permalink() as well, since it's an alias) RETURNS the data, not ECHO. So, it will never be printed to the screen (most WP functions with get_ prefix work this way.)
You have two options:
Use get_permalink( get_the_ID() ) pass the current post id (if not in the loop) and echo it.
Use the_permalink() which will echo out the permalink (in the loop);
the_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php the_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
get_permalink():
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php echo get_permalink(); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
This will echo out the URL, but will not make the link clickable - you need to add it to an <a> tag:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post(); ?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
Click Here
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
endif; ?>
If you want to get Post Permalink you should use get_permalink($post_id)
Wordpress get_permalink function Reference
Please try this:
<?php if (have_posts()):
// This function belowm is responsible for iterating through the posts
while (have_posts()): the_post();
$id = get_the_ID();
?>
<div class="col-md-4">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_the_permalink($id); ?>
<?php edit_post_link(); ?>
</div>
<?php
endwhile; ?>
<?php
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 category else-condition not working

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');?>

Call top posts in different divs

I want to have my latest post show up with all info(title, author, thumbnail, and content) but only the title of the second most recent post in another div. Here is my code. It renders the divs correctly but the 'title' in the second div is the 'title' of the latest post still.
<div id="blog-pane">
<div id="blog-post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-title">
<?php the_title(); ?>
</div>
<div id="post-author">
<?php the_author(); ?>
<?php the_date(); ?>
<?php the_time(); ?>
</div>
<div id="post-image">
<?php the_post_thumbnail(); ?>
</div>
<div id="post-text">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php rewind_posts(); ?>
</div>
<div id="post-link-1">
<?php
query_posts( 'p' );
while ( have_posts() ) : the_post();
the_title();
endwhile;
wp_reset_query();
?>
</div>
</div>
</div>
<?php get_footer(); ?>
you can try to introduce some skip logic to have it skip the first post,
<div id="post-link-1">
<?php
query_posts( 'p' );
$count = 0;
while ( have_posts() ) {
if ($count++ == 0) {
the_post();
continue;
}
the_post();
the_title();
}
wp_reset_query();
?>
</div>

Wordpress Loop: how to wrap each 3 posts into a div?

I'm trying this:
<?php query_posts('cat=6'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div>
<?php $counter=3; ?>
<?php the_post_thumbnail(); ?>
<?php $counter++; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
But it's not working! :/
Thank you!
Thanks for your support guys! :)
I tried both solutions but didn't work,
I ended up with this and works perfectly!
<?php query_posts('cat=6'); ?>
<?php $variable=0;?>
<div>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(($variable+1)<4){ ?>
<a href="<?php echo get_post_meta($post->ID, 'colaborador-link', true); ?>" target="blank">
<?php the_post_thumbnail(); ?>
</a>
<?php $variable+=1; ?>
<?php }else{ ?>
<?php $variable=1; ?>
</div>
<div>
<a href="<?php echo get_post_meta($post->ID, 'colaborador-link', true); ?>" target="blank">
<?php the_post_thumbnail(); ?>
</a>
<?php }?>
<?php endwhile; ?>
</div>
I haven't tested it with wordpress so i'm not 100% sure it'll work.
The idea is to use the modulus operator (see an example that matches your needs here http://codepad.org/78d2aAKp)
<?php query_posts('cat=6'); ?>
<?php if (have_posts()) : ?>
<!-- Your div starts here -->
<div>
<?php
while (have_posts()) :
the_post();
$counter = 0;
if($counter%3 == 0 && $counter > 0):
?>
<!--Close and then open the div-->
</div><div>
<?php
endif;
?>
<?php the_post_thumbnail(); ?>
<?php $counter++; ?>
<?php endwhile; ?>
</div><!--/Your div ends here -->
<?php endif; ?>
<?php query_posts('cat=6'); ?>
<?php if (have_posts()) : ?>
<?php $counter=0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php if($counter%3==0) : ?>
<div>
<?php $counter=3; ?>
<?php the_post_thumbnail(); ?>
<?php $counter++; ?>
</div>
<?php else:
//Some code for other posts..
endif;
?>
<?php $counter++; ?>
<?php endwhile; ?>
<?php endif; ?>

Categories