I'd like to preface that I am not too fluent in php so i'm trying to understand and build php as i go.
I am attempting to add a custom Slidedeck slider to my website, http://cfsb.org/ . I currently have one in the header, but would like to change it on a per page basis.
I currently have:
function slidedecktwo_header_home() {
if (is_home()) {
echo do_shortcode('[SlideDeck2 id=xxx]');
}
}
add_action('genesis_header', 'slidedecktwo_header_home');
But when i try to add something like
else if (is_page(PAGE_ID = x)) {
echo do_shortcode('[SlideDeck2 id=xxx');
}
My webpage crashes and I have to delete the code before my website returns. Anyone see some obvious problems?
Thanks in advance.
Not sure if what you posted is the actual PHP you tried, but there are definite syntax errors.
Your code should look more like this (I added a random ID passed as an integer to the is_page() function):
function slidedecktwo_header_home() {
if ( is_home() ) {
echo do_shortcode('[SlideDeck2 id=xxx]');
} elseif ( is_page(42) ) {
echo do_shortcode('[SlideDeck2 id=xxx]');
}
}
add_action('genesis_header', 'slidedecktwo_header_home');
In your snippet, you had is_page( PAGE_ID = x ) and your shortcode snippet wasn't closed with a closing bracket ]
Related
I'm having one of those moments where I know I'm so close, and probably missing something super minor that's causing this to not work.
I'm using a pre-built theme, with an if statement for including or not including a link. What I want to do it put an if statement inside of THAT differentiating between internal and external links, opening external links in a new tab.
This is the portion I'm specifically working on
if(!empty($parallax_one_service_box->title)){
if( !empty($parallax_one_service_box->link) ){
if($parallax_one_service_box->link_type == 'External'){
echo '<h3 class="colored-text">'.esc_attr($parallax_one_service_box->title).'</h3>';
}else {
echo '<h3 class="colored-text">'.esc_attr($parallax_one_service_box->title).'</h3>';
}
}
else {
if (function_exists ( 'icl_translate' ) && !empty($parallax_one_service_box->id)){
echo '<h3 class="colored-text">'.icl_translate('Featured Area',$parallax_one_service_box->id.'_services_title',esc_attr($parallax_one_service_box->title)).'</h3>';
} else {
echo '<h3 class="colored-text">'.esc_attr($parallax_one_service_box->title).'</h3>';
}
}
}
So it's something along the lines of "if not empty, if external link open in a new tab." I ran it through php code checker, and the code is correct...not missing any brackets or anything. The problem seems to be that it's either opening all in a new tab or opening none in a new tab...it's not differentiating between external and internal. So I'm guessing something is wrong with the "if external" line...
Here's a link to Pastebin with the whole section of code: http://pastebin.com/kmuGiVJv
I want to append a disclaimer paragraph to my single posts based on a tag, specifically my "affiliate" tag. I have tried a few codes I found online that show some text but cause my excerpt to be blank. I don't have as much PHP knowledge as I'd like, if someone could point me in the right direction to writing a function for this.
Wordpress has a function for this: https://codex.wordpress.org/Function_Reference/has_tag
This should work:
<?php if ( has_tag('affiliate') ) { ?>
<p>Disclaimer paragraph</p>
<?php } ?>
I got it to work with this code, it's appearing after my social share buttons.. but oh well.
`function affiliate_add_to_content( $content ) {
if( is_single() ) {
if (has_tag('affiliate')) {
$content .= 'The disclaimer here';
}
}
return $content;
}
add_filter('the_content', 'affiliate_add_to_content');`
I had successfully created shortcode on my wordpress website to use it in the posts. But I later realised that it is breaking my rss-feed.
Here is the shortcodes.php
<?php
function disclaimer() {
$disc = " <p style='font-style:italic;'> <strong>Disclaimer</strong> :</p>";
return $disc;
}
add_shortcode('disclaimer', 'disclaimer');
?>
Here is how I am including it on the functions.php
<?php
include 'shortcodes.php';
So now, I am getting this error when I access my rss URL on my browser
So when I removed this include 'shortcodes.php'; my rss feed started working. But then I also want my shortcode to work. Am I doing anything wrong here? Any help would be highly appreciated.
You can change your function so that the disclaimer is not added on feed page
function disclaimer() {
if ( ! is_feed() ) {
$disc = " <p style='font-style:italic;'> <strong>Disclaimer</strong> :</p>";
return $disc;
}
}
Edit:
Then it is not a problem with the code, but some problem with the file.
Make sure there is not extra space after the closing ?> php tag. Or better, try to remove it completely.
I have looked at the other posts on this topic and can't find what I'm looking for.
I want to call a different shortcode depending on the page I am on.
The is_page function doesn't work in the loop so Im not sure how to accomplish this. Here is what I have so far. It makes logical sense to me but won't work for some reason.
if ( is_page(545) ) { <?php echo do_shortcode('[scrapeazon asin="Product1"]') ?>}
elseif ( is_page(525) ) { <?php echo do_shortcode('[scrapeazon asin="Product2"]') ?>}
else ...
Any help is much appreciated!
Thanks,
Mike
You're using the function correctly according to Wordpress Docs but your PHP is off. You have <?php open tag when php is already open, matching closing tags that aren't required either, and you're missing semicolons.
<?php
if ( is_page(545) ) { echo do_shortcode('[scrapeazon asin="Product1"]'); }
elseif ( is_page(525) ) { echo do_shortcode('[scrapeazon asin="Product2"]'); }
else ...
?>
You don't need the curly braces in there for single line executions but I left them for the sake of highlighting the error.
There is a cleaner way to do it though
<?php
$pages=array(
545=>'Product1',
525=>'Product2',
567=>'Product3',
568=>'Product4',
// etc
// etc
);
foreach ($pages as $page_id=>$asin){
if (is_page($page_id)) {
echo do_shortcode('[scrapeazon asin="'.$asin.'"]');
break; // because you used elseif you must want to stop checking ids after finding one is a page
}
}
?>
Though it's probably faster, and less work for your db, to select all the pages at once and loop through that array.
I have created a theme options panel for a client however they would like the ability to enter PHP code in the textareas and have it executed on the front end.
However, when they enter the code, it does not display properly in the front end, please see the following two screenshots:
http://i.imgur.com/alOAD.png
http://i.imgur.com/pYhW0.png
It looks like the code is being stripped when displayed on the front end. Its displayed using this code:
<?php global $options;
foreach ($options as $value) {
if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); }
}
?>
<?php echo $ag_footer_top; ?>
How can I get it to work properly? Is it possible?
If I enter the following into the theme options:
<?php wp_nav_menu( array( 'theme_location' => 'first','fallback_cb'=> ” ) ); ?>
It gets saved INTO the database as:
<?php wp_nav_menu( array( \'theme_location\' => \'first\',\'fallback_cb\'=> ” ) ); ?>
And its displayed on the front end as:
\'first\',\'fallback_cb\'=> ” ) ); ?>
First of all you must prevent WordPress from adding slashes to your content, using something like:
update_option('my_option',stripslashes($_POST['my_option']));
The other thing is that you want your code to be executed... well I do not know how to do this exactly, but a lot of Plugins provide this functionality, like
Linkable Title Html and Php Widget
You should download the Plugin and figure out how it works.
Just take the content of the field from the database now and try to parse it in some way.
Use stripslashes to unescape the string before evaluating/displaying the code.
You should replace ” with ''
you can use this functon of PHP
PHP: eval();
you can take the code in textarea save it to options as string and whenever you want to execute it, retrieve from options and pass it to eval.
Thanks
-Shak
I personally use Exec-PHP. It should be quite easy for your clients to use.