Toggle excerpt and content onclick - Wordpress - php string - php

I am altering an wordpress theme to display my blog posts and I've been trying to add a toggle between excerpt and content when clicking on 'Read More'.
I have been unsuccessful on many attempts.
Any help would be greatly appreciated.
$post_data = "<article class='post style-2$sticky_class'>";
$post_data.= "<div class='blog-item'>";
$post_data .= "<header class='col-md-4 col-xs-12 no-padding'>";
if(is_sticky(get_the_ID()) && strlen(anps_header_media(get_the_ID(), $image_class)) > 0 ) {
$post_data .= "<div class='absolute stickymark'><div class='triangle-topleft hovercolor'></div><i class='nav_background_color fa fa-thumb-tack'></i></div>";
}
$post_data .= "<a href='".get_permalink()."'>".anps_header_media(get_the_ID(), $image_class)."<div class='cat_banner'>".$cat_name."</div></a>";
$post_data .= "</header>";
$post_data.= "<div class='archive_blog_info col-md-8'>";
if (is_sticky(get_the_ID()) && strlen(anps_header_media(get_the_ID(), $image_class)) < 1 ) {
$post_data .= "<a href='".get_permalink()."' title='".get_the_title()."'><h1><i class='fa fa-thumb-tack hovercolor'></i> ".get_the_title()."</h1></a>";
$post_data .= "<h1>".get_the_date()."</h1>";
}
else {
$post_data .= "<a href='".get_permalink()."' title='".get_the_title()."'><h1>".get_the_title()."</h1></a>";
}
$post_data .= "<div class='post-excerpt'>".$excerpt_text."</div>";
$post_data .= "<div class='post-content'>".$content_text."</div>";
$post_data .= '<a class="btn btn-sm style-4 read" href="">'.__("Read more", 'accounting').'</a>';
$post_data .= '<a class="btn btn-sm style-4 read-less" href="">'.__("Read less", 'accounting').'</a>';
$post_data .= '<a class="news_link btn btn-sm style-4" style="float:right!important;" href="/category/'.$cat_name.'/">'.__("More ".$cat_name." articles >>", 'accounting').'</a>';
$post_data .= "</div>";
$post_data .='</div>';
$post_data .= "</article>";
echo $post_data;

You need to use javascript to handle the click events of read more or read less. I prefer jquery. I have prepared a fiddle.
[http://jsfiddle.net/at36xwa8/4/]

Related

div is not on the code

I'm generating html code with a string this way
foreach ($busquedas as $busqueda) {
$checked = $busqueda->porDefecto ? "checked" : ' ';
$radio_html = "<input type='radio' radio-id='".$busqueda->id."' name='default' class='radio-default' value='Por defecto' checked =".$checked." > Por defecto";
$html .= "<div class='col-md-12 search-div'>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'>".$busqueda->nom."</div>";
$html .= "<div class='col-md-6'>".$_SESSION['user_rol'] == 0?$radio_html:''."</div>";//if $radio_html is shwon the paren div col-md-6 is not shown
$html .="</div>";
$html .= "<div class='col-md-12'>";
$html .= "<div class='col-md-6'><button class='btn btn-default load_search_btn' search_id='".$busqueda->id."'>Cargar</button></div>";
$html .= "<div class='col-md-6'><button class='btn btn-default delete_search_btn' search_id='".$busqueda->id."'>Eliminar</button></div>";
$html .="</div>";
$html .="</div>";
}
When $radio_tml is shown the parent div with class col-md-6 is not on the code but if $radio_html is shown the div is shown too,I thought some tag is not closed but I can't see it
For me your code implies that when you have
$_SESSION['user_rol'] == 0
the div section after that is not closed
You should add brackets as IncredibleHat was saying or add the </div> in the if statement (and not only in the else)

Adding MailChimp Shortcode in Theme with PHP

I have the following website: https://www.ajagrawal.com
Currently in my website, I am trying to edit the hero section and replace the name, email and button fields with a custom wordpress form that was made with the plugin. Right now the emails are stored in Wordpress, but I want them in Mailchimp.
I know which code in my theme that form pertains to which is the following:
if(function_exists('newsletter_form')):
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= '<form method="post" action="'.home_url('/').'?na=s" onsubmit="return newsletter_check(this)">';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="text" name="nn" required="" placeholder="'.esc_html($name_placehodler).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-user"></span></div>';
$output .= '</div>';
$output .= '<div class="c-input-3-wrapper">';
$output .= '<input class="c-input type-3" type="email" name="ne" required="" placeholder="'.esc_html($email_placeholder).'">';
$output .= '<div class="c-input-3-icon"><span class="lnr lnr-envelope"></span></div>';
$output .= '</div>';
$output .= '<input class="newsletter-submit c-btn type-1 size-4 full" type="submit" value="'.esc_html($btn_text).'">';
$output .= '</form>';
$output .= '</div>';
$output .= '</div>';
endif;
Here is my mailchimp shortcode that represents the form.
[mc4wp_form id="2198"]
I've tried editing the code and inserting it multiple ways and even ended up crashing the site and still no luck. Considering my goal, is this the right approach (putting the shortcode in the PHP code)?
If so, how can this be done?
You can use the do_shortcode function.
Examples in the documentation:
// Use shortcode in a PHP file (outside the post editor).
echo do_shortcode( '' );
// In case there is opening and closing shortcode.
echo do_shortcode( '[iscorrect]' . $text_to_be_wrapped_in_shortcode . '[/iscorrect]' );
// Enable the use of shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );
// Use shortcodes in form like Landing Page Template.
echo do_shortcode( '[contact-form-7 id="91" title="quote"]' );
// Store the short code in a variable.
$var = do_shortcode( '' );
echo $var;
So in your case something like:
echo do_shortcode( '[mc4wp_form id="2198"]' );
should do the trick.
EDIT:
Something like this should work:
$output .= '<div class="row">';
$output .= '<div class="col-sm-6 col-sm-offset-6 col-md-4 col-md-offset-6">';
$output .= do_shortcode( '[mc4wp_form id="2198"]' );
$output .= '</div>';
$output .= '</div>';
Then do whatever you want with output - probably will have to echo it...

How to concatenate a string with a variable to include an image

for($i ; $i < $rowcount ; $i++){
/*here i want to add an image to my page.*/
echo"<li class='streep mix'><img src='img/'".$productnaam."'.jpg'\>
<button>
<i class='fa fa-2x fa-shopping-cart' aria-hidden='true'></i>
</button>
</li>";
echo"<li class='gap'></li>";
}
i dont understand how to concatenate the image part
Try like this :
echo "<li class='streep mix'><img src='img/" . $productnaam . ".jpg'\>";
echo "<button>";
echo "<i class='fa fa-2x fa-shopping-cart' aria-hidden='true'></i>";
echo "</button>";
echo "</li>";
echo "<li class='gap'></li>";
Hope it helps.
Most probably; You are looking for something like this:
<?php
// OBSERVE THE PART: <img src='img/" . $productnaam . ".jpg' />
// AS WELL AS THE FORWARD SLASH BEFORE CLOSING THE <IMG /> TAG
echo"<li class='streep mix'><img src='img/" . $productnaam . ".jpg' />
<button>
<i class='fa fa-2x fa-shopping-cart' aria-hidden='true'></i>
</button>
</li>";
echo"<li class='gap'></li>";
However, It would be ideal for you (at this stage) to build up your Output-String in sequences so that you could easily spot any typos or potential quote-related errors without much fuss.
<?php
$output = "";
for($i; $i < $rowcount; $i++){
// EMBED AN IMAGE WITHIN A LIST ITEM
$output .= "<li class='streep mix'>";
$output .= "<img src='img/{$productnaam}.jpg' />"; //<== FORWARD SLASH HERE
// THEN, ADD THE SHOPPING-CART BUTTON
$output .= "<button>";
$output .= "<i class='fa fa-2x fa-shopping-cart' aria-hidden='true'></i>";
$output .= "</button>";
$output .= "</li>";
$output .= "<li class='gap'></li>";
}
echo $output;

How to output a list of social icons in custom function?

I have an unordered list of social icons I would like to output, but am stumped on how I would I do it with my current function (use an array, $figure?) -
So my raw code is currently (which checks if a field is filled, and if so shows the icon on the front-end) -
<?php if ( get_post_meta( $post->ID, 'member_twitter', true ) ) { ?>
<li><i class="fa fa-twitter"></i></li>
<?php } if ( get_post_meta( $post->ID, 'member_facebook', true ) ) { ?>
<li><i class="fa fa-facebook"></i></li>
<?php } if ( get_post_meta( $post->ID, 'member_googleplus', true ) ) { ?>
<li><i class="fa fa-google-plus"></i></li>
<?php } ?>
Now I would like to insert these list items into the following (and looping through them) -
if ($layout_style == "style-one") {
$html .= "<div class='team-member'>";
$html .= "<h3 class='member-name'>$title</h3>";
$html .= "<div class='member-role'>$member_role</div>";
$html .= "<ul class='social-icons'>";
$html .= "</ul>";
$html .= "</div>";
}
Have tried a few unsuccessful ways already.
thanks
I think I know what you are asking for. Let me know if I missed the mark:
$socials = array(
array('twitter', 'Twitter', 'twitter'),
array('facebook', 'FaceBook', 'facebook'),
array('googleplus', 'Google Plus', 'google-plus'),
);
if ($layout_style == "style-one") {
$html .= "<div class='team-member'>";
$html .= "<h3 class='member-name'>$title</h3>";
$html .= "<div class='member-role'>$member_role</div>";
$html .= "<ul class='social-icons'>";
foreach ($socials as $social) {
if (get_post_meta($post->ID, 'member_' . $social[0], true)) {
$html .= "<li><i class=\"fa fa-" . $social[2] . "\"></i></li>";
}
}
$html .= "</ul>";
$html .= "</div>";
}

Issue on output's return value

Currently I am using a file with css and all.I have a problem as i am using php output between many html tags. The menu consist of the hard coded pages which is included with the php require_once. However, i also want to add in some pages from my database.
The Problem: The menu only shows 1 page name from my database when i have 2. It is displaying the 2nd page's name i have made. However clicking on it, it calls up the content from the 1st page.
The page name from my database is added using php output after the <ul id="menu-toc">
The page's content from my database is added at the bottom of the "The Page" section.
update: I've changed the placing of the while loop before the to after the
Pattern of css
<div class="bb-item" id="abscess">
<div class="content">
<div class="scroller">
Content
</div>
</div>
</div>
The Page
function find_condition() {
global $connection;
$query = "SELECT * ";
$query .= "FROM pages ";
$query .= "WHERE visible = 1 ";
$query .= "AND subject_id = 2 ";
$query .= "ORDER BY position ASC";
$page_set = mysqli_query($connection, $query);
confirm_query($page_set);
return $page_set;
}
$two = find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
}
echo $output;
?>
</ul>
</div>
<div class="bb-custom-wrapper">
<div id="bb-bookblock" class="bb-bookblock">
<?php require_once("abscess.php"); ?>
<?php require_once("apthousulcer.php"); ?>
<?php require_once("bad breath.php"); ?>
<?php
$two= find_condition();
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
$content = $page['content'];
$output .= "<div class=\"bb-item\" id=\"";
$output .= $pagearray;
$output .= "\">";
$output .= "<div class=\"content\">";
$output .= "<div class=\"scroller\">";
$output .= "<h2>";
$output .= $pagearray;
$output .= "</h2>";
$output .= $content;
$output .= "</div>";
$output .= "</div>";
$output .= "</div>";
}
echo $output;
?>
You're starting a new <ul> each time through the while loop. You should just do that once, before the loop. You're also reinitializing $output each time through the loop, but not printing it in the loop. So you're just printing the last assignment of output after the loop is done.
$two = find_condition();
?>
<ul id="menu-toc" class="menu-toc">
<li class="menu-toc-current">
ABSCESS</li>
<li>APTHOUS ULCER</li>
<li>BAD BREATH</li>
<?php
while($page = mysqli_fetch_assoc($two)) {
$pagearray = $page['menu_name'];
?>
<?php
$output = "<li>";
$output .= "<a href=\"";
$output .= $pagearray;
$output .= "\">";
$output .= $pagearray;
$output .= "</a></li>";
echo $output;
}
?>

Categories