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
Related
I have added a function into my child theme to override the original function. This functions purpose is to display the products meta data in a list below the product.
The only part i can get past is the base64 image.
My current code is :
function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html = '';
$args = wp_parse_args(
$args,
array(
'before' => '<ul class="wc-item-meta"><li>',
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => true,
'autop' => false,
'label_before' => '<strong class="wc-item-meta-label">',
'label_after' => ':</strong> ',
)
);
foreach ( $item->get_all_formatted_meta_data() as $meta_id => $meta ) {
$value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
$key = wp_kses_post( $meta->display_key );
if($key === "Tie Preview"){
$b64 = explode(',', $value);
$image = base64_decode($b64[1]);
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . '<img src="data:image/png;base64,' . $image . '" />';
}else{
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . $value;
}
}
if ( $strings ) {
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}
$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );
if ( $args['echo'] ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $html;
} else {
return $html;
}
}
As you can see in the image below the image tag appears but the base64 image doesn't display.
Here is a screenshot of the data url loaded in a browser:
Has anyone else come up with this issue.
I have figured out the issue!!!!
instead of the <img> i replaced it with and <a>. I decided to do this as i could view the tool tip to see the out of the href attribute was.
as you can see somehow there is a <p> being added. iv now taken the value and used substr() to remove the <p> at the front and end of the string.
My new code now looks like so:
function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html = '';
$args = wp_parse_args(
$args,
array(
'before' => '<ul class="wc-item-meta"><li>',
'after' => '</li></ul>',
'separator' => '</li><li>',
'echo' => true,
'autop' => false,
'label_before' => '<strong class="wc-item-meta-label">',
'label_after' => ':</strong> ',
)
);
foreach ( $item->get_all_formatted_meta_data() as $meta_id => $meta ) {
$value = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
$key = wp_kses_post( $meta->display_key );
if($key === "Tie Preview"){
$sub1 = substr($value, 3);
$sub2 = substr($sub1, 0, -4);
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . '<img src="' . $sub2 . '" />' . "<a href='" . $sub2 . "' >click here</a>";
}else{
$strings[] = $args['label_before'] . wp_kses_post( $meta->display_key ) . $args['label_after'] . $value;
}
}
if ( $strings ) {
$html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}
$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );
if ( $args['echo'] ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $html;
} else {
return $html;
}
}
I'm no programmer so I'm clueless on solutions.
I have been using CMB2
for a Portfolio/Project custom post type.
I've incorporated a slideshow that uses Group Field metadata for each slide.
On the home page there are 2 posts labeled "Empty Project" & "Test Project 1".
If you click the Empty Project you will be directed to it's single post page, there you will see a ".flexslider" div with a red background. That's the div that I would like to remove if the Group Fields are empty. My that I mean completed remove the div leaving no empty divs instead of changing the background color to white.
If you click "Test Project 1", there will be the images uploaded using Repeatable Group Fields within the "flexslider" slideshow. That is to be the result of Metafields that were saved with Metadata inside of them.
METABOX//
Here is the code I have used to register the repeatable fields, which allows me to insert images and captions for the slideshow.
add_action( 'cmb2_admin_init', 'gallery_metabox' );
function gallery_metabox() {
$prefix = 'gallery_';
/**
* Repeatable Field Groups
*/
$cmb_group = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => __( 'Gallery', 'cmb2' ),
'object_types' => array( 'portfolio', ),
) );
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_group->add_field( array(
'id' => $prefix . 'demo',
'type' => 'group',
'options' => array(
'group_title' => __( 'Image {#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => __( 'Add Another Image', 'cmb2' ),
'remove_button' => __( 'Remove Image', 'cmb2' ),
'sortable' => true, // beta
'closed' => true, // true to have the groups closed by default
),
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => __( 'Image', 'cmb2' ),
'id' => 'image',
'type' => 'file',
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => __( 'Image Caption', 'cmb2' ),
'id' => 'image_caption',
'type' => 'text',
) );
}
I followed this to display meta data for those group fields.
Everything works perfectly fine when I use this chunk of code:
FRONT-END//
<div class="flexslider">
<ul class="slides">
<?php $entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
foreach ( (array) $entries as $key => $entry ) {
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
'class' => 'thumb',
) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
} ?>
</ul>
</div>
but I would very much like to display the .flexslider container + metadata ONLY when data exist. If fields are empty then I would like to display default text or better yet remove the whole div itself.
I tried my best to do research but I can't seem to figure out what is wrong.
I've also tried this chunk of code as well:
ATTEMPT//
<?php $entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
if(empty ($entry)) { echo ''; }
else {
foreach ( (array) $entries as $key => $entry ) {
echo '<div class="flexslider">';
echo '<ul class="slides">';
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
'class' => 'thumb',
) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
echo '</ul>';
echo '</div>';
}
}
?>
The only good thing about the code above is that it definitely removes the div when metafield is empty but if the metadata DOES exist the div is still gone.
EDIT// I tried using "#stweb" code in the answers below:
$entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
foreach ( (array) $entries as $key => $entry ) {
if(empty($entry)){
continue;
}
echo '<div class="flexslider">';
echo '<ul class="slides">';
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick',
null, array( 'class' => 'thumb', ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop(
$entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
echo '</ul>';
echo '</div>';
}
but nothing happens...the red div just sits there instead of disappearing.
I'd basically like to figure out how I can display the first chunk of code ONLY if images were uploaded to Group Field and if not then display nothing at all not even the container div.
Can anyone please explain where I went wrong?
Try this:
$entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
foreach ( (array) $entries as $key => $entry ) {
if(empty($entry)){
continue;
}
echo '<div class="flexslider">';
echo '<ul class="slides">';
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick',
null, array( 'class' => 'thumb', ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop(
$entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
echo '</ul>';
echo '</div>';
}
UPDATE:
foreach ( (array) $entries as $key => $entry ) {
if ( !isset( $entry['image_id'] ) || $entry['image_id'] == '' ) {
continue;
}
echo '<div class="flexslider">';
echo '<ul class="slides">';
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick',
null, array( 'class' => 'thumb', ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop(
$entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
echo '</ul>';
echo '</div>';
}
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'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;
}