I want to add a newline in a textarea. I tried with < p> tag but it's not working. Can you help me to insert a newline in a textarea? Please find the code above for a generic contact form. Where should I add tags for line breaks here?
public function cs_form_textarea_render($params = '') {
global $post, $pagenow;
extract($params);
if ( $pagenow == 'post.php' ) {
$cs_value = get_post_meta($post->ID, 'cs_' . $id, true);
} else {
$cs_value = $std;
}
if ( isset($cs_value) && $cs_value != '' ) {
$value = $cs_value;
} else {
$value = $std;
}
$cs_rand_id = time();
if ( isset($force_std) && $force_std == true ) {
$value = $std;
}
$html_id = ' id="cs_' . sanitize_html_class($id) . '"';
$html_name = ' name="cs_' . sanitize_html_class($id) . '"';
if ( isset($array) && $array == true ) {
$html_id = ' id="cs_' . sanitize_html_class($id) . $cs_rand_id . '"';
$html_name = ' name="cs_' . sanitize_html_class($id) . '_array[]"';
}
$cs_required = '';
if ( isset($required) && $required == 'yes' ) {
$cs_required = ' required="required"';
}
$cs_output = '<div class="' . $classes . '">';
$cs_output .= ' <textarea' . $cs_required . ' rows="5" cols="30"' . $html_id . $html_name . ' placeholder="' . $name . '">' . sanitize_text_field($value) . '</textarea>';
$cs_output .= $this->cs_form_description($description);
$cs_output .= '</div>';
if ( isset($return) && $return == true ) {
return force_balance_tags($cs_output);
} else {
echo force_balance_tags($cs_output);
}
}
Just add a "\n" char somewhere between your text area tags
$cs_output .= ' <textarea' . $cs_required . ' rows="5" cols="30"' . $html_id . $html_name . ' placeholder="' . $name . '">' . sanitize_text_field($value) . "\n" . "this text is on a new line". '</textarea>';
You will try to add "\n" before your end position of the tag. You need to concatenate Like ."\n".
I am trying to also display the category of a related post, below one article.
I am using Newsmag theme.
The code that builds my related posts is this:
class td_module {
var $post;
var $title_attribute;
var $title;
var $href;
var $td_review; //review meta
var $category;
//constructor
function __construct($post) {
//this filter is used by td_unique_posts.php - to add unique posts to the array for the datasource
apply_filters("td_wp_boost_new_module", $post);
$this->post = $post;
$this->title = get_the_title($post->ID);
$this->title_attribute = esc_attr(strip_tags($this->title));
$this->href = esc_url(get_permalink($post->ID));
$this->category = '';
if (has_post_thumbnail($this->post->ID)) {
$this->post_has_thumb = true;
} else {
$this->post_has_thumb = false;
}
//get the review metadata
$this->td_review = get_post_meta($this->post->ID, 'td_review', true);
}
and the part that displays the related articles is this:
$buffy .= '<div class="td-module-thumb">';
if (current_user_can('edit_posts')) {
$buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>';
}
$buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
$buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
$buffy .= '<span class="td-module-thumb-category">'.$this->category.'</span>';
....................................
$this->category was added by me. I am trying to get the data from wp_terms table and display the categories of each of the related posts.
I am new to WordPress(actually, this is the first time I touch WordPress code).
Thank you
This should work with your existing code:
$buffy .= '<div class="td-module-thumb">';
$related_category = get_the_category($this->post->ID);
if (current_user_can('edit_posts')) {
$buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>';
}
$buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
$buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
$buffy .= '<span class="td-module-thumb-category">'.$related_category[0]->cat_name.'</span>';
Or if you need the category to be a link use:
$buffy .= '<div class="td-module-thumb">';
$related_category = get_the_category($this->post->ID);
if (current_user_can('edit_posts')) {
$buffy .= '<a class="td-admin-edit" href="' . get_edit_post_link($this->post->ID) . '">edit</a>';
}
$buffy .='<a href="' . $this->href . '" rel="bookmark" title="' . $this->title_attribute . '">';
$buffy .= '<img width="' . $td_temp_image_url[1] . '" height="' . $td_temp_image_url[2] . '" itemprop="image" class="entry-thumb" src="' . $td_temp_image_url[0] . '" ' . $attachment_alt . $attachment_title . '/>';
$buffy .= '<span class="td-module-thumb-category">'.$related_category[0]->cat_name.'</span>';
You will probably want to move $related_category = get_the_category($this->post->ID); to where you have $this->category = ''; in your first pasted code segment
I can't figure out how to write a ternary operator for the ending of this line (i.e. "location" and "description"). I've defined the $location and $description variables but I'm getting an error that says unexpected $location variable.
Basically, for both location and description, I'm trying to say: If the field has been filled out, display the field. If not, don't print anything.
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . get_sub_field('location') ? '<em>'$location'</em>' : ''; . get_sub_field('description') ? '<hr><span class="details">Details [+]</span><div class="details-display">'$description'</div>' : ''; . '</div></div></div>';
Here is the full context:
if( have_rows('slides', $frontpage_id) ):
while ( have_rows('slides', $frontpage_id) ) :
the_row();
$group = get_sub_field('group');
$count = 1;
$i = 0;
$nav .= '<ul id="nav_'.$group.'" style="display: none;">';
$content .= '<div class="image-data-container image-container-'. $group .'">';
while( have_rows('images') ) : the_row(); $count++;
$image_url = get_sub_field('image');
$location = get_sub_field('location');
$description = get_sub_field('description');
$isize = #getimagesize(get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url);
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . get_sub_field('location') ? '<em>'$location'</em>' : ''; . get_sub_field('description') ? '<hr><span class="details">Details [+]</span><div class="details-display">'$description'</div>' : ''; . '</div></div></div>';
$nav .= '<li >' . get_sub_field('title') . '</li>';
$i++;
endwhile;
$content .= '</div>';
$nav .= '</ul>';
endwhile;
endif;
?>
Other the fact that your $content variable is a formatting nightmare. The ternary operator syntax returns result based on the condition and it should be separated from your $content attribute
$someVariable = (get_sub_field('location')) ? '<em>'.$location.'</em>' : '';
Now use that some variable inside your $content variable instead
Here is your code with comments:
while( have_rows('images') ) : the_row(); $count++;
$image_url = get_sub_field('image');
// you have already defined the location variable here
// It also means and I am assuming they your get_sub_field is returning a string
// If you are receiving a boolean or null value than and than only than you can do your ternary condition call of
// get_sub_field('location') ? '<em>'$location'</em>' : ''
// and if you are receiving a string field then you will have to explicitly check the condition
// something like this (get_sub_field('location') === '') ? '<em>'$location'</em>' : ''
// I am not that familiar with wordpress framework or the ACF plugin but it looks like get_sub_field will return something so you actually wont need the ternary condition
$location = get_sub_field('location');
// Similar case with description
$description = get_sub_field('description');
$isize = #getimagesize(get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url);
// After removing the ternary condition your content variable will look like this
$content .= '<div data-order="' . $count . '" data-group="' . $group . '"><div class="img-wrap"><img data-iwidth="'.$isize[0].'" data-iheight="' . $isize[1] . '" class="myslide" data-lazy="' . get_template_directory_uri() . '/img/slides/' . strtolower($group) . '/' . $image_url . '" alt="' . get_sub_field('title') . '"><div class="slide-caption"><strong>' . get_sub_field('title') . '</strong><hr>' . '<em>' . $location . '</em>' . '<hr><span class="details">Details [+]</span><div class="details-display">' . $description . '</div>' . '</div></div></div>';
$nav .= '<li >' . get_sub_field('title') . '</li>';
$i++;
endwhile;
You know you can always make it more readable
// You know instead of all the concatenation you can do this instead
$templateDirURI = get_template_directory_uri();
$groupLower = strtolower($group);
$title = get_sub_field('title');
$content .= "<div data-order='{$count}' data-group='{$group}'><div class='img-wrap'><img data-iwidth='{$isize[0]}' data-iheight='{$isize[1]}' class='myslide' data-lazy='{$templateDirURI}/img/slides/{$groupLower}/{$image_url}' alt='{$title}'><div class='slide-caption'><strong>{$title}</strong><hr><em>{$location}</em><hr><span class='details'>Details [+]</span><div class='details-display'>{$description}</div></div></div></div>";
Updated Code
$content .= "<div data-order='{$count}' data-group='{$group}'><div class='img-wrap'><img data-iwidth='{$isize[0]}' data-iheight='{$isize[1]}' class='myslide' data-lazy='{$templateDirURI}/img/slides/{$groupLower}/{$image_url}' alt='{$title}'><div class='slide-caption'><strong>{$title}</strong>";
// Assuming the user did not submit location information
if($location !== "")
$content .= "<hr><em>{$location}</em><hr>";
if($description !== "")
$content .= "<span class='details'>Details [+]</span><div class='details-display'>{$description}</div>";
$content .= "</div></div></div>";
I'm trying to create a dynamic navigation menu.
All allowed possibilities are defined in an array.
I'm trying to count the array, split it in half and print each half of the array into seperate div's. One on the left side of the screen and the other on the right side.
this is what i've got:
function menu_main() {
$count = count($GLOBALS['menu']);
$count = round($count / 2);
$menu_1 = array_slice($GLOBALS['menu'], 0, $count);
$menu_2 = array_slice($GLOBALS['menu'], $count++);
echo ' <div id="menu_1">' . "\n";
foreach($menu_1 as $page) {
echo ' <div class="menu_items">' . "\n";
echo ' <a class="menu" href="index.php?page=' . $page . '">' . $page . '</a> .' . "\n"; //Create menu left
echo ' </div>'. "\n";
}
echo ' </div>'. "\n";
echo ' <div id="menu_2">' . "\n";
foreach($menu_2 as $page) {
echo ' <div class="menu_items">' . "\n";
echo ' <a class="menu" href="index.php?page=' . $page . '">' . $page . '</a> .' . "\n"; //Create menu right
echo ' </div>'. "\n";
}
echo ' </div>'. "\n";
}
Is there a way to do this with itteration?
This works but it looks way to messy.
Ok, a couple of things:
$GLOBALS['menu'];
Why use $GLOBALS, or global $menu, for that matter? Why not pass what you need to the function?
function menuMain(array $menu)
{
$count = floor(count($menu)/2);
Would make a lot more sense. Also:
$menu_1 = array_slice($GLOBALS['menu'], 0, $count);
$menu_2 = array_slice($GLOBALS['menu'], $count++);//<== why the increment?
Why are you incrementing $count, only to then not use it anymore? You're post-incrementing the value, which means that if $count is 10, then the code above is evaluated to:
$menu_1 = array_slice($GLOBALS['menu'], 0, 10);
$menu_2 = array_slice($GLOBALS['menu'], 10);//10+1 happens AFTER the array_slice call is made
Also don't use "\n", because line-feeds are system dependent, PHP has a predefine constant for this reason: PHP_EOL. Use it (that's an order).
And don't concatenate what you echo, just comma-separate it:
echo 'foo', 'bar', PHP_EOL;
echo 'foo'.'bar'.PHP_EOL;
both may produce the same time, but the first version is faster, because the strings are pushed to the output, without first concatenating them into a new string.
Not that it matters here, because functions should return something, not echo it.
Anyway, your question: is there a way to do this with iteration? Yes, and you're already iterating (foreach iterates an array/object).
An alternative approach would be:
function mainMenu (array $menu)
{
$return =' <div id="menu_1">' . PHP_EOL;//functions shouldn't echo
//start with div 1
for($i = 0, $j= count($menu), $count = floor($j/2); $i<$j;++$i)
{
if ($i == $count)
{//reached half-way marker, close div 1, open div 2
$return .= '</div>'.PHP_EOL.'<div id="menu_2">'.PHP_EOL;
}
$return .= ' <div class="menu_items">' . PHP_EOL
.' <a class="menu" href="index.php?page=' . $menu[$i] . '">' . $menu[$i]
. '</a> .' .PHP_EOL;
}
return $return.'</div>';//close div2 and return
}
That should give you the exact same output as before, only it only uses a single loop, doesn't rely on globals, and doesn't echo things, so you call it like this:
$menu = array('your', 'global', 'array');
echo mainMenu($menu);//pass the array, echo what it returns
My solution if it helps.
$menu = array ("link1","link2","link3","link4","link5");
function menu_main()
{
global $menu;
$count = count($menu);
$menu_items = round($count / 2);
//initialize the divs for menus
$output_menu[0] = ' <div id="menu_1">' . "\n";
$output_menu[1] = ' <div id="menu_2">' . "\n";
$menu_count = 0;
foreach($menu as $link)
{
if ($menu_count < $menu_items)
{
//what enters in first menu
$output_menu[0] .= ' <div class="menu_items">' . "\n";
$output_menu[0] .= ' <a class="menu" href="index.php?page=' . $link . '">' . ucfirst($link) . '</a> .' . "\n"; //Create menu left
$output_menu[0] .= ' </div>'. "\n";
}
else
{
//what enters in second menu
$output_menu[1] .= ' <div class="menu_items">' . "\n";
$output_menu[1] .= ' <a class="menu" href="index.php?page=' . $link . '">' . ucfirst($link) . '</a> .' . "\n"; //Create menu left
$output_menu[1] .= ' </div>'. "\n";
}
$menu_count++;
}
//close the divs for menus
$output_menu[0] .= ' </div>'. "\n";
$output_menu[1] .= ' </div>'. "\n";
//return array with content for menus
return $output_menu;
}
$menu_main = menu_main(); // get content in variable
print_r($menu_main[0]); //print 1st menu
print_r($menu_main[1]); //print 2nd menu
i have a function like this. i want reset the $output var first call.
function create_tree($tree_array, $reset = TRUE, $ul_class = FALSE) {
if($reset) unset($output); // NOT WORK!!!
static $output = '';
$class = '';
if ($ul_class) {
$class = ' class="' . $ul_class . '"';
}
$output .= '<ul' . $class . '>' . PHP_EOL;
foreach ($tree_array as $v) {
$output .= '<li>' . $v['name'] . '' . PHP_EOL;;
if (isset($v['children'])) {
create_tree($v['children'], false);
}
$output .= '</li>' . PHP_EOL;
}
$output .= '</ul>' . PHP_EOL;
return $output;
}
$output doesn't magically exist at that point in the function; it magically retains its value when the declaration is seen again.
if ($reset)
$output = '';