I want to make a slide show in wordpress, I made a category for it and now I want to get images of that category for my slide show. Here is part of my code:
<div id="slide-show">
<ul>
<?php query_posts('cat=1'); while(have_posts()): the_post();?>
<li>
<a href="<?php the_permalink();?>">
<img src="<?php
$image=wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail');
echo $image[0];?>" alt="<?php the_title();?>" /></a>
</li>
<?php endwhile; wp_reset_query();?>
</ul>
</div>
But it does not work. Can anybody help me please?
First of all, I'll repeat myself:
First of all, don't use query_posts.
Please, check:
When should you use WP_Query vs query_posts() vs get_posts()?
When to use WP_query(), query_posts() and pre_get_posts
In this case, I'd suggest building a function to create the desired output. You put it inside your theme functions.php file. Put it at the end of the file.
If there is any ?> as the last thing of a PHP file, remove it, it is not necessary and may break your site if there is any white space after it.
That said, our function will print exactly the Html you want and it is called like this in any theme template file (index.php, single.php, page.php, etc).
<?php get_gallery(4); ?>
The number 4 is the Category ID.
And this is the function, check the comments in the code:
function get_gallery( $id )
{
// Simple query
$posts = get_posts( array(
'category' => $id,
'post_status' => 'publish',
'post_type' => 'post',
) );
// Start building the Html code
$slide_show = '
<div id="slide-show">
<ul>';
// Iterate through the results
foreach ( $posts as $post )
{
// Assign value and test if it exists at the *same time*
if( $thumb = get_post_thumbnail_id( $post->ID ) )
{
$permalink = get_permalink( $post->ID );
$image = wp_get_attachment_image_src( $thumb );
// Build the Html elements
$slide_show .= '
<li>
<a href="' . $permalink . '">
<img src="'. $image[0] . '" alt="' . $post->post_title .'" />
</a>
</li>';
}
}
// Finish the Html
$slide_show .= '
</ul>
</div>
';
// Print the Html
echo $slide_show;
}
Result:
Related
I'm using this piece of code in my Wordpress template:
<?php
$args = array( 'numberposts' => '12', 'post_type' => 'training', 'post_status' => 'publish' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ) {
echo '<div class="col-xs-12 col-md-4"><article><div class="kartel"><a href="' . get_permalink($recent["ID"]) . '">';
if ( has_post_thumbnail( $recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'medium');
}
echo '</a></div><h3>' . $recent["post_title"].'</h3> ';
echo '<em>Doelgroep //</em>
<p>One-liner/super korte omschrijving</p>';
echo 'Tell me more ';
echo '</article></div>';
}
wp_reset_query();
?>
Thing is that I now want to add a custom field (let's say 'custom_field') that displays a custom excerpt underneath the thumbnail for the grid. I can get the usual fields (excerpt, title, etc.) but not the custom fields. For example the_field ('custom_field'); isn't working..
Any ideas/suggestions?
Hope to hear from you guys!
Filt
First of all change your approach to queries and Wordpress loops using the WP_Query class.
<?php
$args = array( 'numberposts' => '12', 'post_type' => 'training', 'post_status' => 'publish' );
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ){
$loop->the_post(); ?>
<div class="col-xs-12 col-md-4">
<article>
<div class="kartel">
<a href="<?php the_permalink(); ?>">
<?php if( has_post_thumbnail("medium") ) {
the_post_thumbnail( "medium" );
}
?>
</a>
</div>
<a href="<?php the_permalink(); ?>">
<?php the_title("<h3>","</h3>"); ?>
</a>
<em>Doelgroep //</em>
Tell me more
</article>
</div>
<?php }
}
wp_reset_postdata();
?>
Later, in the loop of your posts, you can recall the custom field using:
the_field ('custom'); //which prints the result on screen
$var = get_field ('custom'); //or echo get_field ('custom') which returns the content in a variable.
If you want to recall a specific custom field inserted in a post or page or custom post type, you must use the following syntax:
the_field ('custom', $ post_id);
get_field ('custom', $ post_id)
That's all :)
I have an archive page for a custom content type, and I want to display one of the custom fields in the archive listing. Im not sure what hook to use or how to access the variables in the custom content type to display in -archive.php. Can anyone help me out with the relevant hooks, or how to access those field variables?
First create archive file with your custom post type, the file name should be like this archive-{post_type}.php
Go through the below code this may help you
<?php
remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop
add_action( 'genesis_loop', 'custom_do_grid_loop' ); // Add custom loop
function custom_do_grid_loop() {
// Intro Text (from page content)
echo '<div class="page hentry entry">';
echo '<h1 class="entry-title">'. get_the_title() .'</h1>';
echo '<div class="entry-content">' . get_the_content() ;
$args = array(
'post_type' => 'custom post type', // enter your custom post type
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page'=> '12', // overrides posts per page in theme settings
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post(); global $post;
echo '<div id="testimonials">';
echo '<div class="one-fourth first">';
echo '<div class="quote-obtuse"><div class="pic">'. get_the_post_thumbnail( $id, array(150,150) ).'</div></div>';
echo '<div style="margin-top:20px;line-height:20px;text-align:right;"><cite>'.genesis_get_custom_field( '_cd_client_name' ).'</cite><br />'.genesis_get_custom_field( '_cd_client_title' ).'</div>';
echo '</div>';
echo '<div class="three-fourths" style="border-bottom:1px solid #DDD;">';
echo '<h3>' . get_the_title() . '</h3>';
echo '<blockquote><p>' . get_the_content() . '</p></blockquote>';
echo '</div>';
echo '</div>';
endwhile;
endif;
echo '</div><!-- end .entry-content -->';
echo '</div><!-- end .page .hentry .entry -->';
}
/** Remove Post Info */
remove_action('genesis_before_post_content','genesis_post_info');
remove_action('genesis_after_post_content','genesis_post_meta');
genesis();
?>
I'm trying to filter data from a custom post-type called "Clients", based on category. What I need to appear is the logo for each particular client.
I've set up a repeater field of Post Objects, so I can change the order that the logos will be displayed.
What I have currently works, however I cannot figure out how to incorporate the Post Object selector, so that what appears is determined by the instances I've added via the Repeater.
Here is the link to the site. Appreciate any answers!
See below for screenshot of my dashboard setup:
<ul id="filters">
<?php
$terms = get_terms("category", array(
'orderby' => 'slug'
)); // get all categories, but you can use any taxonomy
$count = count($terms); //How many are they?
if ( $count > 0 ){ //If there are more than 0 terms
foreach ( $terms as $term ) { //for each term:
echo "<li><a href='#' data-filter='.".$term->slug."'>" . $term->name . "</a></li>\n";
//create a list item with the current term slug for sorting, and name for label
}
}
?>
</ul>
<?php $the_query = new WP_Query(array(
'post_type' => 'clients',
'posts_per_page' => '-1',
'order' => 'ASC'
)); //Check the WP_Query docs to see how you can limit which posts to display ?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="isotope-list-container">
<div id="isotope-list" class="row small-up-1 medium-up-2 large-up-3">
<?php while ( $the_query->have_posts() ) : $the_query->the_post();
$termsArray = get_the_terms( $post->ID, "category" ); //Get the terms for this particular item
$termsString = ""; //initialize the string that will contain the terms
foreach ( $termsArray as $term ) { // for each term
$termsString .= $term->slug.' '; //create a string that has all the slugs
}
?>
<div class="<?php echo $termsString; ?> portfolio columns"> <?php // 'portfolio' is used as an identifier (see Setp 5, line 6) ?>
<div class="portfolio-item-container">
<?php
$image = get_field('logo');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
</div> <!-- end portfolio item -->
<?php endwhile; ?>
</div> <!-- end isotope-list -->
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
I was able to make this considerably simpler by using the Post Types Order plugin to reorder my posts.
https://wordpress.org/plugins/post-types-order/
Current theme is set up to generate a thumbnail based on user input (variable height). I would like to have this thumbnail link to a full size featured image via prettyphoto. Current code calling generated thumb:
<?php
//if our user has a post thumbnail
//out featured image URL
$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
?>
<?php if($src[0] != '') : //if user has featured image ?>
<img src="<?php echo ddTimthumb($src[0], $contentW, get_post_meta($post->ID, 'postThumbHeight', true)); ?>" alt="<?php the_title(); ?>" />
You can use this just modify the classes and other attributes according to your need.
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a rel="prettyPhoto" href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail();
echo '</a>';
}
?>
I am actually using this same thing in one of my project see the code link and see if that can help you :) and once again don't expect it to work by copy pasting this is just to give you an idea modify it to your needs :)
Link to pastebin for code sample (or see below)
<div class="row" id="gallery-main">
<?php $args = array(
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
);
query_posts($args); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="span4 portfolio-item">
<?php
if ( has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
echo '<a class="image-link pi-img" rel="prettyPhoto" href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
the_post_thumbnail('portfolio-listing');
echo '<div class="hover-style"></div></a>';
}
?>
</div>
<?php endwhile;endif; ?>
<?php wp_reset_query(); ?>
</div>
When I am writing below code for recent posts and then after I write code for next and previous link then that function will give me same link of post. If I am comment out "$tags = wp_get_post_tags($post->ID);" This line then It'll print next post link. How can I resolved this error ? please help me.
<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '<div class="articlecontent font16 bold fontgray">Related Posts</div>';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo '<div class="articlecontent"><ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo "</ul></div>";
}
}
?>
<div class="nextprevbar">
<div class="prevtopic"><?php
previous_post_link( '%link', '<img border="0" alt="" src="'.get_template_directory_uri().'/images/prev-bullet.gif">' . _x( ' ', 'Previous post link', 'twentyten' ) . ' %title' ); ?></div>
<div class="nexttopic"><?php next_post_link( '%link', '%title <img border="0" alt="" src="'.get_template_directory_uri().'/images/next-bullet.gif">' . _x( ' ', 'Next post link', 'twentyten' ) . '</span>' ); ?></div>
</div>
The problem has to be in the $post variable. Before this line is executed:
while ($my_query->have_posts()) : $my_query->the_post(); ?>
the $post var holds the current post data (I guess this code is inside single.php?). But after this line, and inside the loop, the $post var holds your various recent posts, one by one (you setup the $post variable when you call the_post() ).
After that loop (bellow the endwhile), $post will hold the data of the last post retrieved in that loop.
previous_post_link() and next_post_link() need to access $post for reference of the current post, but they are taking as reference the last post of your recent posts, instead of the post being read by your user.
I don't know what the html structure of this page is, but I would put the recent posts list AFTER the navigation links (next, previous posts). That would solve the problem if I am right, and in my opinion it would be semantically clearer.
Or you could also try this:
Add this line:
$currentPost = clone $post;
Before:
$my_query = new WP_Query($args);
And add this line:
<?php $post = $currentPost; ?>
Before you call next and previous post links functions.