I'm attempting to grab only the images from a page in Wordpress.
I read there was a function called get_post_gallery() but it doesn't seem to obtain my images. This is what I did:
if ( get_post_gallery() ) :
$images = get_post_gallery(get_the_ID(), false);
foreach($images as $image)
{
echo $image['src'];
}
endif;
This line of code is put inside the loop from what I understand like so:
if ( have_posts() ) {
\\Bit of code in here
}
However, the array that it returns appears to be empty (even though there are images in the post page editor). Do you guys suggest this way or another way to retrieve only images from a post/page?
I've found your answer in another SO question.
Here it is:
function wpse_get_images() {
global $post;
$id = intval( $post->ID );
$size = 'medium';
$attachments = get_children( array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order'
) );
if ( empty( $attachments ) )
return '';
$output = "\n";
/**
* Loop through each attachment
*/
foreach ( $attachments as $id => $attachment ) :
$title = esc_html( $attachment->post_title, 1 );
$img = wp_get_attachment_image_src( $id, $size );
$output .= '<a class="selector thumb" href="' . esc_url( wp_get_attachment_url( $id ) ) . '" title="' . esc_attr( $title ) . '">';
$output .= '<img class="aligncenter" src="' . esc_url( $img[0] ) . '" alt="' . esc_attr( $title ) . '" title="' . esc_attr( $title ) . '" />';
$output .= '</a>';
endforeach;
return $output;
}
Related
I am using WordPress plugin Taxonomy images. I'm using it to get images for taxonomies.
From the below code I am getting the images and link. I want to get category name with these images. Here is my code. How can I get category name?
<?php
$terms = apply_filters( 'taxonomy-images-get-terms', '', array( 'taxonomy' => 'category', ) );
if ( ! empty( $terms ) ) {
print '<div class="row">';
foreach ( (array) $terms as $term ) {
print '<div class="col-sm-3"><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, ' ' ) . '</div>';
}
print '</div>';
}
?>
My new client has a website that hadn't been updated in a long time. Now the PHP version, wordpress version and plugins are all up to date, but the Portifolio items now return this error:
Screenshot:
Warning: Illegal string offset 'alt' in
/home/elite856/public_html/wp-content/themes/eliteled/functions.php on
line 679
Warning: Illegal string offset 'url' in
/home/elite856/public_html/wp-content/themes/eliteled/functions.php on
line 679
This is line 679:
'<img src="'. $ob_img['url'] .'" class="capa" alt="'. $ob_img['alt'] .'" />'.
This is the whole function:
$portfolio = new WP_Query( $args );
if( $portfolio->have_posts() ){
while ( $portfolio->have_posts() ) { $portfolio->the_post();
$ob_img = get_field( 'imagem_de_capa' );
$allcats = get_the_category( get_the_ID() );
$cat = $allcats[0]->name;
$arrayData = array(
'id' => get_the_ID(),
'Titulo' => get_the_title(),
'cat' => $cat
);
//Criando arrayData com as informações do portfolio
if( have_rows('conteudo_do_portfolio') ){
while( have_rows('conteudo_do_portfolio') ){ the_row();
$tipo = get_field( 'tipo' );
if( $tipo == 'foto' ){
if( empty( $arrayData['imgs'] ) ){
$arrayData['imgs'][] = array(
'full' => $ob_img['original_image']['url'],
'thumb' => wp_get_attachment_image_src( $ob_img['id'], 'thumb_portfolio' )[0]
);
}
$idImg = get_sub_field('foto');
$arrayData['imgs'][] = array(
'full' => wp_get_attachment_image_src( $idImg, 'full' )[0],
'thumb' => wp_get_attachment_image_src( $idImg, 'thumb_portfolio' )[0]
);
}else{
$idVideo = youtube_video_id( get_sub_field('video') );
$arrayData['imgs'][] = array(
'full' => $idVideo,
'thumb' => "http://img.youtube.com/vi/{$idVideo}/0.jpg"
);
}
}
}
//'<li class="'. ( is_home() || is_front_page() ? 'item-home' : '' ) .'">'
$conteudo .= '<li>'.
'<img src="'. $ob_img['url'] .'" class="capa" alt="'. $ob_img['alt'] .'" />'.
'<div class="camada text-center">'.
'<a href="#" class="openModalPortfolio" data-tipo="'. get_field( 'tipo' ) .'" data-json=\''. json_encode( $arrayData ) .' \'\ >'.
//'<div class="'. ( is_home() || is_front_page() ? 'center-home' : 'center' ) .'">'.
'<div class="center">'.
//'<img src="/wp-content/themes/eliteled/images/expand'. ( is_home() || is_front_page() ? '' : '-red' ) .'.png" class="expand" />'.
'<img src="/wp-content/themes/eliteled/images/expand-red.png" class="expand" />'.
'<span class="titulo">'. ( strlen( get_the_title() ) > 60 ? substr( get_the_title(), 0, 60 ) . '...' : get_the_title() ) .'</span>'.
'<span class="cat">'. $cat .'</span>'.
'</div>'.
'</a>'.
'</div>'.
'<div class="camada camada-branca"></div>'.
'</li>';
}
}
I've read that this warning used to not be printed like that before PHP 5.4, and also that it happens because it was expecting and array. I got little PHP skills, can someone give me a clue about what to do?
Based from the dumped data, I am guessing the content of original_image is an attachment ID, so try this:
$img_params = json_decode($ob_img, true); // convert string to associative array
$url = wp_get_attachment_image_url($img_params['original_image']);
if ($url) {
// we have the image url now so display the image here tag here
}
if it doesn't work, check the original theme/plugin developer if there is new update to the plugin/theme where this portfolio came from
Hello with the code bellow i am getting some content.
How is possible with php simple html dom to get all the img src?
my code:
foreach($html->find('div[class=post-single-content box mark-links]') as $table)
{
$arr44[]= $table->innertext ;
}
div with class post-single-content box mark-links contains text and images
Try this :
<?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>';
}
}
}
?>
im using the following code to display my posts on a page in wordpress that use a featured image:
$mypages = get_pages( array() );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
but before printing this information out i want to sort the $mypages array so that they display by date published. ive tried this code:
<?php $args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date',
);
$mypages = get_pages($args);
?>
but it doesnt seem to work, am i missing something or doing this the wrong way?
thanks in advance.
FULL CODE BEING USED:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( array($args) );
if ( !empty( $mypages ) ) {
echo '<ul>';
foreach ( $mypages as $mypage ) {
if ( get_the_post_thumbnail( $mypage->ID ) ) {
echo '<div class="featured-container">';
echo '<div class="featured-image">';
echo '<li><a class="feat-hover" href="' . get_permalink( $mypage->ID ) . '">' . get_the_post_thumbnail( $mypage->ID ) . '</a></li>';
echo '</div>';
echo '<div class="featured-text">';
echo '' . get_the_title($mypage->ID ) . '';
echo '</div>';
echo '</div>';
}
}
echo '</ul>';
}
The issue is that you're passing $args into another array(). You only need to pass in the $args directly, since it's already an array(). Change the top block of code to:
$args = array(
'sort_order' => 'ASC',
'sort_column' => 'post_date'
);
$mypages = get_pages( $args );
I am able to list the taxonomy children:
<?php
$taxonomy = 'store_name';
$tax_terms = get_terms($taxonomy);
?>
<ul class="split-list2">
<?php
foreach ($tax_terms as $tax_term) {
echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
}
</ul> ?>
And I found a plugin to make thumbnails for categories/taxonomies:
https://wordpress.org/plugins/taxonomy-images/
But I can't figure out how to add the code to include the thumbnails to the taxonomy category grids.
Does anyone know how to do this
Read the plugin doc perfactly it gives what you want
print apply_filters( 'taxonomy-images-list-the-terms', '', array(
'after' => '</div>',
'after_image' => '</span>',
'before' => '<div class="my-custom-class-name">',
'before_image' => '<span>',
'image_size' => 'detail',
'post_id' => 1234,
'taxonomy' => 'post_tag',
) );
Second parameter in apply_filters( 'taxonomy-images-get-terms', '' ); is array as show above
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach( (array) $terms as $term ) {
print '<li><a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, 'detail' ) . '</li>';
}
print '</ul>';
}