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
Related
I am trying to remove the URL from the tag name. Please let me know. how to achieve this.
Screenshot Link - https://i.ibb.co/grSPH7t/Screenshot-1.png
Thanks
Try By Code :
<?php
$postalltags = get_the_tags();
if ($postalltags) {
foreach($postalltags as $singletag) {
echo $singletag->name . ' ';
}
}
?>
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'm currently building links like this:
<?php echo get_the_title(111); ?>
I was building links like this using the WPML plugin (but steering away from it due to various reasons):
<?php icl_link_to_element(111); ?>
This builds a link similar to my first example.
So my question is is there a native Wordpress function that does this? I'm sure there must be, but cannot find the solution anywhere. I'm looking to reduce my markup...
Thanks!
EDITED WITH ANSWER
This is how I built my custom function:
function build_pretty_link($id,$link_title='') {
if($link_title=='') {
$link_title = get_the_title($id);
}
$link_url = get_permalink($id);
echo "{$link_title}";
}
WordPress give a function that print an anchor tag with the title and the url, but you have to be in a the loop (http://codex.wordpress.org/Function_Reference/permalink_anchor).
I suggest you to create your own function (the functions.php file in your theme is here for that).
You can do someting like that :
function vp_link_to($post_id) {
echo '<?php echo get_the_title($post_id); ?>';
}
get_permalink(x);
Where the ID of the page is x and wrap this in whatever you need, so
$id = 10;
$link = get_permalink($id);
echo 'Linked text';
I have the following code on my page:
<p class="postmetadata">Category: <?php foreach((get_the_category()) as $cat) {
if (!($cat->cat_ID=='12')) echo ''. $cat->cat_name . '' . ', ';
} ?></p>
Obviously as long as the cat is not 12, it will show the category name as a link.
If I wanted to include multiple categories i.e.
if (!($cat->cat_ID=='12 or 13 or 14'))
how would I do this?
Thanks
dvent
Try
if (!is_category(array('12','13','14')))
"||" means "or", so something like this should work:
if ( !($cat->cat_ID=='12') || !($cat->cat_ID=='13') || !($cat->cat_ID=='14') )
// Place the list of categories to test inside an array
$categories_list=array(12, 24, 32);
// Then test to see if your category is in this list
if (in_array($cat, $categories_list)) {
// do something
}
else
{
// do something else
}
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';}
?>