WordPress echo image and url - php

I'm trying to get a dynamic image to display the image defined on a variable, and also to have the link to that url to be the same as the permalink for the item.
echo '<img src="'.$thumburl.'" alt="Status">';
Any idea what I'm doing wrong with that code?

code use on your loop:
<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'thumbnail');
?>
<?php echo '<a href="'.get_permalink().'"><img src="'.$image_url[0].'"
alt="Status"></a>'; ?>

You need to use get_permalink() instead of the_permalink()
echo '<img src="'.$thumburl.'" alt="Status">';
get_permalink():
Returns the permalink to a post or page for use in PHP. It does NOT display the permalink and can be used outside of The Loop. On failure returns false.
the_permalink():
Displays the URL for the permalink to the post currently being processed in The Loop. This tag must be within The Loop, and is generally used to display the permalink for each post, when the posts are being displayed. Since this template tag is limited to displaying the permalink for the post that is being processed, you cannot use it to display the permalink to an arbitrary post on your weblog. Refer to get_permalink() if you want to get the permalink for a post, given its unique post id.

You don't seem to tell us what the problem is. I suppose that the image is not displayed. The code is OK.
Double check the image path stored in $thumburl. Inspect the element in a browser to check that img's source and post it here. Are you using an absolute path in $thumburl?

Related

How to get image URL from attachment ID and place it in an image src in Wordpress PHP?

I'm quite new to PHP but my desired functionality is to pull the image attachment URL from it's ID (generated through an ACF Custom Field) and use that as the src for an image.
This is the code that I have right now which should dynamically pull the image URL from the ID but doesn't seem to work:
<?php echo wp_get_attachment_url( the_field('upload_image') ); ?>
This is the code that works but isn't dynamic:
<?php echo wp_get_attachment_url( 257 ); ?>
Note the 257 is the ID of the image - I just need this to be able to be pulled dynamically by appropriating the first code snippet to take that attachment ID from the custom post field (which it should be doing in the first snippet but isn't).
Can anyone help out?
In acf the_field just output the item directly. In your case you need to use get_field function to get the id of the uploaded image. Because get_field returns the item.

Wordpress - Access a custom field value with PHP

I am having trouble accessing a custom field on my Wordpress page.
I have a field for a user to upload an image file. The theme that I am using then displays the value of that image as just the image ID, instead of an image link. I display it using {CUSTOM_FIELD_TestFile} in the page editor.
I want it to display the actual image though, not the ID.
So, I created a PHP snippet that runs on the page, to do the converting.
If I use <img src=" <?php echo wp_get_attachment_url( 1013 )?> "> and I have the image ID hard coded (1013), it works perfectly.
However, I have had no luck getting the value of 'TestFile' into the PHP snippet.
I double checked the user_meta table and the meta_key for this file is indeed TestFile.
The viewing of the page is done usually when a user is not logged in. So, get_current_user_id() isn't an option, which means, I also can't use get_user_meta().
If I use:
<?php
$postId = get_the_ID();
echo $postId;
$fileLink = get_metadata( "user", $postId, "TestFile");
echo $fileLink;
?>
I get "array" as a response. If I change it to get_metadata( "user", $postId, "TestFile", true); I get nothing.
The exact same results if I use get_post_meta($postId, 'TestFile'); or get_post_meta($postId, 'TestFile', true);.
Am I on the completely wrong track? Is there an easier way to do this? Or is there something that I should be doing with that "array" value that is being returned.
Thanks so much for any help you can offer.
If you have added a custom field called TestFile in the editor, you should be able to get that in your template with:
$value = get_post_meta($postId, 'TestFile', true);
I'm not sure what you mean about the user being logged in or out, because custom fields belong to the post and have nothing to do with the user.
Assuming that works, you should be able to add the ID to your image element:
<img src="<?= wp_get_attachment_url($value); ?>">

Properly displaying thumbnails with the_post_thumbnail()

I have created a custom post type named bateau in functions.php. I also created a page where all the posts that belong to the bateau custom post type are displayed, showing some of the most important fields. Finally, when I click on one of those posts, a link sends me to the related custom post page, i.e. a particular type of boat.
The custom post bateau has its own custom fields, a thumbnail, and also its custom taxonomies.
I want to retrieve and display, when I'm on the page of a particular boat, not only the most important but all its custom fields, its thumbnail and its taxonomy.
So, in the functions.php, I have written this filter :
add_filter('the_content','add_text');
function add_text($text) {
global $post;
$text = "";
if($post->post_type == 'bateau') {
$text.= "<h1 class=\"bateau-entry-title\">".get_post_meta( $post->ID, 'bateau_nom', true )."</h1>";
return $text;
}
}
It works fine, provided that I don't write plain HTML text within closing and opening PHP tags, i.e. it only works if I wrap all the HTML in a PHP text var. If I don't do so, the content is also displayed at the beginning of the header, not once, but twice. Strange, isn't it ?
If I add this line :
$text.= "<img class=\"thumb\" src=\"the_post_thumbnail();
the thumbnail displays properly in the "article"...but, guess what, also at the beginning of the header, not once, but twice !!! I just can't find why the thumbnail behaves like this. Anyone can help please ?
In Wordpress, the page you called:
when I'm on the page of a particular boat
is called single. instead of putting a filter on the_content using
add_filter('the_content','add_text');
you would better add whatever you want to the loop. To do so, you may create a file called single-bateau.php (this is a protocol, so you need to name it exactly as this) in your current theme's root directory and there you may have your loop:
if(have_posts()){
while(have_posts()){
the_post();
the_title();
if( has_post_thumbnail() ){
the_post_thumbnail( $thumb_size );
}
the_taxonomies();
the_category();
the_tags();
}
}
I don't think you need the img tag if you're using the_post_thumbnail().
Try doing something like:
if ( has_post_thumbnail() )
the_post_thumbnail();

Wordpress get_permalink($this->get_pageid()) showing wrong url

I am using get_permalink($this->get_pageid()) for getting page url.
my permalink is enables as sample-post. It should be example.com/forum but it is showing example.com/login
Your question is not quite clear because get_pageid() is not a wordpress' native function and you didn't provide any code. Anyways...
Actually get_permalink() expects an optional id as an argument which should be an integer and this is required only when you are using it outside the loop so make sure your $this->get_pageid() returns an integer value. For an example it should be something like this
get_permalink(10) // 10 should be your page/post id
Also you can use
get_permalink(get_page_by_title('Some Page')) // Some Page is page/post title
Alternatively, you can use
global $post;
get_permalink($post->ID); // $post->ID will return the page/post id
when you are outside of the loop. Hope this helps.

putting a php code into another php code! Possible?

i'm a wordpress user and i have a php code, and in that php code there is an area to put a url in: $url = "http://blabla.com"; well in wordpress you can call post permalinks with this code: <?php the_permalink(); ?> What i want to do is putting <?php the_permalink(); ?> instead of http://blabla.com above in the php code. Target is: getting permalinks put them there in php code, and let the php code use them to do its job. Is that possible? If yes, how with an example please...Thank you...
You could assign the return value of get_permalink() to $url:
<?php $url = get_permalink(); ?>
get_permalink() is different from the_permalink() because it doesn't display the link, but just returns it. (Originally this answer naively used the_permalink(), but I did some extra research to be sure.)
The above answer will not work. Use get_permalink().
the_permalink will display it's output
get_permalink will return the value.
<?php $url = get_permalink(); ?>
You need to understand the concept of some Template Tags, since the_permalink fits this category. They are defined especially for use in WordPress Themes. They can be summarize as "a code that instructs WordPress to "do" or "get" something".
Writing the_permalink simply echoes the permalink in your template. It's not something that you get in a php function and manipulates it. It just echoes the html of the information it needs to show.
This is useful for designers working out template files in Wordpress themes: they don't need to understand a lot of programming or a lot of php keywords: they just need to know that writing "the_permalink" gives them the desired html output.
What you're trying to do is get the output from a template tag that already outputs it's value. You need to use other template tag that RETURNS the value you want to use instead of ones that OUTPUTs it.
In your example, you need the get_permalink. Since the_permalink is used in the Loop, you need to provide a post id to your get_permalink function.
There are another examples that fits the same problem domain: for example, I can't manipulate what wp_list_pages returns (because it automatically outputs it's result), so I need get_pages (that RETURNS an array) to manipulate it's result.
Read Wordpress official documentation at Codex. It's great.

Categories