Showing recent post from a specific category - php

I wanted to show post from just recent post from a specific categories
so far this is what I have but:
<ul>
<?php
$number_recents_post = 5;
$recent_posts = wp_get_recent_posts($number_recents_post);
foreach($recent_posts as $post){
echo '<li><a href="' . get_permalink($post["ID"]) . '" title="Look '.$post["post_title"].'" >' . $post["post_title"].'</a> </li> ';
} ?>
</ul>
I tried turning it into this but not working
<ul>
<?php
$number_recents_post = 5;
$recent_posts = wp_get_recent_posts($number_recents_post . 'cat=3,4,5');
foreach($recent_posts as $post){
echo '<li><a href="' . get_permalink($post["ID"]) . '" title="Look '.$post["post_title"].'" >' . $post["post_title"].'</a> </li> ';
} ?>
</ul>
Please let me know what am I doing wrong....

According to the Codex, you can't use wp_get_recent_posts() the way you do:
Parameters
$num
(integer) (optional) Number of posts to get.
Default: 10
Maybe codedude's example helps.

why don't you try this (assuming you are using Wordpress)
<?php query_posts('post_per_page=5&category_name=yourcategoryname'); ?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
<?php endwhile; else: ?>
<p>An error Message</p>
<?php endif; ?>

Related

WordPress Adding tags to loop content

I am wrapping my head on wordpress loop at the moment, im trying to give tags to the respective content, so an H tag to the title, an p tag to the excerpt and so on...
The code i got so far is
<div id="<?php echo $page_id; ?>" class="container"><!-- begin container -->
<div id="postovi" style="display:none;">
<?php $custom_loop = new
WP_Query('showposts=5&category_name=Zanimljivosti&orderby=rand');
if ( $custom_loop->have_posts() ) : echo '<ul>'; while ( $custom_loop->have_posts() ) : $custom_loop->the_post(); echo '<li>' . get_the_title() . get_the_post_thumbnail($loop->post->ID, 'shop_catalog') . get_the_excerpt();'</li>'; endwhile; wp_reset_query(); echo '</ul>';endif;?> </div>
any suggestions apreciated :)
So your solution is :
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Zanimljivosti&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ) :
$custom_loop->the_post(); echo '<li><h2><a href="' . get_permalink() . '">' . get_the_title();
echo '</h2>' . get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
echo'<p>' . get_the_excerpt();'</p></a></li>';
endwhile; wp_reset_query();
echo '</ul>';endif;
?>

Strange misbehaviour with chaining PHP Wordpress Post Loop

I am trying to simply output all posts but everything is working fine but I want to add the classes in the tag but the classes gets written in plain text not into the tag.
my code:
<?php
if (have_posts()) :
// Start the Loop.
while (have_posts()) : the_post();
echo '<article id="post-' . get_the_ID() . '" ' . post_class() . '>';
twentyfourteen_post_thumbnail();
the_title('<h1 class="entry-title">', '</h1>');
echo '<div class="entry-summary">';
the_excerpt();
echo '</div>';
echo '</article>';
endwhile;
endif;
?>
I think it has something to do with the chaining but I tried everything... :(
Replace
echo '<article id="post-' . get_the_ID() . '" ' . post_class() . '>';
with
echo '<article id="post-' . get_the_ID() . '" ';
post_class();
echo '>';

Display custom post type posts and meta data within the_content filter

I am using this in my mu-plugins.php file//
function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>
<figure>
<?php the_post_thumbnail('thummy'); ?>
<figcaption>
<h3><?php the_title(); ?></h3>
<span>Cool stuff brah.</span>
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
</figcaption>
</figure>
</li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');
and in my page template I used <?php the_content(); ?> to display everything.
UPDATE//
Full Page Template code
<div id="container" class="clearfix">
<div id="left-content">
<?php get_sidebar('two');?>
</div>
<div id="right-content">
<h1><?php wp_title(''); ?></h1>
<ul class="grid cs-style-3">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div><!--right-content-->
</div><!--container-->
<?php get_footer(); ?>
But I am recieving this error//
Parse error: syntax error, unexpected T_STRING in /home/xxx/public_html/domain.com/testing/wp-content/mu-plugins/must-use.php on line 15
I am trying this method of displaying content because I want to use the WP-Members plugin, and I realized that the plugin will only work for content within the_content().
So my question is how can I fix the code I posted above to display the title, thumbnail, links etc the correct way?
function new_default_content($content) {
global $post;
if ($post->post_type == 'textures') {
$content .='<li>';
$content .='<figure>';
$content .= the_post_thumbnail('thummy');
$content .= '<figcaption>';
$content .= '<h3>';
$content .= the_title();
$content .= '</h3>';
$content .= '<span>Cool stuff brah.</span>';
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>';
}
$content .= '</figcaption>';
$content .= '</figure>';
$content .= '</li>';
}
return $content;
}
add_filter('the_content', 'new_default_content');
Just try this code. If you still get same error we'll check.
FOUND A SOLUTION
I remembered that you can add content inside of shortcodes.
So I created my own shortcode [textures_content]
Then used the code pasted below to display the content within <?php the_content(); ?> function//
add_shortcode( 'textures_content', 'textures_shortcode' );
function textures_shortcode( $atts ) {
ob_start();
$query = new WP_Query( array(
'post_type' => 'textures',
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'date',
) );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li>
<figure>
<?php the_post_thumbnail('thummy'); ?>
<figcaption>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
</figcaption>
</figure>
</li>
<?php endwhile;
wp_reset_postdata(); ?>
<?php $myvariable = ob_get_clean();
return $myvariable;
}
}
Now the Members only plugin works as expected and the blocked content is displayed once logged in :)

return in shortcode wordpress

I have this code for displaying a shortcode.
<?php
function recent_posts_function() {
$mypost = array( 'post_type' => 'gallery_pictures', );
$loop = new WP_Query( $mypost );
?>
<div id="boxhover">
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<!--Fade-->
<?php $ddd = '<div class="mosaic-block fade">
<a href="'. $image[0] . '" data-fancybox-group="gallery" target="_blank" class="mosaic-overlay preview fancybox" title="' . the_title . '">
<div class="details">
<h4>' . the_title() . '</h4>
<p>' . the_content_rss('', TRUE, '', 30) . '</p>
<br/>
<div class="btt">VIEW</div>
</div>
</a>
<div class="mosaic-backdrop"><img src="' . $image[0] . '" alt="gallery thumbnail" /></div>
</div>';
endwhile; ?>
</div>
<?php
return $ddd;
}
function register_shortcodes(){
add_shortcode('gallery', 'recent_posts_function');
}
add_action( 'init', 'register_shortcodes');
as you can see from the above codes, the 'return $ddd' should return all the output from the loops that the 'while' process done but its display only one.
Im currently looking for a solution and would love to hear any suggestion, recommendations and ideas on how to do it. Thank in advance.
You need to add a [dot] before your = [equal] on the loop while.
This will cause each loop add content current with the previous.
<?php function recent_posts_function() {
$ddd = ''; //First declare the string var ?>
...
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php $ddd .= '<div class="mosaic-block fade">'; // Put a [dot] before sign symbol ?>
<?php endwhile; ?>
...
return $ddd;
...
<? php } ?>

Pagination not working properly on author and category archives

I've made a category page that has pagination on the bottom of it. Everytime i try to go to page 2, the URL structure changes but the posts from my loop do not. The pagination is working fine for the blog page with almost the exact same loop. Any hints to what I could be doing wrong? I am also using the Wp-Paginate plug in.
<ul class="blogpostings">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post();?>
<li>
<?php if ( has_post_thumbnail() ):?>
<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id, 'thumbnail', true);
echo '<img src="' . $image_url[0] . '" title="' . get_the_title($post->ID) . '" alt="' . get_the_title($post->ID) . '" />';
?>
<?php endif; ?>
<h2><?php the_title(); ?></h2><br/>
<h3><?php the_author_posts_link(); ?> <span class="date">| <?php the_time('F d, Y') ?> |</span> <?php comments_popup_link('Post Comment', '1 Comment »', '% Comments »'); ?>
<fb:like href="<?php the_permalink() ?>" layout="button_count" width="100" font="lucida grande"></fb:like>
</h3>
<?php the_excerpt(); ?>
<p class="postreadmore"> Read More </p>
</li>
<?php endwhile; ?>
</ul>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } else { ?>
<?php if(function_exists('wp_paginate')) {
wp_paginate();
} ?>
<?php } ?>
<?php endif; ?>
</div>
If you are using query_posts(), make sure you are including the paged parameter. The following should help: http://ivanasetiawan.com/wp-pagination-stays-on-the-same-page/

Categories