return acf image src in twig - php

i cant get the acf images to return.
in my single-work.twig (view file) i have
<img src="{‌{ Image(post.meta(‘laptop_image')).src }}" />
and in the single-work.php (controller file) template i have
$context[‘laptop_image'] = get_field(‘laptop_image’);
i also tried
in single-work.php (controller file)
$post->laptop_image = new Timber\Image($post->laptop_image);
in single-work.twig (view file)
<img src="{‌{ post.laptop_image.src | resize(500, 300) }}" />
most of the time i get “unknown” as the result.. I used to get the image id returned but i cant remember how i got that… Any help would be great.
full single-work.php
$context = Timber::context();
$timber_post = Timber::query_post();
$context['post'] = $timber_post;
$context['text_with_image_title'] = get_field('headline');
$context['text_with_image_text'] = get_field('description');
$post->laptop_image = new Timber\Image($post->laptop_image);
$context['ipad_image'] = get_field('ipad_image');
$context['iphone_image'] = get_field('iphone_image');
$context['text_with_image_btn_link'] = get_field('work_url');
$context['text_with_image_btn_label'] = 'Go to ' . $timber_post->post_title . ' Website';
$context['text_with_image_btn_target'] = '_blank';
if ( post_password_required( $timber_post->ID ) ) {
Timber::render( 'single-password.twig', $context );
} else {
Timber::render( array( 'single-' . $timber_post->ID . '.twig', 'single-' . $timber_post->post_type . '.twig', 'single-' . $timber_post->slug . '.twig', '/pages/single-work.twig' ), $context );
}
var_dump($post->laptop_image); //returns image id
full code of work.twig
<div class="col-6 text-with-image__column text-with-image__column--image">
<div class="text-with-image__wrapper">
<img src="{{ post.laptop_image.src | resize(500, 300) }}" />
{{dump(post.laptop_image)}} //returns nothing
</div>
</div>

i realized i was using a macro and therefore
<img src="{‌{ Image(post.laptop_image).src }}" /> works fine, though it needs to be applied as the macro value of the new instance. Not just apart of the template.

Related

WordPress get_the_post_thumbnail_url() not working when posting from front-end

Whenever I post from the dashboard, I can grab the featured image URL with get_the_post_thumbnail_url() and display it in a email with wp_mail(). However, when I try this from the front-end, I get a empty URL.
Ive tried numerous frontend plugins but none works when it comes to the featured image. The rest of the post's fields display fine in the emails.
Im using the following code:
// POST MAILMAN
// Add the hook action
add_action('transition_post_status', 'send_new_post', 10, 3);
// Listen for publishing of a new post
function send_new_post($new_status, $old_status, $post) {
if('publish' === $new_status && 'publish' !== $old_status && $post->post_type === 'post') {
$latestPost_ID = $post->ID;
$latestPost_ImgUrl = get_the_post_thumbnail_url($latestPost_ID, 'full');
$latestPost_Category = get_the_category($latestPost_ID);
$latestPost_Category_Name = esc_html( $latestPost_Category[0]->name );
$latestPost_Url = get_post_permalink($latestPost_ID);
$latestPost_Title = get_post_field( 'post_title', $latestPost_ID );
$latestPost_Excerpt = get_post_field( 'post_excerpt', $latestPost_ID );
$todaydate = date("l") . ", " . date("j") . " " . date("F") . " " . date("Y");
to display the image in the email:
<td class="fluid-img" style="font-size:0pt; line-height:0pt; text-align:left;"><img src="' . esc_url($latestPost_ImgUrl) . '" border="0" width="650" height="366" alt="" /></td>
When done front-end vs backend:
https://imgur.com/a/TqP9L6a
Some plugins I tried:
https://wordpress.org/plugins/accesspress-anonymous-post/
https://wordpress.org/plugins/wp-user-frontend/
I find getting the Featured Image via it's attachment record is more reliable, for either public-facing or admin requests. Try this:
$attachment = wp_get_attachment_image_src(get_post_thumbnail_id($latestPost_ID), 'full', true);
$featuredImageUrl = $attachment[0];

placeholder picture wordpress

How to display a placeholder image if there's no image set up in WordPress? The picture is displayed by the $property_image var. It is a featured image from a custom post (not a WordPress original one).
It is defined by the code below, displays the featured image of the post if set up, if not, only shows the alt tag content ("Upcoming picture..."):
$property_image = dreamvilla_mp_get_device_image($property_ID);
Below my code from a WordPress theme:
$html .= '<div class="property-list-list property-listing-list-full">
<div class="col-xs-12 col-sm-4 col-md-4 property-list-list-image">
<a href=' . esc_url(get_permalink($property_ID)) . '>
<img '. $property_image . ' alt="Upcoming picture..." class="img-responsive">
</a>
' . $featured_proeprty_label . '
' . $featured_proeprty_label_icon . '
' . dreamvilla_mp_agent_favorites_property_icon($property_ID) . '
</div>
I'd place this:
if( empty( $property_image ) ) {
$property_image = 'src="your_fallback_image"';
}
directly after:
$property_image = dreamvilla_mp_get_device_image($property_ID);
What to be used to determine if the variable is empty depends on what is stored in the variable.
Directly before the img tag, you could insert this:
<?php
if($property_image == '') {
$property_image ="scr='http://placehold.it/300x200/f0a'";
}
?>
It checks whether that variable is empty and if yes, puts a links to a placeholder image into it, including the src attribute.

Display images based of tags in wordpress blog posts

What I want to do is have images display on each blogposts, the images that will be displayed will be what the post was tagged with, for example I will tag 1 post with 2 tags, design and print, I would like 2 small images to appear on that posts page.
This is the code that I have managed to scrape together, the else statement is displaying so a post will have the same amount of default-author.jpg showing as there are tags.
This makes me think that something in the foreach is not working but I'm still trying to learn and this is puzzling me, any help would be much appreciated.
<div class="image_wrap">
<?php
$posttags = get_the_tags(); // Get articles tags
$home = get_bloginfo('url'); // Get homepage URL
// Check if tag-slug.png exists and display it.
if ($posttags) {
foreach($posttags as $tag) {
$upload_dir = wp_upload_dir();
$image_relative_path = "/wp-content/uploads/2017/06/" . $tag->slug .".png";
$image_path = $upload_dir['basedir'] . $image_relative_path;
$image_url = $upload_dir['baseurl'] . $image_relative_path;
if (file_exists($image_path)) {
echo '<a href="' . $home . '/' . $tag->slug . '" /><img title="' . $tag->name . '" alt="' . $tag->name . '" src="' . $image_url . '" /></a>';
// If no image found, output something else.
} else { ?>
<img src="<?php bloginfo('template_directory'); ?>/img/default-author.jpg" title="image missing"/>
<?php }
}
}
?>
Does anyone have any ideas on how I can achieve the effect I want, images to be displayed on a blogpost [in wordpress] depending on what tag it has?

slug its not working in codeigniter

i want url slug like this
tkd/index.php/article/77/Kejurnas-2013
but when i click button like this
tkd/index.php/article/77/Kejurnas%202013
what wrong with my code. this is my view
<div class="post-buttons-wrap">
<div class="post-buttons">
<a href="<?php echo base_url('index.php/article/' . intval($dt->id) . '/' . e($dt->slug)) ; ?>" class="ui black label">
Read More
</a>
</div><!-- end .post-buttons -->
</div>
and this is my controller
public function index()
{
$this->data['berita'] = $this->mberita->get_berita();
$this->data['halaman'] = $this->mhalaman->get_profil();
dump('Page!');
// Fetch the page template
$this->data['page'] = $this->page_m->get_by(array('slug' => (string) $this->uri->segment(1)), TRUE);
count($this->data['page']) || show_404(current_url());
// Fetch the page data
$method = '_' . $this->data['page']->template;
if (method_exists($this, $method)) {
$this->$method();
}
else {
log_message('error', 'Could not load template ' . $method .' in file ' . __FILE__ . ' at line ' . __LINE__);
show_error('Could not load template ' . $method);
}
$this->data['contents'] = $this->data['page']->template;
$this->load->view('template/wrapper_mahasiswa', $this->data);
}
please help me what to do. thank you.
You have to use the helper "URL" and use the functions "url_title()" and "convert_accented_characters()" if you need to convert some special characters.
http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html for more info
Example:
In your controller code, you will capture your form inputs. then you have to create the slug with a line like this:
$event['slug'] = url_title(convert_accented_characters($event['eventName']),'-',TRUE);
This line will return you a slug separated by "-", setting all the words to lowercase and deleting all special characters too.
try this- use rawurlencode while working with strings in url-
<a href="<?php echo base_url('index.php/article/' . intval($dt->id) . rawurldecode('/') . e($dt->slug)) ; ?>" class="ui black label">
Read More
</a>

The plugin generated xxx characters of unexpected output during activation [duplicate]

This question already has answers here:
The plugin generated X characters of unexpected output during activation (WordPress)
(25 answers)
Closed 4 years ago.
I am creating WordPress plugin to display Total Twitter counter & Feed Subscriber. You can manage it via widget.
I am getting this error.
The plugin generated 123 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
<?php
/*
* Plugin Name: Twitter & RSS Stats
* Version: 1.0
* Plugin URI: http://sss.com/
* Description: Facebook, Twitter & RSS Social Stats widget tutorial.
* Author: Ajay Patel
* Author URI: http://sss.com/
*/
addHeaderCode();
function addHeaderCode() {
echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/TRR_Stats/css/style.css" />' . "\n";
}
/*******************************/
/* Create table */
/********************************/
function my_plugin_create_table()
{
// do NOT forget this global
global $wpdb;
// this if statement makes sure that the table doe not exist already
if($wpdb->get_var("show tables like TRR_Stats") != 'TRR_Stats')
{
$sql = "CREATE TABLE TRR_Stats (
id mediumint(9) NOT NULL,
rss_email tinytext NOT NULL,
twitter tinytext NOT NULL,
rss tinytext NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
// this hook will cause our creation function to run when the plugin is activated
register_activation_hook( __FILE__, 'my_plugin_create_table' );
class FTRWidget extends WP_Widget
{
/**
* Declares the FTRWidget class.
*
*/
function FTRWidget(){
$widget_ops = array('classname' => 'widget_hello_world', 'description' => __( "Example widget demoing WordPress 2.8 widget API") );
$control_ops = array('width' => 300, 'height' => 300);
$this->WP_Widget('helloworld', __('Twitter & RSS Social Stats'), $widget_ops, $control_ops);
}
/**
* Displays the Widget
*
*/
function widget($args, $instance){
extract($args);
$rss_email = empty($instance['rss_email']) ? 'webdesignergeeks' : $instance['rss_email'];
$twitter = empty($instance['twitter']) ? 'webdesignergeek' : $instance['twitter'];
$rss = empty($instance['rss']) ? 'webdesignergeeks' : $instance['rss'];
# Featch Data from table
global $wpdb;
$item_info = $wpdb->get_row("SELECT * FROM TRR_Stats WHERE id=1;");
$rss_email_f = $item_info->rss_email;
$url = file_get_contents('https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=24thfloor');
preg_match( '/circulation="(\d+)"/', $url, $matches );
if ( $matches[1] )
$rss_f = $matches[1] . " Subscribers";
else
echo "0";
$twit = file_get_contents('http://twitter.com/users/show/'.$twitter.'.xml');
preg_match( '/\<followers_count\>(\d+)\<\/followers_count\>/', $twit, $matches );
if ( $matches[1] )
$twitter_f = $matches[1] . " Followers";
else
$twitter_f = "0";
echo '
<div class="sidebarContainer" id="sidebarSubscribe">
<a target="_blank" href="http://twitter.com/'.$twitter.'" class="subscribeSidebarBox" id="followTwitter">
<span class="icon"><img src="'.get_bloginfo('url').'/wp-content/plugins/TRR_Stats/img/twitter.png" alt="Twitter" /></span>
<span class="title">Follow Us on Twitter</span>
<span class="count">'.$twitter_f.'+</span>
</a>
<a target="_blank" href="http://feeds.feedburner.com/'.$rss.'" class="subscribeSidebarBox" id="subscribeRSS">
<span class="icon"><img src="'.get_bloginfo('url').'/wp-content/plugins/TRR_Stats/img/rss_feed.png" alt="RSS"/></span>
<span class="title">Subscribe to our RSS feed</span>
<span class="count">'.$rss_f.'+</span>
</a>
<a target="_blank" href="http://feedburner.google.com/fb/a/mailverify?uri='.$rss_email_f.'" class="subscribeSidebarBox" id="subscribeEmail">
<span class="icon"><img src="'.get_bloginfo('url').'/wp-content/plugins/TRR_Stats/img/rss_email.png" alt="rss_email" /></span>
<span class="title">Subscribe for updates via</span>
<span class="count">EMAIL</span>
</a>
</div>';
# After the widget
echo $after_widget;
}
/**
* Saves the widgets settings.
*
*/
function update($new_instance, $old_instance){
$instance = $old_instance;
$instance['rss_email'] = strip_tags(stripslashes($new_instance['rss_email']));
$instance['twitter'] = strip_tags(stripslashes($new_instance['twitter']));
$instance['rss'] = strip_tags(stripslashes($new_instance['rss']));
global $wpdb;
//Insert First time
$wpdb->insert( 'TRR_Stats', array(
'id' => 1,
'rss_email' => $instance['rss_email'],
'twitter' => $instance['twitter'],
'rss' => $instance['rss']
)
);
//Rest Update Data
global $wpdb;
$wpdb->update( 'TRR_Stats',
array(
'rss_email' => $instance['rss_email'],
'twitter' => $instance['twitter'],
'rss' => $instance['rss']
),
array(
'id' => 1
)
);
return $instance;
}
/**
* Creates the edit form for the widget.
*
*/
function form($instance){
//Defaults
$instance = wp_parse_args( (array) $instance, array('rss_email'=>'', 'twitter'=>'engiguide', 'rss'=>'www.rss_email.com/engiguide') );
$rss_email = htmlspecialchars($instance['rss_email']);
$twitter = htmlspecialchars($instance['twitter']);
$rss = htmlspecialchars($instance['rss']);
# Output the options
# Twitter
echo '<p style="text-align:right;"><label for="' . $this->get_field_name('twitter') . '">' . ('Twitter:') . ' <input style="width: 200px;" id="' . $this->get_field_id('twitter') . '" name="' . $this->get_field_name('twitter') . '" type="text" value="' . $twitter . '" /></label></p>';
echo '<p style="padding-left: 110;">i.e: webdesignergeeks</p>';
# Rss
echo '<p style="text-align:right;"><label for="' . $this->get_field_name('rss') . '">' . __('Rss:') . ' <input style="width: 200px;" id="' . $this->get_field_id('rss') . '" name="' . $this->get_field_name('rss') . '" type="text" value="' . $rss . '" /></label></p>';
echo '<p style="padding-left: 110;">i.e: webdesignergeeks</p>';
# Rss Email
echo '<p style="text-align:right;"><label for="' . $this->get_field_name('rss_email') . '">' . ('Rss Email:') . ' <input style="width: 200px;" id="' . $this->get_field_id('rss_email') . '" name="' . $this->get_field_name('rss_email') . '" type="text" value="' . $rss_email . '" /></label></p>';
echo '<p style="padding-left: 110;">i.e: webdesignergeeks</p>';
}
}// END class
/**
*
* Calls 'widgets_init' action after the Hello World widget has been registered.
*/
function TTRInit() {
register_widget('FTRWidget');
}
add_action('widgets_init', 'TTRInit');
?>
I got extra "xxx" white spaces after the final php closing tag. Removed it and the warning is gone. Those white spaces were the "unexpected output".
Remove space from start of tags. remove addHeaderCode(); from top and add this code add_action('wp_head', 'addHeaderCode'); to your file after addHeaderCode() function. its definitely resolve your problem.
it was a UTF8 related issue. I converted it with Notepad++ in UTF8 but I had to conert it in "UTF8 (without BOM)".
Now I don't have any notice.
You should try this:
remove white spaces
run function plugin_error()
in wp_config.php file set wp_DEBUG true , that will help you to see errors
This might help you.
I don't think you should be sending any output in addHeaderCode(), this occurs far too early. See Actions Run During a Typical Request.
Instead, try hooking to wp_head to add your stylesheet, which should be triggered between <head>...</head>.
Try wrapping the message with __() like __("Hello, world!") did the trick for me.
You can remove update() function without this function it will work fine and will do same thing you are doing.
I had the same issue with WooCommerce Smart Coupons plugin. I removed all extra new lines from plugin php files and saved them in "UTF8 (without BOM)" (as #Umaie Hamid mentioned). This fixed the issue.
I've fixed this by removing the spaces in the code using the "edit plugin" in the WP admin. I think when WP save the changes it also does all the necessary settings like saving the file as UTF-8.
I Was getting same error just remove white spacing in your code.

Categories