I'm trying to limit the title into 1 word only
I tried using this code but it doesn't return anything..
<?php
$title = get_the_title();
$names = explode(' ', $title);
echo $names[0];
?>
This is where I'm trying to implement the code If I change the get_the_title() into $names[0] but it doesn't return anything.
$postheader .= '<h3 class="entry-title">' . get_the_title() . '</h3>';
What is in the content of $title? The explode/split should work in that fashion, so there are a couple of things you can do to try to debug:
First, check the content of $title. You can do this as simply as outputting is and viewing source: make sure it has actual spaces, and not non-breaking spacing, or that the field does not start with a space.
Second, make sure your first code block is actually getting executed by echo'ing something you can check for in source, or any other method to ensure it is running. Good luck!
Related
I am using the repeater field of Wordpress' advanced custom fields to make the following content dynamic on my site.
Image of how the content looks while marked up statically
This is how I have marked up the content using plain html.
<div class="container">
<h2>A few general rules of thumb</h2>
<p><span>1.</span>Hallways and transitional spaces are best painted...</p>
<p><span>2.</span>It is best to keep expensive furnishings...</p>
<p><span>3.</span>If you want furnishings and accessories...</p>
<p><span>4.</span>Neutral furnishings and accessories?</p>
</div>
The paragraphs have been styled by setting their display to flex so that the span (number) is on the left and the paragraph is on the right.
This is how my code now looks with PHP.
<div class="container">
<h2><?php echo $container3title['title'];?></h2>
<?php
// Check rows existexists.
if(have_rows('rules')):
// Loop through rows.
while(have_rows('rules')) : the_row();
// Load sub field value.
$ruleNumber = get_sub_field('ruleNumber');
$ruleDetails = get_sub_field('ruleDetails');
// Do something...
echo '<span>' . $ruleNumber . '</span>';
echo '<p>' . $ruleDetails . '</p>';
// End loop.
endwhile;
// No value.
else :
// Do something...
endif;
?>
</div>
The problem is that I do not know how to echo the php so that the paragraphs aren't displayed as two block elements that are stacked on top of one another. I want it to be marked up the same way as I have marked it up using plain html so that my CSS will style it accordingly.
I tried the following but it didn't work.
echo '<p>' '<span>' . $ruleNumber . '</span>'; . $ruleDetails . '</p>';
Can someone please tell me how to put this together correctly? Thank you
Just store the span in a variable and concatenate it in:
$span = '<span>' . esc_html($ruleNumber) . '</span>';
echo '<p>' . $span . esc_html($ruleDetails) . '</p>';
I threw some escapes in there, too. It is generally recommended to always escape user-generated input when outputting, just in case. And even though you might “know” that the number is always numeric, it doesn’t really hurt to escape it anyway, and it makes it easier to scan for unescaped values.
I have a website where users can write posts, however im haveing trouble echoing the post along with its title.
Heres My Code:
Post.inc.php:
echo '<input type="button" value="Read More"
onclick="window.location=\'read_more.php?start=' .
urlencode($row['post']) . ' \';" />';
echo "</p>";
Read_more.php:
<?php
date_default_timezone_set('America/New_York');
include 'post.inc.php';
?>
<?php
$start = (!empty($_GET['start']) ? $_GET['start'] : false);
echo '<p>. $title .<br> ' . $start . '<p>';
?>
Only the post is being echoed and not the title. How would I go about fixing this problem?
-Thanks in advance
As Chris said, this is very vulnerable to XSS attacks, you need to sanitize the $_GET['start'] value before echoing it. Also, passing the entire post in the url is not a great idea. If you are storing the post in a database you are better off sending an ID value of some sort through the URL, validating/sanitizing it and retrieving the post directly from the database to be displayed.
I cannot see where you have given $title a value. It is not defined in any of the code you have provided. This means it cannot display anything when echoed.
Also, the structure of your echo is incorrect.
echo '<p>'. $title .'<br>'. $start .'</p>';
is what you are looking for here. notice the single quotes are closed before $title and re-opened before the break tag.
variable will not be expanded when it occur in single quoted strings.You can find this notice in PHP Manual
so,
echo '<p>. $title .<br> ' . $start . '<p>'
should be modified to
echo '<p>'. $title .'<br>'. $start .'</p>';
or use double quoted instead,
"<p>{$title}<br>{$start}</p>"
As Matt said, echoing the $_GET['start'] directly will make your site be vulnerable,which can result in attacks like phishing.
In a general way, there are some operations like filter,type check to sanitize the value gained from frontend. The PHP Manual provides a lot of function to do this,intval(),PCRE,mysqli_escape_string(),array_filter(),htmlspecialchars()...
IMHO,you should use a SQL server like MySQL,SQLite,or others to storage the data from frontend which can help your site work better.
MySql: I have my products table set up as follow:
pg_id | pg_name
1 | Pizza's calzone
2 | Kids menu
Php: Echo out the html while looping through the records in the MySQL table.
<?php do { ?>
<li>
<?php echo "<a href=". "products.php?p_group=" .$row_getproductnames[ 'pg_name'] . ">"; ?>
<?php echo $row_getproductnames[ 'pg_name']; ?>
</a>
</li>
<?php } while ($row_getproductnames=mysql_fetch_assoc($getproductnames)); ?>
My hyperlink: The link to the products.php page should look like this for records with white space in it. This post and reference the product names correctly in the products page.
http://127.0.0.1/products.php?p_group=Pizza's calzone
But it truncates after the white space to
http://127.0.0.1/products.php?p_group=Pizza's
I have checked numerous samples like using in the place of the white space, Html encryption or decryption etc. Still having problem with getting the string to link correctly. Any help would be appreciated.
You need to quote the href with double quotes:
echo "<a href=\"products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "\">"
If you use single quotes or no quotes then the ' in pg_name is misunderstood by the browser.
Your not using quotes? I don't know for sure this is causing it but usually with any parsing issues quotes will fix it.
Try replacing this line:
<?php echo "<a href='products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "'>"; ?>
If you are trying to create a valid URL, you can will want to replace the spaces with a + or %20. Either will do. I also suggest removing the apostrophes:
$new_url = str_replace(" ","+", $old_url); //Replace space
$new_url = str_replace("'","", $new_url ); //Remove apostrophe
Edit:
If you are needing to use the name parameter to retrieve an item from the database, you can do it by 're-replacing' the space and apostrophe characters at the other end like this:
Build the url:
$new_url = str_replace(" ","+", $old_url); //Replace space
$new_url = str_replace("'","APOSTROPHE", $new_url ); //Remove apostrophe
Then at the page where you will perform the SELECT query:
$product_name = str_replace("+"," ", $product_name); //Put spaces back
$product_name = str_replace("APOSTROPHE","'", $product_name ); //put apostrophes back
There are however much easier ways to send values to other pages such as sending a POST request
So I'm making comment section for my page, and I'm trying to make them work properly.
The problem is that when I save comment to DB, for example like this:
My comment
{
ohh
}
and when displaying it like echo $comment, I get:
My comment { ohh }
So I fixed this with:
nl2br(preg_replace('/(\r)|(\n)/', '<br>', $comment->text));
Now I get:
My comment
{
ohh
}
But I need to display the first variant. When I vardie it gives me:
string(36) "My comment
{
ohh
}"
In db it's storing comment with all indents.
So question: How do I correctly display comment with whitespaces. And also extra question, how I store these comments to DB to allow only 4 sequent whitespaces??, it doesn't matter on the beginning or in the middle.
One solution (possibly not ideal) would be to wrap the text in <pre></pre>, e.g.:
<pre><?php echo $comment; ?></pre>
Another solution (also possibly not ideal) would be to replace spaces with an HTML non-breaking space. You can wrap it around your "newlines to <br>" function:
str_replace(' ', ' ', preg_replace('/(\r)|(\n)/', '<br>', $comment->text))
You can convert whitespaces to before inserting into the database.
$_POST['code_field'] = str_ireplace(' ' , ' ' , $_POST['code_field']);
If you are allowing for editing of comments then convert back into regular spaces.
$row['code_field'] = str_ireplace(' ' , ' ' , $row['code_field']);
Confused by the title? hehe. Not sure how to explain this one, but I think my snippet of code should explain things a little easier.
This is what I'm trying to pass through the $data variable. div is displayed as it should be, but the echo statement inside (which I need) is NOT displayed.
Where am I messing up?
$data['packagename'] = '<div class="somedoodoo"> echo $row->subscription </div>';
You can just use string concatenation:
$data['packagename'] = '<div class="something">' . $row->subscription . '</div>';
you can't execute code inside a string like that, plus, it's the wrong quotes:
$data['packagename'] = <<<EOL
<div class="somedoodoo">{$row->subscription}</div>
EOL;
relevant docs on heredocs: http://php.net/heredoc