Use value inside another PHP function - php

In WordPress i'm currently using this function from a plugin <?php the_field('event_boat'); ?> to output the post ID for the selected field, which happens to be 5755 in this example.
As the plugin only allows me to output the post ID is it possible to incorporate the value from that function inside <?php echo get_permalink(); ?> to get the permalink based on the post ID?

You can pass the ID as a parameter in the get_permalink function, either by storing the ID value in a new variable, or just passing in the ACF-function directly as a parameter.
$post_id = get_field('event_boat');
echo get_permalink($post_id) // echoes out link for ID 5755
I'm using get_field() instead of the_field() because the_field() will echo out the value, We just want to pass it along.
We might aswell just do:
echo get_permalink(get_field('event_boat'));

This should work fine. :)
<?php $a = get_permalink(get_field('event_boat')); echo $a; ?>

<?php $a =get_permalink(get_field('event_boat')); echo $a; ?>

This should work fine. :)
<?php echo get_permalink(get_field('event_boat')); ?>

Related

Adding content between a PHP shortcode

I have a shortcode in a theme that displays only when a user is logged out. Usually, I would use the following shortcode to achieve this
[logged_out]Content[/logged_out]
How would I achieve this in PHP?
<?php echo do_shortcode("[logged_out]"); ?>
Can you just try the code like below,
<?php
$string = 'Hello world';
echo do_shortcode('[logged_out]'.$string.'[/logged_out]');
?>
you can assign any value to the variable $string ;
Or you can directly use like this,
<?php
echo do_shortcode('[logged_out]Hello world[/logged_out]');
?>
For more information please refer,
https://developer.wordpress.org/reference/functions/do_shortcode/
you can use following way in your php file.
echo do_shortcode('[logged_out] your content [/logged_out]');

how to print the content in wordpress php by passing the string

how to print the content in wordpress by passing the string php.I have written the following code but it print all the content including image.I want it to print only particular text.
<?php
$content = get_the_content('Read more');
print $content;
?>
This depends on whether or not you're in the Loop - if you are then your code should work - see more here: https://codex.wordpress.org/The_Loop
However, if you are outside the loop and want to pass in a post ID as a paramter, you can refer to this post as everything is explained very well in there: https://wordpress.stackexchange.com/questions/51741/get-post-content-from-outside-the-loop
$page_id = 302;
$page_object = get_page( $page_id );
echo $page_object->post_content;
Use the_excerpt for only showing small content with read more
<?php $short_desc= get_the_excerpt();
echo substr($short_desc,0,200);?>

Syntax for using echo inside an echo (wordpress slider)

this is the piece of code I'm not sure how to deal with:
<?php echo get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="InsertHere"]'); ?>
I simply want to insert the first echo instead of InsertHere. The first echo should output the content of a custom field. The second should recall the slider with the specific number inserted in the custom field. When trying different possibilities I only get errors.
Can anybody help?
Thank you :)
Don't echo the first value, use it directly inside your second echo.
<?php
echo do_shortcode('[smartslider2 slider="'.get_post_meta($post->ID, 'smartslider2', true).'"]');
?>
Or you can easily put that first value in a variable and use that variable in the second line
<?php $slider = get_post_meta($post->ID, 'smartslider2', true); ?>
<?php echo do_shortcode('[smartslider2 slider="'.$slider.'"]'); ?>

site_url with path and custom variable

I'm not very familiar with PHP and have been trying my hardest to figure out how to create this URL. So far, this is working:
<?php echo site_url($p->post_title) ?>
Where post title is defined by the Mapify.it Wordpress plugin. The result is:
http://siteurl.com/post_title
What I'd like to do is add a string before it, ideally ?s= or /search/, but when I try to add this before $p->post_title I'm still generating the above URL. Variations such as:
<?php echo site_url('?s=', $p->post_title) ?>
<?php echo site_url('/search/', $p->post_title) ?>
produce http://siteurl.com/?s= and ignore the variable. Nothing seems to do what I want.
What am I doing wrong?
Hope you need the following url format,
http://siteurl.com/?s=Here come the post title
So,
<?php echo site_url("?s=".$p->post_title) ?>
OR
<?php echo site_url("/search/".$p->post_title) ?>
should work.
Found it!
<?php echo site_url('?s='), $p->post_title ?>
Instead of adding custom URL Parameters directly, I'd suggest you to use WordPress built-in function add_query_arg(), it's more cleaner.
Here is an usage example:
$url = get_site_url();
$params = array(
's' => $p->post_title
);
echo add_query_arg($params, $url);
You can specify multiple parameters this way.
For ref: Check add_query_arg()

Using subsrt() with method the_title()

I'm trying to substring the title which it is called via the method the_title() .
Here are 2 things I did to do it but they both failed.
first try: <?php echo substr(the_title(),1,15) ?>
second try: <?php $new_title = the_title() ; echo substr($new_title, 1,15) ?>
They both didn't work. when I used the second try I still get the full title.
Note: I'm trying to implement this on a Wordpress script, also it's for my php practice.
Thanks in Advance.
The function the_title() does not return the title as string but outputs it by default.
Use the_title('','', 1) which makes it return the title or the alternative function get_the_title() instead.

Categories