How to get the title of wordpress post - php

I know there is a question with similar title but my question is different. How can I get the title of wordpress post. I know the_title() returns AND prints the title of the post. But I don't want it to print. I just want to take it in a PHP variable that I can use later on. Anybody knows?

Look in to the link below which is available in the wordpress docs.
This function will return the title of a post for a given post ID. If the post is protected or private, the word "Protected: " or "Private: " will be prepended to the title. It can be used inside or outside of The Loop. If used outside the loop an ID must be specified.
Quoted from the link below.
https://codex.wordpress.org/Function_Reference/get_the_title

Capture it with the ob_ functions:
ob_start();
the_title()
$title = ob_get_clean();
What this does, is you print it, but only after starting an output buffer, so you print it to that buffer, then get it from the buffer and close the buffer.

Related

PHP remove <body><html>...</html></body> from echo output

I have a php script that does a query in my database and returns a string ( like "2" ). I print it using
print strip_tags('2');
but in the output of my browser I get :
<body><html>2</html></body>
Is there any way to prevent the tags from beiing printed? Is it maybe that the browser auto adds them?
For all those answering about strip_tags (" 2 ");
THIS IS WRONG:
I want a siple version.php
with
echo '2';
and nothing else. It prints the tags too. I don't have the tags and then try to print.
More explanation to those who try to get easy rep
my code is:
$str = '2';
print strip_tags($str);
and it prints
<html><head></head><body>2</body></html>
It is not possible. The browser creates these elements automatically, without it there would not be any text flow(means nothing of this could be made visible). You can just use this variable for any script, it won't include the HTML tags. This is only made by the browser to make it visible for you.
You can use
header("Content-Type: text/plain");
at the beginning of your script, in order to tell the browsers you're only gonna send plain text, not html. This will prevent your browser from automatically adding those html tags.
Then, check what you print (or echo). Here, the body tag should be in html tag.

how to implement read more button in every post of wordpress

I am working on the site http://www.nickthepromisering.com/
Now friends my question is how to limit the Number of words in the wordpress post by adding a read more button. so that only some words are displayed and then when clicked on read more button the remaining post is displayed. I mean is there is any plugin for that?
To only show a portion of a post's content, change the_content(); with the_excerpt(); in your theme.
Then, add read more links like this: Read more
echo implode (" ", array_slice (explode (" ", strip_tags($post->post_content)), 300));
echo 'Read more';
Please note, that this will also remove the HTML, otherwise you might end up with half open tags..

String replace the contents of a div

What I want to do:
I have a div with an id. Whenever ">" occurs I want to replace it with ">>". I also want to prefix the div with "You are here: ".
Example:
<div id="bbp-breadcrumb">Home > About > Contact</div>
Context:
My div contains breadcrumb links for bbPress but I'm trying to match its format to a site-wode bread crumb plugin that I'm using for WordPress. The div is called as function in PHP and outputted as HTML.
My question:
Do I use PHP of Javascript to replace the symbols and how do I go about calling the contents of the div in the first place?
Find the code that's generating the <, and either set the appropriate option (breadcrumb_separator or so) or modify the php code to change the separator.
Modifying supposedly static text with JavaScript is not only a maintenance nightmare, extremely brittle, and might lead to a strange rendering (as users see your site being modified if their system is slow), but will also not work in browsers without (or with disabled) JavaScript support.
You could use CSS to add the you are here text:
#bbp-breadcrumb:before {
content: "You are here: ";
}
Browser support:
http://www.quirksmode.org/css/beforeafter_content.html
You could change the > to >> with javascript:
var htmlElement = document.getElementById('bbp-breadcrumb');
htmlElement.innerHTML = htmlElement.innerHTML.split('>').join('>>').split('>').join('>>')
I don't recommend altering content like this, this is really hacky. You'd better change the ouput rendering of the breadcrumb plugin if possible. Within Wordpress this should be doable.
you can use a regex to match the breadcrumb content.. make the changes on it.. and put it back in the context..
check if this helps you:
$the_existing_html = 'somethis before<div id="bbp-breadcrumb">Home > About > Contact</div>something after'; // let's say this is your curreny html.. just added some context
echo $the_existing_html, '<hr />'; // output.. so that you can see the difference at the end
$pattern ='|<div(.*)bbp-breadcrumb(.*)>(.*)<\/div>|sU'; // find some text that is in a div that has "bbp-breadcrumb" somewhere in its atributes list
$all = preg_match_all($pattern, $the_existing_html, $matches); // match that pattern
$current_bc = $matches[3][0]; // get the text inside that div
$new_bc = 'You are here: ' . str_replace('>', '>>', $current_bc);// replace entity for > with the same thing repeated twice
$the_final_html = str_replace($current_bc, $new_bc, $the_existing_html); // replace the initial breadcrumb with the new one
echo $the_final_html; // output to see where we got

How do you edit the "Posted To Category" part of a Wordpress post to 'linkify' the entire phrase instead of just the category?

Hey guys, I have a pretty basic question (even though I know the title sounds confusing) which is best explained as follows. This code is what I currently have:
<?php if (!is_page()): ?><?php ob_start(); ?><?php printf(__('Posted to %s', 'kubrick'), get_the_category_list(', ')); ?>
Which generates this HTML code:
Posted to Category
As you can see, the category is 'linkified' by this code, however I would like the entire phrase to be the anchor text for the link, like this:
Posted to Category
How would I modify the original PHP code to include the "Posted to" text in the hyperlink's anchor text? Much appreciated! :)
You can use get_the_category() and get_category_link() functions as follows:
<?php
foreach(get_the_category() as $category)
echo 'Posted to '.$category->cat_name.' ';
?>
However you may like to consider what happens when a post is in more than one category: your original code will show a list of category links separated by commas (e.g. "Posted in foo, bar, qux"), whereas this code will show "Posted in [name]" for each category (e.g. "Posted in foo Posted in bar Posted in qux").
It should also be noted that this answer does not use WordPress's __() function to translate the resulting text where appropriate: you may like to consider how best to support non-English speakers.
Also, it's not necessary to close and reopen PHP tags after each contiguous command - just separate them with semicolons.

Cake PHP Link Doesn't Display

I have string like "Venditoris: Beware of Scams » Blog Archive » Trilegiant Complaints ..." in Database but when I try to display it ,It is not displaying.
So,I used html_entity_decode function but still it is not display.
I am Using cakePHP.below is my code to display that links.
echo $html->link(html_entity_decode(
$listing_end_arr[$i]['Listing']['listing_title'],ENT_QUOTES),
$listing_end_arr[$i]['Listing']['listing_url'],
array('target'=>'_blank', 'style'=>'color:'
. $colorArr[$listing_end_arr[$i]['Listing']['listing_sentiment']])) ;
Please Help me.
Check the CakePHP manual if you are using $html->link correctly. If so, var_dump the return value instead of echoing it. If it is empty, do
var_dump( $listing_end_arr[$i]['Listing'] );
to see what the Listing key contains. If the desired content is not in the dump, you know the error is elsewhere; probably in fetching the string from where it is stored.
Also, instead of using array[n][foo][bar][baz], consider assigning the subarray to a variable while looping over the array, e.g. $listing = array[n][foo][bar], so you can just do $listing[baz]. This will dramatically increase readability of your code.
inspect generated html first.. your code should echo a link, maybe it's just not visible (styling, color..).

Categories