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.
Related
I'm a little lost here, hoping that someone can help. I'm using the Meta Box plugin for WordPress, and I'm trying to create a process for the user to select an option from a predefined list, and then assign a URL to that option as a link. Im trying to define the URL in a variable, and then call it in a function, but I'm still a little green on PHP syntax. this is my code now:
<?php
$article_url= rwmb_meta('orion_2016_article_url', 'type=URL');
if (rwmb_meta('orion_2016_article_source') != '') {
echo '<a href= ("$article_url") target=blank>';
echo rwmb_meta('orion_2016_article_source');
echo '</a>';} ?> on <?php the_date(); ?>
Since the options are already predefined, it seems like assigning a random URL to one of the options should be pretty simple. Hopefully this makes sense!
You need to to place variables you wish to echo inside double quotes or simply concatenate strings using . as in my example. Note that I didn't check the plugin's specific syntax, only general PHP syntax.
<?php
$article_url= rwmb_meta( 'orion_2016_article_url', 'type=URL' );
if (rwmb_meta('orion_2016_article_source') != '') {
echo '' . rwmb_meta( 'orion_2016_article_source' ); . '';
} ?> on <?php the_date(); ?>
I'm trying to find the category ID of a post and then use it in the short code below. To find the category id I'm using <?php the_category_ID(); ?>, but i'm not sure how to use the output from category id and replace it in include_categories=. i.e. i want to replace number 4 with the current category ID i got from <?php the_category_ID(); ?>.
<?php echo do_shortcode('[include_categories="4"]'); ?>
i tired doing the following, but it didn't work. Any ideas will be appreciated.
<?php echo do_shortcode('[include_categories="<?php the_category_ID(); ?>"]'); ?>
I know i can't use php inside another php code, but i'm not sure how to place the category id between the quotation.
Thanks.
A couple of things: (1) you can't have nested <?php ?> tags, (2) the_category_ID() has been deprecated since WordPress 0.71. You'll want to use get_the_category() instead.
The correct solution would be:
<?php echo do_shortcode('[include_categories="' . get_the_category() . '"]'); ?>
The dots . "concatenate" the string with the returned values of get_the_category(). You can read more about string operators in the PHP docs.
Try with -
<?php echo do_shortcode('[include_categories="'.the_category_ID().'"]'); ?>
Use this to add php verial in short code:
<?php $category=the_category_ID();
echo do_shortcode("[include_categories=$category]"); ?>
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')); ?>
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()
This may be a simple one for you PHP experts out there. I need to give a certain <h1> to a post else show the page/post title.
I have this so far, it works if it is on a single post page, but when I am on a different page it just shows 'the_title' instead of the page title. I think its basically about calling a php function inside an already open php tag, if that makes sense. Here is the code:
<?php
if ( is_single() ) {
echo 'News';
} else {
echo the_title();
}
?>
The Wordpress tag for the page title is <?php the_title ?>
You are echoing 'the_title' as a string, you need to actually execute the function like so:
if ( is_single() ) {
echo '<h1>News</h1>';
} else {
echo '<h1>' . the_title() . '</h1>';
}
Note the closing quote to halt the string, and the . to concatenate the WordPress function the_title(), and then another to join the ending <h1> tag.
A cleaner way is to add the tags inside the function itself, like this:
<?php the_title('<h1>', '</h1>'); ?>
No need for 'echo'.