How do I replace this string in WP? - php

There's this code in my wp theme. I know I'm supposed to simply make/add filters ecc to my child theme functions file...but I don't know php. Here's the code:
function astra_get_breadcrumb( $echo = true ) {
if ( ! $echo ) {
return '<div class="ast-breadcrumbs-wrapper">
<div class="ast-breadcrumbs-inner">' .
astra_get_selected_breadcrumb( $echo ) .
'</div>
</div>';
}
?>
<div class="ast-breadcrumbs-wrapper">
<div class="ast-breadcrumbs-inner">
<?php astra_get_selected_breadcrumb( $echo ); ?>
</div>
</div>
<?php
}
In my theme functions file, I just want to add the class "demo" so that the html ends up being like this in the end
<div class="ast-breadcrumbs-wrapper demo">.
As far as I understand, it's a matter of replacing a string, but like I said, I'll only mess it up.

function astra_get_breadcrumb( $echo = true ) {
if ( ! $echo ) {
return '<div class="ast-breadcrumbs-wrapper demo">
<div class="ast-breadcrumbs-inner">' .
astra_get_selected_breadcrumb( $echo ) .
'</div>
</div>';
}
?>
<div class="ast-breadcrumbs-wrapper demo">
<div class="ast-breadcrumbs-inner">
<?php astra_get_selected_breadcrumb( $echo ); ?>
</div>
</div>
<?php
}

Related

Wordpress Shortcodes Appear In Wrong Place

I have a wordpress shortcode to handle some PHP (for custom fields) for my sidebar. The shortcode is below:
add_shortcode('my_further_information_list','my_further_information_list_func');
function my_further_information_list_func(){
$additional_info_1 = get_field( 'additional_info_1');
$additional_info_2 = get_field( 'additional_info_2');
$additional_info_3 = get_field( 'additional_info_3');
if ( $additional_info_1) {
$html = '<li>'.the_field('additional_info_1').'</li>';
}
if ( $additional_info_2) {
$html = '<li>'.the_field('additional_info_2').'</li>';
}
if ( $additional_info_3) {
$html = '<li>'.the_field('additional_info_3').'</li>';
}
return $html;
}
For some reason, when I use this shortcode, it generates the html in completely the wrong place on the page (in the div above where the shortcode is). I haven't come across this before, so any ideas on why this might happen?
You can see the weird place they're appearing here:
shortcode generating in weird place...
This is the theme sidebar code:
<?php if ( x_get_content_layout() != 'full-width' ) : ?>
<aside class="x-sidebar nano" role="complementary">
<div class="max width nano-content">
<?php if ( get_option( 'ups_sidebars' ) != array() ) : ?>
<?php dynamic_sidebar( apply_filters( 'ups_sidebar', 'sidebar-main' ) ); ?>
<?php else : ?>
<?php dynamic_sidebar( 'sidebar-main' ); ?>
<?php endif; ?>
</div>
</aside>
<?php endif; ?>
This is the generated HTML. As you can see, the top widget (also using a shortcode whose function references custom fields (but in a different way - with a loop - as the fields it's referencing are different)) works fine, as does the bottom (just a simple followed by a button). But in the middle widget, the shortcode generates the link and text above the div that the widget is supposed to be in...
<div id="text-8" class="widget widget_text">
<h4 class="h-widget">Related Articles</h4>
<div class="textwidget">
<p>
<ul>
<li>Related Article 1</li>
<li>Related Article 2</li>
<li>Related Article 3</li>
</ul>
</p>
</div>
</div>
Further Information 1Further Information 2
<div id="text-9" class="widget widget_text">
<h4 class="h-widget">Further Information</h4>
<div class="textwidget"><p><ul><li></li><li></li></ul></p></div>
</div>
<div id="custom_html-4" class="widget_text widget widget_custom_html">
<h4 class="h-widget">Feedback</h4>
<div class="textwidget custom-html-widget">
<p>Please provide feedback!<p>
<button>PROVIDE FEEDBACK</button>
</div>
</div>
i think you code should be
add_shortcode('my_further_information_list','my_further_information_list_func');
function my_further_information_list_func(){
$additional_info_1 = get_field( 'additional_info_1');
$additional_info_2 = get_field( 'additional_info_2');
$additional_info_3 = get_field( 'additional_info_3');
$html = "<ul>";
if ( $additional_info_1) {
$html .= '<li>'.$additional_info_1.'</li>';
}
if ( $additional_info_2) {
$html .= '<li>'.$additional_info_2.'</li>';
}
if ( $additional_info_3) {
$html .= '<li>'.$additional_info_3.'</li>';
}
$html .= "</ul>";
return $html;
}
non valid html elements can cause this type of behavior depending on your css. If you require more assistance please provide template code, or at least the snippet around the_content()
edit: the_field is the same as echo get_field. it can't be used to build a string. that's why you use get_field on top :)

Variable Scope Wordpress Query

I am trying to change a CSS class, based on the contents of a variable called $project_cats which contains a category name. While $project_cats appears to be valid throughout my script, it is not valid within one of my if statements, at least that is my assumption.
How can I access $project_cats from within my if statement?
<?php
if ( $query->have_posts() )
{
?>
<div id="myresource">
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<?php
$terms = get_the_terms($post->id,"project-type");
$project_cats = NULL;
if ( !empty($terms) ){
foreach ( $terms as $term ) {
$project_cats .= strtolower($term->name) . ' ';
}
}
//$project_cats does not appear to have content in the below if statement
if ($project_cats == "webinars") {
$iconclass = "iconsmind-Video-4";
}
?>
<div class="nectar-icon-list" data-icon-color="default" data-icon-style="border" data-icon-size="small" data-animate="">
<div class="nectar-icon-list-item">
<div class="list-icon-holder" data-icon_type="icon" style="background-color:#1963af;">
<i class="icon-default-style <?php echo $iconclass; ?>" data-color="default"></i>
</div>
<div class="content">
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php echo $project_cats;?>
</div>
</div>
</div>
<?php
}
?>
</div>
<?php
}
else
{
echo "No Results Found.";
}
?>
I just realized what's happening, in the if statement the name of the category is having a space ' ' appended to it, hence my if statement was never TRUE. Fixed.

Adding Anchor to every module in Joomla 3.1

I am trying to modify the module template code to add an anchor to the title of each module. I have extremely limited PHP knowledge, so any guidance would be very helpful.
I've found posts for doing this with articles, but the code appears to be very different for modules.
This is the code that is in my module template.
function modChrome_basic($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<?php echo $module->content; ?>
<?php endif;
}
function modChrome_standard($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<div class="rt-block <?php if ($params->get('moduleclass_sfx')!='') : ?><?php echo $params->get('moduleclass_sfx'); ?><?php endif; ?>">
<div class="module-surround">
<?php if ($module->showtitle != 0) : ?>
<div class="module-title">
<?php
echo '<h2 class="title">';
if (preg_match("/icon[-]{1,}/i", $params->get('moduleclass_sfx'))) :
echo '<span class="title-icon ' .getIconClass($params->get('moduleclass_sfx')). '"></span>';
endif;
echo $module->title;
echo '</h2>';
?>
</div>
<?php endif; ?>
<div class="module-content">
<?php echo $module->content; ?>
</div>
</div>
</div>
<?php endif;
}
This is the code I tried
if( strlen($this->item->params->get('title')) > 0 ) {
echo '<a name="'.$this->item->params->get('title').'"></a>';
}
I also tried
if( strlen($module->item->params->get('title')) > 0 ) {
echo '<a name="'.$module->item->params->get('title').'"></a>';
}
Joomla has an easy way to customize the module appearance.
You have to use a Custom module chrome.
You have to create a modules.php file under /templates/TEMPLATE_NAME/html/modules.php.
Inside this file you have to put:
<?php
function modChrome_custom( $module, &$params, &$attribs ) {
echo '<div id="' .$params->get( 'moduleclass_sfx' ) .'" >';
if ($module->showtitle)
{
echo '<h' .$headerLevel .'>' .$module->title .'</h' .$headerLevel .'>';
}
echo '<div class="module_content">';
echo $module->content;
echo '</div>';
echo '</div>';
}
?>
In the code above the module id getting moduleclass_sfx value. You could change it and set another module variable.
In order to use this appearence you have to set the appropriate style to the template module calls like: <jdoc:include type="modules" name="user1" style="custom" />
If you want to use another style name you have to change the function from modChrome_custom to modChrome_NEWSTYLE.
Good Luck!

Move menu next to logo instead of below or above in WordPress theme (header.php)

I want to move the menu next to the logo instead of under it in an WordPress theme.
See the actual page I'm working on here: http://wwconsult.no/client/alive/
I found the code for the menus placement in the header.php, here's the snippet:
<?php
if ( $smof_data['head_menu_style'] == "menu_default" ){
if ( has_nav_menu('top_navigation', 'GoGetThemes') ) {
echo '<div id="menu_back">';
menu_back();
echo '</div>';
}
echo'<div class="container">'.$logo.'
<div class="nav'.$navClass.'">
<nav>
'.$parallax_menu.'
</nav>
</div>';
if(!is_page_template('home-index.php')){
echo '<div class="mob_nav"><nav><span class="trigger"></span>'.$parallax_menu.'</nav></div>';
} else{
echo '<div class="mob_nav js"><nav><span class="trigger"></span>'.$parallax_menu.'</nav></div>';
}
echo '</div>';
} else if ( $smof_data['head_menu_style'] == "menu_dropdown" ) {
echo'<div class="container">'.$logo.'
<div class="nav'.$navClass.'">
<nav>
<div id="menu_current" class="'.$menu_cur.'">
<a href="'.$href.'" class="menu_1">
<span class="menu_name">'.$smof_data["home_menu_name"].'</span><span class="hover"><span class="arr"></span></span>
</a>
</div>
'.$parallax_menu.'
</nav>
</div>
<div class="mob_nav js"><nav><span class="trigger"></span>'.$parallax_menu.'</nav></div>
</div>';
if ( has_nav_menu('top_navigation', 'GoGetThemes') ) {
echo '<div id="menu_back">';
menu_back();
echo '</div>';
}
// Header Border
echo'<div class="head_box bot_box"></div>';
}
?>
I'm no php wizard, but I have tried to make it work without much succsses. Got it above the logo, got blank page, but that about it... Any help would be highly appreciated.
replace below line
echo'<div class="container">'.$logo.'
<div class="nav'.$navClass.'">
with
echo'<div class="container">'.$logo;
if ( has_nav_menu('top_navigation', 'GoGetThemes') ) {
echo '<div id="menu_back">';
menu_back();
echo '</div>';
}
echo '<div class="nav'.$navClass.'">

How can I combine these two if/else statements for different results?

I currently have these two blocks of php that are pulling results:
<?php
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
?>
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
</div>
<?php
endif;
?>
And
<?php
$url = article_custom_field('site-url');
elseif ( !empty($url) ) :
?>
<div class="site-url">Visit</div>
<?php
endif;
?>
I want to combine them to output one single block, like:
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<div class="site-url">Visit</div>
</div>
It needs to meet the following:
If web-tech exists, output it. Don't output site-url if it doesn't exist.
If web-tech exists, output it. If site-url exists, output it.
If site-url exists, output it. Don't output web-tech if it doesn't exist.
The containing div should not be output at all if neither variable does not exist.
Am I missing an obvious way to do it? It seems trivial, but I can't get the if/else/elseif statements to line up.
You could store your outputs in a variable, like so:
<?php
$output = '';
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
$output .= '<div class="tech-title">Technologies:</div>'
. '<ul class="tech-ul">' . $webtech . '</ul>';
endif;
$url = article_custom_field('site-url');
if(!empty($url)) :
$output .= '<div class="site-url">Visit</div>';
endif;
if($output != ''):
echo '<div class="tech-list">';
echo $output;
echo '</div>';
endif;
?>
This way, only when there is something set in your output variable, will it show anything.
Does this solve your problem?
It sounds like you need to check both variables before you output the container that they exist in.
<?php
$webtech = article_custom_field('web-tech');
$url = article_custom_field('site-url');
if ( !empty($webtech) || !empty($url))
{
?>
<div class="tech-list">
<?php
if ( !empty($webtech) )
{
?>
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<?php
}
if ( !empty($url) )
{
?>
<div class="site-url">Visit</div>
<?php
}
?>
</div>
<?php
}
?>
if (a or b) then {
OpenTechTitle;
if (a) SetAtext;
if (b) SetBtext;
CloseTechTitle;
}

Categories