Extracting images from an array - php

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>';
}
}
}
?>

Related

How to get all attachments for a wordpress post?

I am currently attempting to get all the attachments that are attached to each individual post on WordPress and allow a user to download the attachments.
I have looked into the get_attached_media() function in WordPress but I am not sure how to make the files downloadable once I get the media.
Try this code this may help you:
With below code you can fetch all type of media attachment if you need particular media type then you can add one more args 'post_mime_type' => 'image'
<?php if ( $post->post_type == 'post' && $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>';
}
}
}
?>
Another way you can achieve this:
$media = get_attached_media('image', get_the_ID()); // Get image attachment(s) to the current Post
print_r($media);

Sorting wordpress posts by date published

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 );

Adding a link into wordpress shortcode

i have a shortcode that posts recent blog entries from a certain category on one of my web pages, however i want to display a static link at the end of everypost, is there anyway to do this?
the following code is used to display the posts:
<?php echo do_shortcode('[display-posts category="competitions" posts_per_page="4" include_excerpt="true" image_size="thumbnail" wrapper="ul"]');
Thanks in advance.
<?php
// Create the shortcode
add_shortcode( 'display-posts', 'be_display_posts_shortcode' );
function be_display_posts_shortcode( $atts ) {
// Original Attributes, for filters
$original_atts = $atts;
// Pull in shortcode attributes and set defaults
$atts = shortcode_atts( array(
'title' => '',
'author' => '',
'category' => '',
'date_format' => '(n/j/Y)',
'display_posts_off' => false,
'exclude_current' => false,
'id' => false,
'ignore_sticky_posts' => false,
'image_size' => false,
'include_title' => true,
'include_author' => false,
'include_content' => false,
'include_date' => false,
'include_excerpt' => false,
'meta_key' => '',
'meta_value' => '',
'no_posts_message' => '',
'offset' => 0,
'order' => 'DESC',
'orderby' => 'date',
'post_parent' => false,
'post_status' => 'publish',
'post_type' => 'post',
'posts_per_page' => '10',
'tag' => '',
'tax_operator' => 'IN',
'tax_term' => false,
'taxonomy' => false,
'wrapper' => 'ul',
'wrapper_class' => 'display-posts-listing',
'wrapper_id' => false,
), $atts, 'display-posts' );
// End early if shortcode should be turned off
if( $atts['display_posts_off'] )
return;
$shortcode_title = sanitize_text_field( $atts['title'] );
$author = sanitize_text_field( $atts['author'] );
$category = sanitize_text_field( $atts['category'] );
$date_format = sanitize_text_field( $atts['date_format'] );
$exclude_current = be_display_posts_bool( $atts['exclude_current'] );
$id = $atts['id']; // Sanitized later as an array of integers
$ignore_sticky_posts = be_display_posts_bool( $atts['ignore_sticky_posts'] );
$image_size = sanitize_key( $atts['image_size'] );
$include_title = be_display_posts_bool( $atts['include_title'] );
$include_author = be_display_posts_bool( $atts['include_author'] );
$include_content = be_display_posts_bool( $atts['include_content'] );
$include_date = be_display_posts_bool( $atts['include_date'] );
$include_excerpt = be_display_posts_bool( $atts['include_excerpt'] );
$meta_key = sanitize_text_field( $atts['meta_key'] );
$meta_value = sanitize_text_field( $atts['meta_value'] );
$no_posts_message = sanitize_text_field( $atts['no_posts_message'] );
$offset = intval( $atts['offset'] );
$order = sanitize_key( $atts['order'] );
$orderby = sanitize_key( $atts['orderby'] );
$post_parent = $atts['post_parent']; // Validated later, after check for 'current'
$post_status = $atts['post_status']; // Validated later as one of a few values
$post_type = sanitize_text_field( $atts['post_type'] );
$posts_per_page = intval( $atts['posts_per_page'] );
$tag = sanitize_text_field( $atts['tag'] );
$tax_operator = $atts['tax_operator']; // Validated later as one of a few values
$tax_term = sanitize_text_field( $atts['tax_term'] );
$taxonomy = sanitize_key( $atts['taxonomy'] );
$wrapper = sanitize_text_field( $atts['wrapper'] );
$wrapper_class = sanitize_html_class( $atts['wrapper_class'] );
if( !empty( $wrapper_class ) )
$wrapper_class = ' class="' . $wrapper_class . '"';
$wrapper_id = sanitize_html_class( $atts['wrapper_id'] );
if( !empty( $wrapper_id ) )
$wrapper_id = ' id="' . $wrapper_id . '"';
// Set up initial query for post
$args = array(
'category_name' => $category,
'order' => $order,
'orderby' => $orderby,
'post_type' => explode( ',', $post_type ),
'posts_per_page' => $posts_per_page,
'tag' => $tag,
);
// Ignore Sticky Posts
if( $ignore_sticky_posts )
$args['ignore_sticky_posts'] = true;
// Meta key (for ordering)
if( !empty( $meta_key ) )
$args['meta_key'] = $meta_key;
// Meta value (for simple meta queries)
if( !empty( $meta_value ) )
$args['meta_value'] = $meta_value;
// If Post IDs
if( $id ) {
$posts_in = array_map( 'intval', explode( ',', $id ) );
$args['post__in'] = $posts_in;
}
// If Exclude Current
if( $exclude_current )
$args['post__not_in'] = array( get_the_ID() );
// Post Author
if( !empty( $author ) )
$args['author_name'] = $author;
// Offset
if( !empty( $offset ) )
$args['offset'] = $offset;
// Post Status
$post_status = explode( ', ', $post_status );
$validated = array();
$available = array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash', 'any' );
foreach ( $post_status as $unvalidated )
if ( in_array( $unvalidated, $available ) )
$validated[] = $unvalidated;
if( !empty( $validated ) )
$args['post_status'] = $validated;
// If taxonomy attributes, create a taxonomy query
if ( !empty( $taxonomy ) && !empty( $tax_term ) ) {
// Term string to array
$tax_term = explode( ', ', $tax_term );
// Validate operator
if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
$tax_operator = 'IN';
$tax_args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $tax_term,
'operator' => $tax_operator
)
)
);
// Check for multiple taxonomy queries
$count = 2;
$more_tax_queries = false;
while(
isset( $original_atts['taxonomy_' . $count] ) && !empty( $original_atts['taxonomy_' . $count] ) &&
isset( $original_atts['tax_' . $count . '_term'] ) && !empty( $original_atts['tax_' . $count . '_term'] )
):
// Sanitize values
$more_tax_queries = true;
$taxonomy = sanitize_key( $original_atts['taxonomy_' . $count] );
$terms = explode( ', ', sanitize_text_field( $original_atts['tax_' . $count . '_term'] ) );
$tax_operator = isset( $original_atts['tax_' . $count . '_operator'] ) ? $original_atts['tax_' . $count . '_operator'] : 'IN';
$tax_operator = in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) ? $tax_operator : 'IN';
$tax_args['tax_query'][] = array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms,
'operator' => $tax_operator
);
$count++;
endwhile;
if( $more_tax_queries ):
$tax_relation = 'AND';
if( isset( $original_atts['tax_relation'] ) && in_array( $original_atts['tax_relation'], array( 'AND', 'OR' ) ) )
$tax_relation = $original_atts['tax_relation'];
$args['tax_query']['relation'] = $tax_relation;
endif;
$args = array_merge( $args, $tax_args );
}
// If post parent attribute, set up parent
if( $post_parent ) {
if( 'current' == $post_parent ) {
global $post;
$post_parent = get_the_ID();
}
$args['post_parent'] = intval( $post_parent );
}
// Set up html elements used to wrap the posts.
// Default is ul/li, but can also be ol/li and div/div
$wrapper_options = array( 'ul', 'ol', 'div' );
if( ! in_array( $wrapper, $wrapper_options ) )
$wrapper = 'ul';
$inner_wrapper = 'div' == $wrapper ? 'div' : 'li';
$listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $original_atts ) );
if ( ! $listing->have_posts() )
return apply_filters( 'display_posts_shortcode_no_results', wpautop( $no_posts_message ) );
$inner = '';
while ( $listing->have_posts() ): $listing->the_post(); global $post;
$image = $date = $author = $excerpt = $content = '';
if ( $include_title )
$title = '<a class="title" href="' . apply_filters( 'the_permalink', get_permalink() ) . '">' . get_the_title() . '</a>';
if ( $image_size && has_post_thumbnail() )
$image = '<a class="image" href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), $image_size ) . '</a> ';
if ( $include_date )
$date = ' <span class="date">' . get_the_date( $date_format ) . '</span>';
if( $include_author )
$author = apply_filters( 'display_posts_shortcode_author', ' <span class="author">by ' . get_the_author() . '</span>' );
if ( $include_excerpt )
$excerpt = ' <span class="excerpt-dash">-</span> <span class="excerpt">' . get_the_excerpt() . '</span>';
if( $include_content ) {
add_filter( 'shortcode_atts_display-posts', 'be_display_posts_off', 10, 3 );
$content = '<div class="content">' . apply_filters( 'the_content', get_the_content() ) . '</div>';
remove_filter( 'shortcode_atts_display-posts', 'be_display_posts_off', 10, 3 );
}
$class = array( 'listing-item' );
$class = sanitize_html_class( apply_filters( 'display_posts_shortcode_post_class', $class, $post, $listing, $original_atts ) );
$output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $author . $excerpt . $content . '</' . $inner_wrapper . '>';
// If post is set to private, only show to logged in users
if( 'private' == get_post_status( get_the_ID() ) && !current_user_can( 'read_private_posts' ) )
$output = '';
$inner .= apply_filters( 'display_posts_shortcode_output', $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class );
endwhile; wp_reset_postdata();
$open = apply_filters( 'display_posts_shortcode_wrapper_open', '<' . $wrapper . $wrapper_class . $wrapper_id . '>', $original_atts );
$close = apply_filters( 'display_posts_shortcode_wrapper_close', '</' . $wrapper . '>', $original_atts );
$return = $open;
if( $shortcode_title ) {
$title_tag = apply_filters( 'display_posts_shortcode_title_tag', 'h2', $original_atts );
$return .= '<' . $title_tag . ' class="display-posts-title">' . $shortcode_title . '</' . $title_tag . '>' . "\n";
}
$return .= $inner . $close;
return $return;
}
/**
* Turn off display posts shortcode
* If display full post content, any uses of [display-posts] are disabled
*
* #param array $out, returned shortcode values
* #param array $pairs, list of supported attributes and their defaults
* #param array $atts, original shortcode attributes
* #return array $out
*/
function be_display_posts_off( $out, $pairs, $atts ) {
$out['display_posts_off'] = true;
return $out;
}
/**
* Convert string to boolean
* because (bool) "false" == true
*
*/
function be_display_posts_bool( $value ) {
return !empty( $value ) && 'true' == $value ? true : false;
}
You'll want to edit you $output variable which is on line 243 on the code you've given above.
A simple amendment to add a static url will do fine, something like this:
$static_link = 'http://www.test.com/test';
$output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $author . $excerpt . $content . 'Read more' . '</' . $inner_wrapper . '>';
Amend this to your requirements, say adding a proper link from the database.
Hope this helps.

PHP display sibling pages as a set image

I am trying to customise the following code so it displays all sibling pages of the current page, but instead of outputting them as the title, they are output as bulletpoints (styled bulletpoints so an image, 'images/bulletpoint.gif'). I have the following code that displays them as the names, but can't seem to customise them to display as an image. Any ideas?
Many thanks
<ul class="subnav_right">
<?php
global $post;
$current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
wp_list_pages( array(
'title_li' => '',
'child_of' => $current_page_parent,
'depth' => '1' )
);
?>
</ul>
You can customize using walker.I have done it using walker. check following code:
Walker Class:
class Custom_Walker extends Walker_Page {
function start_el( &$output, $page, $depth, $args, $current_page = 0 ) {
if ( $depth )
$indent = str_repeat("\t", $depth);
else
$indent = '';
extract($args, EXTR_SKIP);
$css_class = array('page_item', 'page-item-'.$page->ID);
if ( !empty($current_page) ) {
$_current_page = get_post( $current_page );
if ( in_array( $page->ID, $_current_page->ancestors ) )
$css_class[] = 'current_page_ancestor';
if ( $page->ID == $current_page )
$css_class[] = 'current_page_item';
elseif ( $_current_page && $page->ID == $_current_page->post_parent )
$css_class[] = 'current_page_parent';
}
elseif ( $page->ID == get_option('page_for_posts') ) {
$css_class[] = 'current_page_parent';
}
$css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
$icon_class = get_post_meta($page->ID, 'icon_class', true); //Retrieve stored icon class from post meta
$output .= $indent . '<li class="' . $css_class . '">';
$output .= '<a href="' . get_permalink($page->ID) . '">' . $link_before;
if($icon_class){ //Test if $icon_class exists
$output .= '<span class="' . $icon_class . '"></span>'; //If it exists output a span with the $icon_class attached to it
}
if($depth!=0){
$output .= apply_filters( 'the_title', '', $page->ID );
}else {
$output .= apply_filters( 'the_title', $page->post_title, $page->ID );
}
$output .= $link_after . '</a>';
if ( !empty($show_date) ) {
if ( 'modified' == $show_date )
$time = $page->post_modified;
else
$time = $page->post_date;
$output .= " " . mysql2date($date_format, $time);
}
}
}
Pass walker in argumnent like:
wp_list_pages( array( 'title_li' => '',
'child_of' => $current_page_parent,
'depth' => '1',
'walker' => new Custom_Walker())
);

Getting images from a page in wordpress

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;
}

Categories