How to add read more in wordpress - php

I have tried many things but i didn't get Read more link in paragraph
page.php
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="inner-box-post">';
echo '<h2><a href="get_permalink();">';
the_title();
echo '</a></h2>';
echo '<div class="thumbnail">';
the_post_thumbnail( 'thumbnail' );
get_comments( $args );
echo '</div>';
echo '<div class="post-containt">';
echo '<div class="post-date"><strong>';
echo get_the_date();
echo '</strong> </div>';
echo the_excerpt(); ;
echo '</div> ';
echo '</div>';
endwhile; ?>
</div>
function.php
function new_excerpt_more($output) {
return $output . '<p>' . 'Read more &raquo' . '</p>';
}
add_filter('get_the_excerpt', 'new_excerpt_more');

Filter is excerpt_more,
add_filter('excerpt_more', 'new_excerpt_more');
Reference.

THIS IS NOT AN ANSWER, JUST CORRECTIONS
You really don't need to overuse echo in your code. Just use opening and closing php tags correctly. Rather use this. It also helps with readibility
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="inner-box-post">
<h2><?php the_title(); ?></h2>
<div class="thumbnail">
<?php the_post_thumbnail( 'thumbnail' ); ?>
<?php get_comments( $args ); ?>
</div>
<div class="post-containt">
<div class="post-date"><strong>
<?php get_the_date(); ?>
</strong> </div>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
</div>

<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="inner-box-post">
<h2><?php the_title(); ?></h2>
<div class="thumbnail">
<?php the_post_thumbnail( 'thumbnail' ); ?>
<?php get_comments( $args ); ?>
</div>
<div class="post-containt">
<div class="post-date">
<strong><?php echo get_the_date(); ?></strong>
</div>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
Remove [...] and add read more option using
function new_excerpt_more($output) {
$output = rtrim($output,'[...]');
return $output . '<p>' . 'Read more >>' . '</p>';
}
add_filter('excerpt_more', 'new_excerpt_more');

Related

Set post classes to wordpress

I have a small problem!
I'm trying to add this code to my loop
<?php post_class (); ?>
in to this code:
<?php $ccfit_loop = new WP_Query( array( 'post_type' => 'photo', 'posts_per_page' => 9999 ) ); ?>
<?php if ( $ccfit_loop->have_posts() ) : ?>
<?php while ( $ccfit_loop->have_posts() ) : $ccfit_loop->the_post(); ?>
<?php
$ccfit_img_big = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_id() ), 'ccfit_big', false );
echo '<div id="blogpost" HERE SOULD THE CODE BE><h2>'. get_the_title() .'</h2><img src="'. $ccfit_img_big[0] .'"></div>';
?>
<?php endwhile; ?>
<?php endif; ?>
Grateful for the quick reply!
Replace following code:
<?php
$ccfit_img_big = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_id() ), 'ccfit_big', false );
echo '<div id="blogpost" HERE SOULD THE CODE BE><h2>'. get_the_title() .'</h2><img src="'. $ccfit_img_big[0] .'"></div>';
?>
WIth this:
<?php
$ccfit_img_big = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_id()), 'ccfit_big', false );
?>
<div id="blogpost" <?php post_class(); ?>>
<h2><?php the_title(); ?></h2>
<img src="<?php echo $ccfit_img_big[0]; ?>">
</div>

Wordpress - Permalinks settings stop page-templates from working

I'm about to finish a wordpress-site with a custom theme and several custom-post-types. Now i wanted to change the permalink settings to %post-name%, to make the url's nicer and this is where the problems start.
I have 3 custom-post-types next to the default post-format: events, galleries, shop. Now when I load the page "events (using the template-events.php, what does a wp_query to get all the events), I get all posts. It basically ignores the defined template and loads the one defined for the posts-page, same happens for the shop-page.
The strange thing is that the gallery page (loading the same kind of template file to do the wp_query) works fine.
When I change back to the default permalink-setting everything works fine...
Thanks for any hint!
here my template-events.php
<?php
/*
Template Name: Events
*/
?>
<?php get_header(); ?>
<?php
//Fix homepage pagination
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
$now = strtotime("now");
$args = array(
'post_type' => 'events',
'posts_per_page' => 40,
'post_status' => 'publish',
'meta_key' => '_cmb_date_timestamp',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
array(
'key' => '_cmb_date_timestamp',
'value' => $now,
'type' => 'NUMERIC',
'compare' => '>=' )
)
);
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last 30 days
$where .= " AND _cmb_date_timestamp < '" . date('Y-m-d') . "'";
return $where;
}
//add_filter( 'posts_where', 'filter_where' );
$events_query = new WP_Query($args);
if( $events_query->have_posts() ) :
$derLudwig = get_theme_mod( 'eventsHeading', '');
$url = esc_url( get_theme_mod( 'events_image' ) );
echo '<div class="overview-header" style="background-image: url('.$url.');"></div>';
echo '<h2 class="overview">der Ludwig '.$derLudwig.'</h2>';
?>
<div id="filters">
<div id="filterButtons">
<a id="filterOpen" class="filterB" href="#"><img src="<?php echo get_bloginfo("template_url"); ?>/images/sub-navi2.png"/></a>
<a id="filterClose" class="filterB" href="#"><img src="<?php echo get_bloginfo("template_url"); ?>/images/filter-close.png"/></a>
</div>
<?php
/* FILTERS */
$args = array(
'type' => 'events',
'taxonomy' => 'event_category',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
echo '<div id="categories">';
echo '<h5>Kategorien: </h5>';
echo '<ul>';
foreach ($categories as $category) {
echo '<li>' . $category->name . '</li>';
}
echo '</ul>';
echo '</div>';
?>
Clear
</div>
<!--BEGIN #content -->
<div id="content" class="clearfix">
<?php
echo '<img class="loader" src="'.get_bloginfo('template_url').'/images/loader.gif"/>';
echo '<div id="primary" class="hfeed event-overview">';
while( $events_query->have_posts() ) : $events_query->the_post();
// infos
$time = get_post_meta($post->ID, '_cmb_time', true);
$cdateStamp = get_post_meta($post->ID, '_cmb_date_timestamp');
$monthName = date_i18n('F', $cdateStamp[0]);
$weekdayNumber = date_i18n('j', $cdateStamp[0]);
$weekday = date_i18n('D', $cdateStamp[0]);
$locations = get_the_terms( $post->ID, 'locations' );
$tickets = get_post_meta($post->ID, '_ludwig_events_tickets', true);
$featured = get_post_meta($post->ID, '_ludwig_events_display_featured', true);
$reservation = get_post_meta($post->ID, '_ludwig_events_display_reservation', true);
// grab everything else
$custom_bg = get_post_meta($post->ID, '_zilla_portfolio_display_background', true);
$portfolio_caption = get_post_meta($post->ID, '_zilla_portfolio_caption', true);
?>
<?php
if(!isset($currentMonth) || $currentMonth != $monthName){
?>
<h2 class="<?php echo lcfirst($monthName); ?>"><?php echo $monthName; ?></h2>
<?php
}
$currentMonth = $monthName;
?>
<!--BEGIN .hentry-->
<div id="post-<?php the_ID(); ?>" class="event-small <?php echo lcfirst($monthName); ?>">
<!--BEGIN .entry-content -->
<div class="entry-content">
<div class="eTop">
<span class="dateDay"><?echo $weekdayNumber; ?></span>
<span class="dateMonth"><? _e($monthName, 'ludwig-events'); ?></span>
<span class="dateWeekDay"><? _e($weekday, 'ludwig-events'); ?></span>
</div>
<?php the_post_thumbnail('event-small'); ?>
<div class="eBottom">
<?php the_title('<h3>', '</h3>'); ?>
<span class="eventWo">
<?php
if ( $locations && !is_wp_error( $locations ) ) {
foreach ( $locations as $location ) {
echo $location->name;
}
}
?>
, <?php echo $time; ?>Uhr</span>
<div class="event-buttons">
<div class="inner">
<div class="centerContainer">
<h4>Tickets</h4>
<?php ticketsLink($tickets); ?>
<?php reservationLink($reservation); ?>
</div>
</div>
</div>
</div>
<!--END .entry-content -->
</div>
</div>
<?php endwhile; ?>
<!--END #primary .hfeed-->
</div>
<?php else: ?>
<div id="content" class="nocontent">
<!--BEGIN #post-0-->
<div id="post-0" <?php post_class(); ?>>
<h2 class="entry-title"><?php _e('Keine Events gefunden', 'ludwig') ?></h2>
<!--BEGIN .entry-content-->
<div class="entry-content">
<p><?php _e("Hoppala, das sollte eigentlich nicht passieren", "ludwig") ?></p>
<!--END .entry-content-->
</div>
<!--END #post-0-->
</div>
</div>
<?php endif; ?>
<?php remove_filter( 'posts_where', 'filter_where' ); ?>
<?php get_footer(); ?>
here the working template-gallery.php
<?php
/*
Template Name: Fotos
*/
?>
<?php get_header(); ?>
<?php
//Fix homepage pagination
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} else if ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
// CHECK THAT THIS ONE ISTN DOWN
/*
$args = array(
'post_type' => 'gallery',
'posts_per_page' => 1,
'post_status' => 'publish',
'orderby' => 'modified',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'gallery_category',
'field' => 'id',
'terms' => array( $term_id ),
'operator' => 'IN'
)
),
'paged' => $paged
);
$sina_query = new WP_Query($args);
$sinaString = '';
if( $posts_query->have_posts() ) :
while( $posts_query->have_posts() ) : $posts_query->the_post();
?>
<?php endwhile; ?>
<?php endif; ?>
*/
$args = array(
'post_type' => 'gallery',
'orderby' => 'modified',
'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $paged
);
$posts_query = new WP_Query($args);
if( $posts_query->have_posts() ) :
$derLudwig = get_theme_mod( 'fotosHeading', '');
$url = esc_url( get_theme_mod( 'fotos_image' ) );
echo '<div class="overview-header" style="background-image: url('.$url.');"></div>';
echo '<h2 class="overview">der Ludwig '.$derLudwig.'</h2>';
?>
<div id="filters">
<div id="filterButtons">
<a id="filterOpen" class="filterB" href="#"><img src="<?php echo get_bloginfo("template_url"); ?>/images/sub-navi2.png"/></a>
<a id="filterClose" class="filterB" href="#"><img src="<?php echo get_bloginfo("template_url"); ?>/images/filter-close.png"/></a>
</div>
<?php
/* FILTERS */
$args = array(
'type' => 'events',
'taxonomy' => 'gallery_category',
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($args);
echo '<div id="categories">';
echo '<h5>Kategorien: </h5>';
echo '<ul>';
foreach ($categories as $category) {
echo '<li>' . $category->name . '</li>';
}
echo '</ul>';
echo '</div>';
?>
Clear
</div>
<!--BEGIN #content -->
<div id="content" class="clearfix">
<?php
echo '<img class="loader" src="'.get_bloginfo('template_url').'/images/loader.gif"/>';
echo '<div id="primary" class="hfeed gallery-overview">';
$counter = 0;
while( $posts_query->have_posts() ) : $posts_query->the_post();
$fotograf = get_post_meta($post->ID, '_ludwig_gallery_fotograf', true);
?>
<?php
if ($counter == 1) {
?>
<div id="post-<?php the_ID(); ?>" class="gallery-grid post-<?php echo $counter++; ?>">
<!--BEGIN .entry-content -->
<div class="entry-content">
<a href="<?php echo get_permalink(); ?>">
<div class="overlayContainer">
<div class="overlay">
<div class="content">
<div class="inner">
<?php
/*
$terms = get_the_terms( $post->ID, 'gallery_category');
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
*/
?>
<p class="galleryCategories">
<span><?php //echo $on_draught; ?></span>
</p>
<?php //endif; ?>
<h3><?php the_title(); ?></h3>
<p class="fotograf">von<br><span><?php echo $fotograf; ?></span></p>
</div>
</div>
</div>
</div>
<?php the_post_thumbnail('gallery-mid'); ?></a>
</div>
</div>
<?php echo $counter++; ?>
<?php
} else {
?>
<div id="post-<?php the_ID(); ?>" class="gallery-grid post-<?php echo $counter++; ?>">
<!--BEGIN .entry-content -->
<div class="entry-content">
<a href="<?php echo get_permalink(); ?>">
<div class="overlayContainer">
<div class="overlay">
<div class="content">
<div class="inner">
<?php
/*
$terms = get_the_terms( $post->ID, 'gallery_category');
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
*/
?>
<p class="galleryCategories">
<span><?php //echo $on_draught; ?></span>
</p>
<?php //endif; ?>
<h3><?php the_title(); ?></h3>
<p class="fotograf">von<br><span><?php echo $fotograf; ?></span></p>
</div>
</div>
</div>
</div>
<?php the_post_thumbnail('gallery-mid'); ?></a>
</div>
</div>
<?php } ?>
<?php endwhile; ?>
<!--END .hfeed -->
</div>
<a class="archivlink" href="#">⌸ Archiv</a>
<div class="navigation">
<div class="next-posts"><?php next_posts_link('« Older Entries', $posts_query->max_num_pages) ?></div>
<div class="prev-posts"><?php previous_posts_link('Newer Entries »', $posts_query->max_num_pages) ?></div>
</div>
<?php else: ?>
<div id="content" class="nocontent">
<!--BEGIN #post-0-->
<div id="post-0" <?php post_class(); ?>>
<h2 class="entry-title"><?php _e('Keine Fotos gefunden', 'ludwig') ?></h2>
<!--BEGIN .entry-content-->
<div class="entry-content">
<p><?php _e("Hoppala, das sollte eigentlich nicht passieren", "ludwig") ?></p>
<!--END .entry-content-->
</div>
<!--END #post-0-->
</div>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Hint from me (Since you do not put a code):
Nothing wrong with the permalink. It should be work. I believe what cause this is your code. Please, double check your code and re-install your WordPress, and try again.

Display recent posts with thumbnails with masonry in wordpress

I am working on a wordpress cms. On a page-template, I need to display recent 10 posts. So I tried to use, wp_get_recent_posts, found here in the codex, which I think is an appropriate hook for the purpose.The rest of the code is from my archive.php ( which displays the post-thumbnails in a masonry grid just fine in archive.php). I simply would like to achieve the same thing with the recent posts-thumbnails being on this page-template. Currently, I have this code on a template.
<div id="masonry">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
$args = array('numberposts' => '10', 'meta_key' => '_thumbnail_id');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
if (has_post_thumbnail($recent["ID"])) {
echo '<a href="' . get_permalink($recent["ID"]) . '">';
echo get_the_post_thumbnail($recent["ID"], 'archive_grid');
echo '</a>';
}
}
?>
</div>
</div>
<?php endwhile;
endif; ?>
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
ISSUE : This code returns just a single thumbnail from the most recent posts. I have no clue what is wrong with the loop. The other weird thing to notice is when I var_dump the $recent_posts it returns other text-contents of the posts just fine. If you need to know this, I have my setting as setting->reading->Blog pages show at most->20 posts.
First of all you don't need to have two loops if I understood correct what you want to do. Also 'meta_key' => '_thumbnail_id' isn't necessary. I suppose is just the thumbnail id and not a meta key created by you.
You could achieve what you want like that:
<div id="masonry">
<?php
$args = array(
'posts_per_page' => 10,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => false );
$recent_posts = get_posts( $args );
foreach ($recent_posts as $key=>$post): setup_postdata( $post ); {
?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php } ?> //end of foreach loop
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
But this way you have to fix the pagination your self.
UPDATE:
It seems that get_posts doesn't work with pagination.
<div id="masonry">
<?php
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => false );
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php endwhile; ?> //end while loop
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
<?php wp_reset_postdata(); ?>
If the second solution doens't work you have to provide the dt_pagination() function to see how is working.
Hope it helps.
First thing is you dont need two loops. Try new wp query instead of wp_get_recent_posts. here is an example for you.
<div id="masonry">
<?php $args = array('post_type' => 'post','posts_per_page' => '10' );
$loop = new WP_Query($args); if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post(); global $post; ?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php endwhile; endif; wp_reset_Query(); ?>
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->

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!

Categories