I want to show a template content if ad not available
i use this code but
both ad & template content are showing
<?php
if(dt_show_ads('position=Header&before=<div>&after=</div>'))
{
?>
<!-- Some content here -->
<?php
}
else
{
include TEMPLATEPATH.'/templates/newsleft_col.tpl.php';
}
?>
dt_show_ads('position=Header&before=<div>&after=</div>') seems to not return any value. This evaluates to false in PHP.
What dt_show_ads() does, though, is to insert ad html (if there is any).
Therefor, no matter if there are ads or not, your else part is always executed.
A quick google query didn't turn up any sensible documentation on dt_show_ads for me, you might wanna try this though:
if (strlen($ads = dt_show_ads('position=Header&before=<div>&after=</div>&echo=false')) !== 0)
{
echo $ads;
// whatever other content you want to show
}
else
{
include TEMPLATEPATH.'/templates/newsleft_col.tpl.php';
}
Edit:
Since, per your comment, dt_show_ads() doesn't support the WP-semi-standard echo argument, you'll need to buffer its output to be able to check it:
ob_start();
dt_show_ads('position=Header&before=<div>&after=</div>');
$ads = ob_get_contents();
ob_end_clean();
if (strlen($ads) !== 0)
{
echo $ads;
// whatever other content you want to show
}
else
{
include TEMPLATEPATH.'/templates/newsleft_col.tpl.php';
}
There is no posibility to get both states of the IF statement. There is something wrong with your code. You may have not show up here all of your code correctly ?
Answer #2
The function that you check:
if(dt_show_ads('position=Header&before=<div>&after=</div>'))
{
}
else
{
}
It can print out the appropriate HTML and at the end return a false in example. In that case you get false for the first IF statement and the because of the false you get the else part.
To be sure what is the result of the dt_show_ads(); do that:
echo "<pre>" . print_r(dt_show_ads('position=Header&before=<div>&after=</div>'), true) . "</pre>";
<?php
if (!dt_show_ads('position=Header&before=<div>&after=</div>')) {
include TEMPLATEPATH.'/templates/newsleft_col.tpl.php';}
?>
Related
Hi I am having trouble doing an if state in wordpress. If multisite network url equals then do something
Any help? Thanks
<?php
var $china = "network_home_url()/china";
if($china === true){?>
<img src="..">
<?php } else { ?>
<img src="..">
<?php } ?>
escape quotes, network_home_url() is php function:
var $china = network_home_url()."/china";
And for 'if' statement use not true but the url itself:
if ($china == "http://somekindofurl.com/china") {
//do code
}
This is not the ideal solution to compare with whole string.
Instead, you can use strpos() in PHP to check whether it contains 'china'
something like
$url = 'network_home_url()/china';
if (strpos($url, 'china') !== false) {
echo '<img src="..">';
/*this is true*/
} else {
echo '<img src="..">';
}
Full doc strpos()
Thanks
I have used backstretch to switch bg image for each page on my wordpress site, all is fine except for my 'news page' (i.e. blog functionality). Even though I've identified and used the correct page ID, the script doesn't want to update it.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://www.myurl.co.uk/wp-content/themes/name/bootstrap/js/jquery.backstretch.min.js"></script>
<?php
if(is_page(2))
{
echo '<script>$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_homepage.jpg");</script>';
}
else if(is_page(51))
{
echo '<script>$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_food_menu.jpg");</script>';
}
else if(is_page(60))
{
echo '<script>$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_whats_on.jpg");</script>';
}
else if(is_page(53))
{
echo '<script>$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_drink_menu.jpg");</script>';
}
else if(is_page(57))
{
echo '<script>$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_where_when.jpg");</script>';
}
else if(is_page(55))
{
echo '<script>$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_whats_on.jpg");</script>';
}
?>
From this code, page 60 is displayed without a background image. Do I need to write something specific for news/blog pages?
Any suggestions will be gratefully received.
You'll probably need to nest the whole thing inside a document.ready call, otherwise you're trying to initialise it before anything is ready.
Try this:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://www.myurl.co.uk/wp-content/themes/name/bootstrap/js/jquery.backstretch.min.js"></script>
<script>
$(document).ready(function(){
<?php
if(is_page(2))
{
echo '$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_homepage.jpg");';
}
else if(is_page(51))
{
echo '$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_food_menu.jpg");';
}
else if(is_page(60))
{
echo '$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_whats_on.jpg");';
}
else if(is_page(53))
{
echo '$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_drink_menu.jpg");';
}
else if(is_page(57))
{
echo '$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_where_when.jpg");';
}
else if(is_page(55))
{
echo '$.backstretch("http://www.myurl.co.uk/wp-content/themes/name/img/bg_whats_on.jpg");';
}
?>
});
Also check for any JS errors in your console that might be preventing it from running properly.
I need get the value of function that use in wordpress , this function add the value in other page of wordpress using the API add_action , i put the script :
<?php
function wplogincontrol()
{
$a=2;
}
if ($a=="2")
{
header"Location:http://www.google.com");
}
add_action('login_head', 'wplogincontrol');
?>
If use this never can do works and i think use global var and use this :
<?php
function wplogincontrol()
{
global $a;
$a=2;
}
global $a;
if ($a=="2")
{
header"Location:http://www.google.com");
}
add_action('login_head', 'wplogincontrol');
?>
Also i try with $GLOBALS['a'];
But never can get works and can´t get the value outside the function , i try this because if use header location inside function and no work me and give error of header , by this i need get the value outside of function and i see works fine for redirect of header
It´s possible get this value ? , Thak´s , Regards
Are you trying to do something like this ?
<?php
function myfunc()
{
$a=2;
return $a;
}
function wplogincontrol(){
$val = myfunc();
if ($val=="2")
{
header"Location: http://www.google.com");
exit;
}
}
add_action('login_head', 'wplogincontrol');
?>
The length of the excerpt in wordpress is 55 words by default.
I can modify this value with the following code:
function new_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'new_excerpt_length');
So, the following call will return just 20 words:
the_excerpt();
But I can't figure out how could I add a parameter to obtain different lengths, so that I could call, for example:
the_excerpt(20);
the_excerpt(34);
Any ideas? Thanks!
uhm, answering me again, the solution was actually quite trivial. it's not possible, as far as i know, to pass a parameter to the function my_excerpt_length() (unless you want to modify the core code of wordpress), but it is possible to use a global variable. so, you can add something like this to your functions.php file:
function my_excerpt_length() {
global $myExcerptLength;
if ($myExcerptLength) {
return $myExcerptLength;
} else {
return 80; //default value
}
}
add_filter('excerpt_length', 'my_excerpt_length');
And then, before calling the excerpt within the loop, you specify a value for $myExcerptLength (don't forget to set it back to 0 if you want to have the default value for the rest of your posts):
<?php
$myExcerptLength=35;
echo get_the_excerpt();
$myExcerptLength=0;
?>
There is no way to do this as far as I have found using the_excerpt().
There is a similar StackOverflow question here.
The only thing I have found to do is write a new function to take the_excerpt()'s place. Put some variation of the code below into functions.php and call limit_content($yourLength) instead of the_excerpt().
function limit_content($content_length = 250, $allowtags = true, $allowedtags = '') {
global $post;
$content = $post->post_content;
$content = apply_filters('the_content', $content);
if (!$allowtags){
$allowedtags .= '<style>';
$content = strip_tags($content, $allowedtags);
}
$wordarray = explode(' ', $content, $content_length + 1);
if(count($wordarray) > $content_length) {
array_pop($wordarray);
array_push($wordarray, '...');
$content = implode(' ', $wordarray);
$content .= "</p>";
}
echo $content;
}
(Function credit: fusedthought.com)
There are also "advanced excerpt" plugins that provide functionality like this you can check into.
Thanks for your answer, thaddeusmt.
I am finally implementing the following solution, which offers a different length depending on the category and a counter ($myCounter is a counter within the loop)
/* Custom length for the_excerpt */
function my_excerpt_length($length) {
global $myCounter;
if (is_home()) {
return 80;
} else if(is_archive()) {
if ($myCounter==1) {
return 60;
} else {
return 25;
}
} else {
return 80;
}
}
add_filter('excerpt_length', 'my_excerpt_length');
I am having trouble with the previous_post_link() and next_post_link() functionality. When there is no previous post, the function previous_post_link() does not display a link, likewise for the next_post_link() and the last post. I would like to have a placeholder image so that the design stays consistent.
Currently I have images of green arrows pointing left and right, I would like to place an image of a grey arrow if there are no more posts to go back to.
Is there a way to use the next_post_link()/previous_post_link() functions but not have the link removed.
I also wonder if there is a way for the links to cycle, so that if you come to the most recent post, the next post link would bring you back to the first post.
************ UPDATED ************
Here is the code, based on "silent's" advice (accepted answer) to use get_adjacent_post():
<?php
if(get_adjacent_post(false, '', true)) {
previous_post_link('%link','<img src="larr.gif"/>');
}
else {
echo '<img src="larr2.gif"/>';
};
if(get_adjacent_post(false, '', false)) {
next_post_link('%link','<img src="rarr.gif"/>');
}
else {
echo '<img src="rarr2.gif">';
};
?>
So you can "capture" what next_post_link() and previous_post_link() return using ob_start() and ob_get_clean(), then apply a conditional to it.
Code in practice:
$previous_string = "<-Back";
ob_start(); // start output buffering
previous_post_link("%link", $previous_string);
$previous_link = ob_get_clean(); // stop output buffering and store
if ($previous_link == '') {
echo '<span style="color: #ccc">' . $previous_string . '</span>';
} else {
echo $previous_link;
}
I never try this myself. However, you may refer to this post. It uses get_adjacent_post().
Why cant you try the below?
<div style="float:right"><?php if(get_previous_posts_link()) { previous_posts_link('Newer Entries »'); } else { echo 'No Newer Entries'; } ; ?></div>
Alternatively to display Next and Previous Post Links to Blog post you can easily do that. This is very well explained at globinch.com (WordPress Tips : How to Add Next and Previous Post Links to Blog? )
http://www.globinch.com/2010/10/12/wordpress-tips-how-to-add-next-and-previous-post-links-to-blog/