I am using wordpress and trying to create a blog landing page to show the most recent blog posts. So far so good but I am having difficulties showing the blog image in the image tag. I am able to obtain the postId by using get_the_id function. I was also able to get the date of the post by using the_date function.
However, I cannot get the wp_get_attachment_image function to show the image of the blog post.
Please see my code below.
<?php $query = new WP_Query( 'posts_per_page=5' ); ?>
<?php while ($query -> have_posts()) : $query -> the_post(); ?>
<div class="blog">
<img src="wp_get_attachment_image( get_the_ID() ); ">
<h3><?php the_title(); ?></h3>
<p><?php the_date(); ?></p>
<p><?php the_excerpt(__('(more…)')); ?></p>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
Use following code for get the attached image.
<img src="<?php echo wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium');?>">
The function wp_get_attachment_image is waiting for attachement_id.
See below in wp, how we get all attachments from a post :
<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
}
}
}
?>
I had to use the following function the_post_thumbnail() and echo the result within an image tag
<img src="<?php echo the_post_thumbnail();?>">
The following code worked.
<?php $query = new WP_Query( 'posts_per_page=5' ); ?>
<?php while ($query -> have_posts()) : $query -> the_post(); ?>
<div class="blog">
<img src="wp_get_attachment_image( get_the_ID() ); ">
<h3><?php the_title(); ?></h3>
<p><?php the_date(); ?></p>
<p><?php the_excerpt(__('(more…)')); ?></p>
</div>
?>
Please let me know why Next and Previous Post links are not working on the following code? I am trying to display only one post at a time. I have tried to check on different post but not able to find something similar to what I have. Please guide...
<?php
$args = array( 'numberposts' => 1 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post);
?>
<h2><?php the_title(); ?></h2>
<div>
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
</div>
<?php the_content(); ?>
<?php endforeach; ?>
<?php comments_template(); ?>
<nav id="nav-posts">
<div class="prev"><?php next_posts_link('PREVIOUS POSTS'); ?></div>
<div class="next"><?php previous_posts_link('NEXT POSTS'); ?></div>
</nav>
I think I had the same problem as you, simply adding this will make the link go to previous_post_link or next_post_link
<?php previous_post_link( '%link','Previous' ) ?>
<?php next_post_link( '%link','Next' ) ?>
Best of luck
According to the get_posts documentation, you should use the following method to display prev/next links:
<?php
$postlist = get_posts( 'orderby=menu_order&sort_order=asc' );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
?>
<div class="navigation">
<?php if ( !empty( $prevID ) ): ?>
<div class="alignleft">
<a href="<?php echo get_permalink( $prevID ); ?>"
title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
</div>
<?php endif;
if ( !empty( $nextID ) ): ?>
<div class="alignright">
<a href="<?php echo get_permalink( $nextID ); ?>"
title="<?php echo get_the_title( $nextID ); ?>">Next</a>
</div>
<?php endif; ?>
</div><!-- .navigation -->
I need to use the same page for different taxonomies and terms.
How can I retrieve the taxonomies and the terms from the URL and run the same page for those I that I want?
The query should be run through a URL since I am not using forms.
When the user clicks on the link, a new page should be opened that is an archive of posts having custom post type, taxonomy and taxonomy terms specified in the URL.
This is the code that I have right now:
<? /*
* Template Name: Activities template
* Description: Template for activties like restaurants, pubs, etc.
*/
?>
<?php $options = get_option('mh_options'); ?>
<?php get_header(); ?>
<?php
if ( get_query_var('paged') ) {$paged = get_query_var('paged');}
if ( get_query_var('page') ) {$paged = get_query_var('page');}
$args = array(
'post_type' => 'activties',
'tax_query' => array(
array(
'taxonomy' => 'restaurants',
'field' => 'slug',
'terms' => 'italian'
),
'paged' => $paged
)
);
query_posts( $args ); ?>
<div class="wrapper clearfix">
<div class="main">
<div class="content <?php mh_content_class(); ?>">
<?php mh_before_page_content(); ?>
<?php dynamic_sidebar('pages-1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- <?php the_content(); ?> -->
<article <?php post_class(); ?>>
<div class="loop-wrap loop-layout2">
<div class="clearfix">
<div class="loop-thumb">
<a href="<?php the_permalink(); ?>">
<?php if( get_field('business_logo') ): ?>
<?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'loop'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" />
<?php else: echo '<img src="' . get_template_directory_uri() . '/images/noimage_300x225.png' . '" alt="No Picture" />'; ?>
<?php endif; ?>
</a>
</div>
<div class="loop-content">
<header>
<h3 class="loop-title"><?php the_title(); ?></h3>
</header>
<?php if (get_field('business_description')): ?>
<?php $text_to_trim = get_field('business_description');
echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
<?php endif; ?>
<?php if (get_field('business_paying_desc')): ?>
<?php $text_to_trim = get_field('business_paying_desc');
echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
<?php endif; ?>
</div>
</div>
</div>
</article>
<?php dynamic_sidebar('pages-2'); ?>
<?php endwhile; ?>
<!-- <?php wp_reset_postdata(); ?> -->
<?php wp_reset_query(); ?>
<?php if (isset($options['comments_pages']) ? $options['comments_pages'] : false) : ?>
<section>
<?php comments_template(); ?>
</section>
<?php endif; ?>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php mh_second_sb(); ?>
</div>
<?php get_footer(); ?>
EDIT
After a few days, I found the solution that I am going to post, however it has got an issue: I cannot see the list of the posts if a post has got multiple terms (more than 1 term basically) selected by the checkboxes when it is created or edited.
This is my taxonomy.php file
<?php
$post = $wp_query->post;
/*$catMerc = array('restaurants','pubs', 'the-pizzerias', 'bars', 'cafes', 'nightlife-clubs', 'shopping', 'the-coffeeshops');*/
$catMerc = array('the-restaurants', 'the-coffeeshops', 'the-pizzerias', 'shopping', 'nightlife-clubs', 'cafes', 'bars', 'pubs');
$termsObjects = wp_get_object_terms($post->ID, $catMerc);
//Assuming your post only has one category, if theres more it will return multiple objects in it's return array:
$currentCustomCat = $termsObjects[0]->slug;
$currentCatMerc = get_query_var('taxonomy');
//you can have 'name' instead of 'slug' if that helps too
$customcatarray = array('american-pubs-exclusive5', 'american-pubs-in-town', 'beer-houses-exclusive5', 'beer-houses-in-town', 'free-joints-exclusive5', 'free-joints-in-town', 'local-atmosphere-exclusive5', 'local-atmosphere-in-town', 'spanish-atmosphere-exclusive5', 'spanish-atmosphere-in-town', 'take-away-exclusive5', 'take-away-in-town', 'traditional-dutch-exclusive5', 'traditional-dutch-in-town', 'african-exclusive-5', 'african-in-town', 'argentinian-restaurants-exclusive5', 'argentinian-restaurants-in-town',' asian-restaurants-exclusive5', 'asian-restaurants-in-town', 'dutch-restaurants-exclusive5', 'dutch-restaurants-in-town', 'french-restaurants-exclusive5', 'french-restaurants-in-town', 'italian-restaurants-exclusive5', 'italian-restaurants-in-town', 'seafood-restaurants-exclusive5', 'seafood-restaurants-in-town', 'spanish-restaurants-exclusive5', 'spanish-restaurants-in-town', 'cocktail-bars-exclusive5', 'cocktail-bars-in-town', 'disco-bars-exclusive5', 'disco-bars-in-town', 'dutch-bars-exclusive5', 'dutch-bars-in-town', 'internet-cafes-exclusive5', 'internet-cafes-in-town', 'lounge-bars-exclusive5', 'lounge-bars-in-town', 'art-cafes-exclusive5', 'art-cafes-in-town', 'breakfast-lunch-exclusive5','breakfast-lunch-in-town', 'famous-cafes-exclusive5', 'famous-cafes-in-town', 'fashion-cafes-exclusive5', 'fashion-cafes-in-town', 'timeout-exclusive5', 'timeout-in-town', 'best-boutiques-exclusive5','best-boutiques-in-town', 'famous-brands-exclusive5', 'famous-brands-in-town', 'sportswear-exclusive5', 'sportswear-in-town', 'the-pizzerias-with-table-service-in-town', 'the-pizzerias-with-table-service-exclusive5', 'the-pizzerias-takeway-in-town', 'the-pizzerias-takeaway-exclusive5', 'the-coffeeshops-in-town', 'the-coffeeshops-exclusive5');
if (in_array($currentCatMerc, $catMerc) && in_array($currentCustomCat, $customcatarray) ) {include(TEMPLATEPATH.'/page_activities.php'); }
/*if( $currentCustomCat == "italian" || $currentCustomCat == "local-atmosphere"){
//It matched, do stuff here
{include(TEMPLATEPATH.'/single_activities.php'); }
}*/
else { /*include(TEMPLATEPATH.'/page.php'); */
$pagelink=get_page_link (get_page_by_title( 'Homepage' ));
header("Location: $pagelink",TRUE,301);
}
?>
And this is my page_activities.php file
<?php
if ( get_query_var('paged') ) {$paged = get_query_var('paged');}
if ( get_query_var('page') ) {$paged = get_query_var('page');}
$args = array(
'post_type' => 'activities',
'tax_query' => array(
array(
'taxonomy' => $currentCatMerc,
'field' => 'slug',
'terms' => $currentCustomCat
),
'paged' => $paged,
'posts_per_page'=>'10',
)
);
$args2 = array(
'post_type' => 'activities',
'paged' => $paged
);
query_posts( $args ); ?>
<div class="wrapper clearfix">
<div class="main">
<div class="content <?php mh_content_class(); ?>">
<?php mh_before_page_content(); ?>
<?php dynamic_sidebar('pages-1'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- <?php the_content(); ?> -->
<article <?php post_class(); ?>>
<div class="loop-wrap loop-layout2">
<div class="clearfix">
<div class="loop-thumb">
<a href="<?php the_permalink(); ?>">
<?php if( get_field('business_logo') ): ?>
<?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'loop'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" />
<?php else: echo '<img src="' . get_template_directory_uri() . '/images/noimage_300x225.png' . '" alt="No Picture" />'; ?>
<?php endif; ?>
</a>
</div>
<div class="loop-content">
<header>
<h3 class="loop-title"><?php the_title(); ?></h3>
</header>
<?php if (get_field('business_description')): ?>
<?php $text_to_trim = get_field('business_description');
echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
<?php endif; ?>
<?php if (get_field('business_paying_desc')): ?>
<?php $text_to_trim = get_field('business_paying_desc');
echo '<div class="mh-excerpt">'. davide_excerpt($text_to_trim) . '</div>' . "\n" ; ?>
<?php endif; ?>
</div>
</div>
</div>
</article>
<?php dynamic_sidebar('pages-2'); ?>
<?php endwhile; ?>
<!-- <?php wp_reset_postdata(); ?> -->
<?php wp_reset_query(); ?>
<?php endif; ?>
<div class="sb-widget home-2 home-wide">
<h4 class="widget-title">Latest Updates</h4>
<ul class="cp-widget clearfix">
<?php query_posts( $args2 ); ?>
<?php if (have_posts()) : $i=1; while (have_posts() && ($i<=10)) : the_post(); ?>
<li class="cp-wrap cp-small clearfix">
<div class="cp-thumb">
<a href="<?php the_permalink(); ?>">
<?php if( get_field('business_logo') ): ?>
<?php $image = wp_get_attachment_image_src(get_field('business_logo'), 'cp_small'); ?>
<img width="70" height="53" src="<?php echo $image[0]; ?>" alt="<?php the_field('business_logo');?>" data-thumb="<?php echo $thumb[0]; ?>" class="attachment-cp_small wp-post-image" />
<?php endif; ?>
</a></div>
<div class="cp-data">
<p class="cp-widget-title"><?php the_title(); ?></p>
</div>
</li>
<?php $i++; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endif;?>
</ul>
</div>
<div class="sb-widget home-2 home-wide">
<?php echo do_shortcode('[rev_slider 620_100]'); ?>
</div>
<?php if (isset($options['comments_pages']) ? $options['comments_pages'] : false) : ?>
<section>
<?php comments_template(); ?>
</section>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
</div>
<?php mh_second_sb(); ?>
</div>
<?php get_footer(); ?>
Pls help me with this because I am really clueless.
One good news is that if I check the array with print_r($termsObjects[0]); I can see properly the output.
I guess I should use a foreach somehow, but I don't know where to put my hands on.
Thank you to anyone who can help me with this.
Regards
..your question is a bit broad...but the gist of what you need to do:
to pull the variables from the url
$query = explode('&', $_SERVER['QUERY_STRING']);
$params = array();
foreach( $query as $param ){
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}
now you will have a params array i've no idea what your urls will look like, but say its '
postterm1 = italian, postterm2 = chinese, etc
$array['taxquery'] = array();
$array['taxquery'][relation]= 'OR';
foreach ($param as $key=>$value) {
$array['taxquery'][]=array(
'taxonomy' => 'restaurants',
'field' => 'slug',
'terms' => $value
);
}
this will build the query you see below, just delete 'tax_query' array and insert the var $array instead. It should work but i haven't tested it so play around with the format if needed.
your custom query (i think you want multi taxonomies but not all?)
Use WP_query:
$args= array(
'post_type' => 'activties',
'tax_query' => array(
'relation'=>'OR',
array(
'taxonomy' => 'restaurants',
'field' => 'slug',
'terms' => 'italian'
),
array(
'taxonomy' => 'restaurants',
'field' => 'slug',
'terms' => 'chinese'
)
),
);
ref: http://codex.wordpress.org/Class_Reference/WP_Query
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 »' . '</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');
This is what I have in the loop that grabs the posts and displays them on the index page...
<?php the_attachment_link( $attachment->ID , false); ?>
And this is what it results in...
<a href="linkToImage">
<img src="sorceOfImage"/>
</a>
I just want that middle part without having it wrapped in a link. Any ideas? Thank you so much!
what about this:
<?php echo wp_get_attachment_url( $id ); ?>
outputs something like http://example.net/wp-content/uploads/filename
You can get the image only:
<?php echo wp_get_attachment_image( $attachment->ID ); ?>
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image
<?php if (have_posts()) : while(have_posts()) : the_post(); ?>
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
'post_parent' => $post->ID
);
$attachments = get_posts($args);
if ($attachments) : ?>
<ul>
<?php foreach($attachments as $attachment): ?>
<li>
<a href="<?php echo wp_get_attachment_url($attachment->ID, true); ?>">
<img src="<?php echo wp_get_attachment_url($attachment->ID, true); ?>" width="100%"/>
<h3><?php echo $attachment->post_title; ?></h3></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php endwhile; endif; ?>