<?php $loop = new WP_Query( array( 'post_type' => 'gallery',
'posts_per_page' => 100 )
);
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php if ( get_post_gallery() ) :
/* Loop through all the image and output them one by one */
foreach( $gallery['src'] as $src ) : ?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
endforeach;
endif;
endwhile; wp_reset_query(); ?>
The above code pulls a WordPress gallery from a custom post named "gallery." Then it stores and displays the image. Is there a way to also store the caption of the gallery into a variable?
You can get image captions from WordPress gallery using following code.
<?php $loop = new WP_Query( array( 'post_type' => 'gallery',
'posts_per_page' => 100 )
);
while ( $loop->have_posts() ) : $loop->the_post();
if ( $gallery = get_post_gallery( get_the_ID(), false ) ) :
$img_ids = explode( ',', $gallery['ids'] );
/* Loop through all the image and output them one by one */
foreach( $gallery['src'] as $key => $src ) : ?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
$image_post = get_post( $img_ids[ $key ] ); ?>
<p class="wp-caption-text"><?php echo $image_post->post_excerpt; ?></p>
<?php endforeach;
endif;
endwhile; wp_reset_postdata(); ?>
Related
Maybe I'm going about this incorrectly but I'm having an issue with getting info outside of the while loop:
<?php
$title = get_field('car_list_title');
$field = get_field('tax_field_selector');
$query = new WP_Query( array(
'post_type' => 'cars',
'taxonomy' =>'make',
'term' => $field->name,
'posts_per_page' => -1,
'orderby' =>'title',
'order' =>'ASC'
) );
$taxonomy = get_terms( array(
'taxonomy' => 'location',
'hide_empty' => true
) );
if ( $field || $query->have_posts() ) :
?>
<div class="c-cars">
<h2 class="c-cars_title u-t--underline--lightblue">
<?= $title; ?>
</h2>
<?php foreach( $taxonomy as $tax ) :
$tax_name = $tax->name;
?>
<div class="c-cars_row">
<h4 class="c-cars_location-title">
<?= $tax_name; ?>
</h4>
<div class="c-cars_cars">
<?php while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
$link = get_permalink();
$image = get_field('car-picture');
$image_alt = get_field('car_alt');
$image_title = get_field('car_title');
$post_id = get_the_ID();
$terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true));
$location = $terms[0]->name;
?>
<?php if( $location === $tax_name ) : ?>
<div class="c-cars_car">
<a href="<?= $link; ?>">
<img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>">
</a>
<h4 class="text-center">
<a href="<?= $link; ?>">
<?= $title; ?>
</a>
</h4>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
So what happens here is I get a list of the locations and all the cars in those locations:
Location 1:
Car
Car
Car
Location 2:
Car
Car
Car
Location 3:
Location 4:
Car
Car
Car
The problem here is, as an example, Location 3 shows up even though there's no "posts" in that term.
The while loop is only cars of a specific model, sorted into what location they are at.
I'm not really sure how to filter out the empty locations.
I do:
<?php if( $location === $tax_name ) : ?>
Inside of the loop and that filters them out of the locations but still leaves the location title because it's outside of the while loop. If I were able to do this earlier up in the code it may work but I can't get the list of active terms outside of the while loop.
I'm kind of lost right now. Any ideas or suggestions? Thanks!
you can check using condition if has a term so show title either it will be blank try it below condition and mentioned in a comment if it's work or not.
has_terms
https://developer.wordpress.org/reference/functions/has_term/
function check if the post has terms.
if( has_term( $location, $tax_name ) ) {
// do something
}
Okay, I have updated your answer please try it below code. I have just get post count of terms and applied condition if terms has post count so show terms title name or if there is no post count of terms so the title will show blank.
<?php
$title = get_field('car_list_title');
$field = get_field('tax_field_selector');
$query = new WP_Query( array(
'post_type' => 'cars',
'taxonomy' =>'make',
'term' => $field->name,
'posts_per_page' => -1,
'orderby' =>'title',
'order' =>'ASC'
) );
$taxonomy = get_terms( array(
'taxonomy' => 'location',
'hide_empty' => true
) );
if ( $field || $query->have_posts() ) :
?>
<div class="c-cars">
<h2 class="c-cars_title u-t--underline--lightblue">
<?= $title; ?>
</h2>
<?php foreach( $taxonomy as $tax ) :
$tax_name = $tax->name;
$tax_post_count = $tax->count;
?>
<div class="c-cars_row">
if ( $tax_post_count > 0 ) : ?>
<h4 class="c-cars_location-title">
<?= $tax_name; ?>
</h4> <?php
else : ?>
<h4 class="c-cars_location-title">
<?= $tax_name = ''; ?>
</h4> <?php
endif; ?>
<div class="c-cars_cars">
<?php while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
$link = get_permalink();
$image = get_field('car-picture');
$image_alt = get_field('car_alt');
$image_title = get_field('car_title');
$post_id = get_the_ID();
$terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true));
$location = $terms[0]->name;
?>
<?php if( $location === $tax_name ) : ?>
<div class="c-cars_car">
<a href="<?= $link; ?>">
<img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>">
</a>
<h4 class="text-center">
<a href="<?= $link; ?>">
<?= $title; ?>
</a>
</h4>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
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>
?>
I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).
I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here's what I have so far:
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>
<?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>
<div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">
<div class="port-item-home">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
And here's what I've tried so far to get the category list:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => 'project-type'
);
$categories = get_categories( $args );
echo '<p> '.print_r(array_values($categories)).'something</p>'
?>
I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post.
I also tried:
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'project-type';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?>
And that also displays all categories instead of the ones pertaining to the post.
Any suggestions??
Got it! Found this article that helped me out:
https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type
<!-- The Query -->
<?php
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'menu_order' );
$custom_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms_slugs_string = '';
$terms = get_the_terms( $post->ID, 'my_post_type' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_slugs_array = array();
foreach ( $terms as $term ) {
$term_slugs_array[] = $term->slug;
}
$terms_slugs_string = join( " ", $term_slugs_array );
}
?>
<div class="box<?php echo $terms_slugs_string ?>">
<div class="port-item-home">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>
Hi i have this template in wordpress, i would like to just display 10 items, because right now in general options in wordpress i have 5 items but i would like to make an exception with this template i dont know where i have to modify the code in order to show in this template 10 items:
<?php
/*
Template Name: Blog List
*/
?>
<?php get_header(); ?>
<div class="content-wrap">
<div class="content">
<?php tie_breadcrumbs() ?>
<div id="content" class="podcast_archive">
<!--<div class="podcast_full">-->
<?php if ( have_posts() ) : ?>
<header><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<h1><?php _e( 'El Jurado del Pueblo' , 'ss-podcasting' ); ?></h1>
</header>
<?php
$feed_url = trailingslashit( home_url() ) . '?feed=podcast';
$custom_feed_url = get_option('ss_podcasting_feed_url');
if( $custom_feed_url && strlen( $custom_feed_url ) > 0 && $custom_feed_url != '' ) {
$feed_url = $custom_feed_url;
}
$itunes_url = str_replace( array( 'http:' , 'https:' ) , 'itpc:' , $feed_url );
?>
<section>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post(); ?>
<?php
$terms = wp_get_post_terms( get_the_ID() , 'series' );
foreach( $terms as $term ) {
$series_id = $term->term_id;
$series = $term->name;
break;
}
?>
<article class="podcast_episode">
<?php if( has_post_thumbnail() ) { ?>
<?php $img = wp_get_attachment_image_src( get_post_thumbnail_id() ); ?>
<a>" title="<?php the_title(); ?>">
<?php the_post_thumbnail( 'podcast-thumbnail' , array( 'class' => 'podcast_image' , 'alt' => get_the_title() , 'title' => get_the_title() ) ); ?>
</a>
<?php } ?>
<h3>
<strong><?php the_title(); ?></strong>
<div class="podcast_meta"><?php echo $series; ?><aside></div>
</h3>
<div id="audio">
<?php global $ss_podcasting;
$enclosure = $ss_podcasting->get_enclosure( get_the_ID() );
if( $enclosure ) {
$audio_player = $ss_podcasting->audio_player( $enclosure );
echo $audio_player;
} ?>
<?php the_content(); ?>
</div>
<div id="audioinfo">
<a>">Descargar Audio</a>
<span class="audiometa">
Tamaño: <?php echo get_post_meta(get_the_ID(), 'filesize', true) ?>
</span>
</div>
<?php echo do_shortcode('[divider]');?>
</article>
<?php
endwhile;
?>
</section>
<?php endif; ?>
<?php wp_pagenavi(); ?>
<div class="podcast_clear"></div>
</div>
<?php comments_template( '', true ); ?>
</div><!-- .content -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
?>
i would some advise so i can do it by myself :)
thank you very much
You'll want to use the query_posts() function for that, just before your loop.
global $wp_query;
$args = array_merge( $wp_query->query_vars, array( 'showposts' => '10' ) );
query_posts( $args );
http://codex.wordpress.org/Function_Reference/query_posts
You'd need to modify your template's query. I'd suggest making use of the WP_Query class. For example:
$args = array(
'posts_per_page' => 10
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
// Loop item here
}
wp_reset_postdata();
} else {
// No results found
}
if
$args = array(
'posts_per_page' => 10
);
not work than check at back-end under Setting->Reading there is a "Blog pages show at most" if there is 5 post than it display only 5 post.
i want to display all the attached images of a page in the main slider of my home page. i have been using following code..
<div class="carousel-inner">
<?php if(have_posts()) : while (have_posts()) : the_post();?>
<?php
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {?>
<div class="item active">
<img src="<?php echo wp_get_attachment_src( $attachment_id, 'full' ); ?>" alt="First slide">
</div>
<?php
}
}
?>
<?php endwhile;else: ?>
<?php echo "No slider Images found" ?>
<?php endif; ?>
</div>
But its not loading the images . what is it that i am doing wrong . please help me out
This is how im doing it
$args = array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => 'any',
);
$attachments = get_posts($args);
foreach ($attachments as $att)
{
$imgThumb = image_downsize($att->ID, 'slider-image');
echo $img = '<img src="'.$imgThumb[0].'" alt="'.$att->postitle.'" />';
}
This is part of the code (its more complicated at its later put the image in <li> elements and sorts them and so on - but this should tell you how you may get images.
image_downsize is wordpress command that takes attachment id and its size (named or array of width and height) and then it returns the path to the image with that size (it also scales the image if needed).
You are using wp_get_attachment_image_src() function wrong way.
Here is the corrected code
<div class="carousel-inner">
<?php if(have_posts()) : while (have_posts()) : the_post();?>
<?php
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image'
));
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full'); // returns an array
?>
<div class="item active">
<img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>">
</div>
<?php
}
}
?>
<?php endwhile;else: ?>
<?php echo "No slider Images found" ?>
<?php endif; ?>
</div>