Post text breaks in post with [...] Wordpress development - php

I am receiving all posts from Wordpress but the text in the posts breaks like this:
Image of the line break
Here is a code snip:
<?php
global $post;
$args = array(
'posts_per_page' => 10,
'orderby' => 'date'
);
$postslist = get_posts( $args );
if($postslist) {
foreach ( $postslist as $post ) :
setup_postdata( $post );
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
$content = apply_filters('the_content', $post->post_content);?>
<h2><?php the_title(); ?></h2>
<p><?php the_date(); ?></p>
<img id="blogtemplate-image" src="<?php echo $url ;?>" />
<p><?php echo $content; ?></p>
<?php endforeach; }
wp_reset_postdata();
?>
How can I avoid the break and just getting all the content?
Thanks in advance!

I found the answer myself. Instead of getting the_excerpt() I have to use the_content(). The_excerpt return only 55 characters or so:
<?php
global $post;
$args = array(
'posts_per_page' => 10,
'orderby' => 'date'
);
$postslist = get_posts( $args );
if($postslist) {
foreach ( $postslist as $post ) :
setup_postdata( $post );
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<h2><?php the_title(); ?></h2>
<p><?php the_date(); ?></p>
<img id="blogtemplate-image" src="<?php echo $url ;?>" />
<p><?php echo the_content(); ?></p>
<?php
endforeach;
}
wp_reset_postdata();
?>

Related

Missing Attachment. Get all posts from Wordpress do not show the attachment

I'm new to Wordpress and i'm trying to get all posts out in my php file.
global $post;
$args = array('posts_per_page' => 10,
'order'=> 'ASC',
'orderby' => 'date'
);
$postslist = get_posts( $args );
if($postslist) {
foreach ( $postslist as $post ) :
setup_postdata( $post );
?>
<p><?php the_date(); ?></p>
<p><?php the_title(); ?></p>
<p><?php the_excerpt(); ?></p>
<?php the_attachment_link($post->ID, false); ?>
<?php
endforeach;
}
wp_reset_postdata();
?>
The title, date and description do come out but not the attachements. I need to show all posts also the posts with no attachements. Right now it says "Missing Attachment".
Thanks in advance :-)
You can try like this...
<?php
$args = array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>10, 'orderby'=>'date', 'order'=>'ASC');
$post_list = get_posts($args);
foreach($post_list as $post) {
$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<p><?php the_time('Y/m/d'); ?></p>
<p><?php echo $post->post_title;?></p>
<p><?php echo $post->post_excerpt;?></p>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<img src="<?php echo $url ;?>" />
<?php } ?>

Pagination returning the same posts on each page

Each page returns the same posts when you click "older posts". This was working fine at one time, but now it's not. Here is the code that I'm using:
<?php if ( have_posts() ) : ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 6, 'paged' => $paged, 'post_status' => 'publish', 'offset'=> 0, 'category'=> '-1, -9, -16, -17, -119, -318' );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div class="blog-post-wrap">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="post-thumb blog-index">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'large' );
}
?>
</div>
<div class="title-meta-wrap">
<?php if (strlen($post->post_title) > 75) {
echo substr(the_title($before = '', $after = '', FALSE), 0, 75) . '...'; } else {
the_title();
} ?>
</a>
<p><span class="theauthor"><?php the_date(); ?> <span style="color:#EC7906;"> / </span><?php the_author(); ?> </span></p>
</div>
</div>
<?php endforeach;
wp_reset_postdata();?>
<?php endif; ?>
<?php twentyeleven_content_nav( 'nav-below' ); ?>
I was able to remove this:
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
And go with just this:
get_posts( $args );
And pagination worked but the page only returned one post per page. Any thoughts?

if category empty return a message

i have this code to get some elements from wordpress
global $post;
$i=0;
$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
$myposts = get_posts( $args );
$has_posts = true;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<li>
<?php if($image){ ?>
<div class="news_left"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></div>
<?php } ?>
<?php
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
?>
<div class="news_right">
<h2><?php the_title(); ?></h2>
<span class="date">Date: <?php the_time('j/m/Y') ?></span>
<?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
Read More
</div>
<div class="clear"></div>
</li>
<?php $i++; endforeach; ?>
i need to put a condition to return text message if category is empty.like no posts to display , please note that the post use WPML as the language switcher
<?php
$myposts = get_posts( $args );
if($myposts){
//found posts
}else{
//no posts
}
?>
UPDATE: PLEASE check if the code works probably and THEN compare it with your code, i have commented on the changes i made, so its your chance to learn:
$i=0;
$args = array( 'numberposts' => 5, 'category' =>5,'order'=>'DESC','orderby'=>'post_date','suppress_filters' => 0 );
$myposts = get_posts( $args );
//check if $myposts
if(!$myposts){
//the $myposts has no posts, print the error message
echo "<li>";
echo "This category has zero posts";
echo "</li>";
}else{
//the category has one more or more posts
$has_posts = true;
foreach( $myposts as $post ) : setup_postdata($post); ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<li>
<?php if($image){ ?>
<div class="news_left"><img src="<?php echo $image[0]; ?>" alt="" width="191" height="132" /></div>
<?php } ?>
<?php
$content = apply_filters('the_content', get_the_content());
$content = explode("</p>", $content);
?>
<div class="news_right">
<h2><?php the_title(); ?></h2>
<span class="date">Date: <?php the_time('j/m/Y') ?></span>
<?php echo $content[1] . "</p>";//echo String::content_limit(200,'<p>'); ?>
Read More
</div>
<div class="clear"></div>
</li>
<?php
$i++; endforeach;
}
?>
Thank you so much!! This solved my problem..
ref: howlingwolfmedia.com/site3/classes/class-categories
see 'Personalized Training'
my code here:
`
<?php
$args=array(
'child_of' => 4,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => '0'
);
$categories=get_categories($args);
foreach($categories as $category) {
echo '<h3>' . $category->name . '</h3>
<div>';
global $post;
$args = array( 'posts_per_page' => -1, 'category' => $category->term_id, 'orderby' => 'name', 'order' => 'ASC' );
//alternatively this also works: 'nopaging' => true
$myposts = get_posts( $args );
if (!$myposts) {
echo 'Sorry there are currently no classes in this category';
}
else {
foreach( $myposts as $post ) : setup_postdata($post);
echo '<p>' . get_the_title($post->ID) . '</p>';
endforeach;
}
echo '</div>
';
}
?>
`

Get posts all posts by Tag / URL Wordpress

I'm looking to create a tag.php page, which displays all tags when the user clicks on the tag from a tag cloud. This is what I've got so far, but this seems to display all of my posts.
<article class="articles">
<?php
echo '<h2>Tag:';
$tag = single_tag_title();
echo '</h2>';
$args = array(
'taxonomy' => $tag,
'terms' => $tag,
);
$postslist = get_posts( $args );?>
<?php foreach( $postslist as $post ) : setup_postdata($post); ?>
<div class="clear"></div>
<span class="timestamp"><?php echo mysql2date('j M Y', $post->post_date) ;?></span></h2>
<p class="about"><?php the_title(); ?></p>
<?php the_content(''); ?>
<?php endforeach;?>
</div>
I cannot seem to figure this out, I've been googling it but I can't seem to find out the information I want...
Your single_tag_title it's not returning to variable:
$tag = single_tag_title('', false);
try with:
<?php
$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';
$args = array(
'taxonomy' => $tag,
'terms' => $tag,
);
$postslist = get_posts( $args );?>
<?php foreach( $postslist as $post ) : setup_postdata($post); ?>
<div class="clear"></div>
<span class="timestamp"><?php echo mysql2date('j M Y', $post->post_date) ;?></span></h2>
<p class="about"><?php the_title(); ?></p>
<?php the_content(); ?>
</div>
<?php endforeach;?>
Try using wp_query
$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';
// The Query
$the_query = new WP_Query( $tag );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$post = get_queried_object();
echo "<div class='clear'></div>
<span class='timestamp'>" . mysql2date('j M Y', $post->post_date) . " </span></h2>
<p class='about'" . the_title() . "</p>";
<?php the_content(); ?>
</div>
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Please note, I haven't tested this code!

Wordpress WP_Query

For some reason I cannot get attachment to display if i pass in $attachment_id if i pass a real value in like 187 it works.
I am using WpAlchemy and Custom Image Sizes plugin.
<section id="new">
<?php $myquery = new WP_Query(array('post_type' => array('post', 'website_gallery'),'showposts' => '2'));
while ($myquery->have_posts()) : $myquery->the_post();
global $custom_metabox;
?>
<div class="latest hentry">
<h2><?php the_title(); ?></h2>
<?php $website_gallery->the_meta(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php $website_gallery->the_value('galleryimage');?>" alt="<?php the_title(); ?>">
</a>
<?php echo wp_get_attachment_image($attachment_id, '220x80'); ?>
</div>
<?php endwhile; wp_reset_query(); ?>
</section>
I think you can get the attachment id by using the get_post() function. get_post() requires an array for it's parameter and that array can be something like this:
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );
Since you want attachments, make sure your 'post_type' => 'attachment'.
You can then do:
$attachments = get_post( $args );
if( $attachments ) {
foreach( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, '220x80' );
}
}
Adapt that code for what you need. Hopefully it'll work for you.
Check out: http://codex.wordpress.org/Template_Tags/get_posts#Show_attachments_for_the_current_post

Categories