Why Wont This Display? - php

Can't figure out what I'm doing incorrectly here
this is at the top of the template file
<?php
/**
* #package 1
* #since 1 1.0
*/
$source_name = get_post_meta($post->ID, 'Source Name', true);
$source_url = get_post_meta($post->ID, 'Source URL', true);
?>
here is the other part thats further down:
<?php if($source_url) { ?>
<div id="content-source">
<span>Source:</span> <?php echo $source_name; ?>
</div>
<?php } ?>
If I remove <?php if($source_url) { ?> and <?php } ?> it works fine, but how do I get it to work so if theres no source nothing will be displayed?

A quick look-up of the get_post_meta() function:
If there is nothing to return the function will return an empty array unless $single has been set to true, in which case an empty string is returned.
So, try:
<?php if($source_url <> "") { ?>
<div id="content-source">
<span>Source:</span> <?php echo $source_name; ?>
</div>
<?php } ?>
Previous you were checking if nothing is returned. You need to be checking for an empty string instead.

Related

Isssue with display data from database

I use framework:
CodeIgniter
I try display values from enrol table. Connetcion with DB working correct etc. Because standard app function in this way working correct. I try add my custom own code but something went wrong and result is empty.
I try combinate in this way:
<?php
$enrol_history = $this->db->get_where('enrol', array('active_shop' => $active_shop, 'monthly_subscription_fee' => $monthly_subscription_fee))->row_array();
?>
<?php echo $active_shop['active_shop']; ?>
<?php echo $enrol_history['monthly_subscription_fee']; ?>
<?php echo $active_shop ?>
<?php echo $monthly_subscription_fee ?>
Everytime empty result.
update:
I also try:
Crud_model.php
public function get_installation_details($monthly_subscription_fee = "")
{
return $this->db->get_where('enrol', array('monthly_subscription_fee' => $monthly_subscription_fee));
}
code:
<?php $installation_details = $this->crud_model->get_installation_details($monthly_subscription_fee)->row_array(); ?>
<?php echo $installation_details['monthly_subscription_fee']; ?>
update:
<?php
$enrol_history = $this->db->get_where('enrol', array('active_shop' => $enrol['active_shop'], 'monthly_subscription_fee' => $enrol['monthly_subscription_fee'],))->row_array();
?>
<?php echo $enrol['monthly_subscription_fee']; ?>
<?php echo $enrol['active_shop']; ?>
This is correct. Because in admin panel I can get in this way this values from DB. But in frontend I cannot do it...

Dynamic variable in PHP [GET]

I'm trying to use the php get. everthing is working find exept the include page.
it's adding 1 at the end of include content
any ide how do i ride that?
example:
I love norway
I love norway1
Code:
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') $norge = include('norway.php');
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>
We should use file_get_contents, instead of include.
$norge = file_get_contents("norway.php");
Use the output buffer functions to capture the output from the included script.
<?php
if($_GET['boy'] == 'zabi') $zabkas = 'it is Zabi';
if($_GET['girl'] == 'veronka') $verona = 'It is veronka';
if($_GET['place'] == 'norway') {
ob_start();
include("norway.php");
$norge = ob_get_clean();
}
?>
<?php echo $zabkas ?>
<?php echo $verona ?>
<?php echo $norge ?>

Pass arguments from php function issue

I am working on a wordpress plugin. In plugin, a user first add a slider and then add images of relevant slider.
First i make this with shortcode. User enter the short name of the slider in the shortcode and images slides of relevant slider like this [foo_slider slider=slider_one] or [foo_slider slider=slider_two].
Now i "also" want the snippet, that user can add snippet else shortcode in the code like this echo wp_foo_slider(slider_two). But i dont get that.
Please guide me, how can i do this.
Here is my Code That works for shortcode:
<?php
function wp_foo_sliders($atts) {
global $wpdb;
$tbl_sliders = $wpdb->prefix . "foo_sliders";
ob_start();
extract(shortcode_atts(array(
'slider' => '',
), $atts));
$get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
<div class="foo_main_slider">
<?php
foreach ($get_sliders as $get_slider) {
$slider_id = $get_slider->slider_id;
?>
<div class="foo_slider_img">
<?php
$get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where
slider_id = $slider_id Order By image_order ASC");
foreach ($get_slider_image as $foo_img) {
?>
<img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
<?php
}
}
return ob_get_clean();
}
add_shortcode("foo_slider", "wp_foo_sliders");
?>
I also try this by my own <?php echo wp_foo_sliders("slider_two") ?> or <?php echo wp_foo_sliders(slider_two) ?>, in code and when i refresh the browser only <div class="foo_main_slider"> </div> appears and no images show.
Edit: I want that user can use short code <?php echo do_shortcode('[foo_slider slider=slider_one]'); ?> or user can use snippet <?php echo wp_foo_sliders("slider_two") ?>, only shortcode is working, snippet not work.
What i make mistake please help me.
When you call the shortcode function directly, you are passing a string to it. When you use the shortcode way WordPress will convert the arguments into an associative array.
Try refactoring your code
if( is_array( $atts ) ) {
//Called using shortcode so $atts is an array
extract(shortcode_atts(array(
'slider' => '',
), $atts));
} else {
//Function called directly so $atts is a string
$slider = $atts;
}
So i get my solution by my own.
I make a new function for this:
<?php
function foo_sliders($foo_short_name) {
global $wpdb;
$tbl_sliders = $wpdb->prefix . "foo_sliders";
$get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
<div class="foo_main_slider">
<?php
foreach ($get_sliders as $get_slider) {
$slider_id = $get_slider->slider_id;
?>
<div class="foo_slider_img">
<?php
$get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where
slider_id = $slider_id Order By image_order ASC");
foreach ($get_slider_image as $foo_img) {
?>
<img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
<?php
}
}
ob_start();
return $foo_short_name;
return ob_get_clean();
}
?>
And in theme code:
<?php foo_sliders(short_name) ?>
and its work great

2 pieces of similar PHP, one works one doesn't?

I've got a few fields on a property site, grabbing a value in English & depending on the value, translating it (if another language other than English is selected).
This piece of code works fine:
<?php if(get_post_meta($post->ID,'prop_parking',true) && $prop_parking):
$prop_parking_meta = get_post_meta($post->ID,'prop_parking',true);
if ($prop_parking_meta == 'Yes') {
$prop_parking_meta = '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->';
}
elseif ($prop_parking_meta == 'No') {
$prop_parking_meta = '<!--:en-->No<!--:--><!--:es-->No<!--:--><!--:ru-->нет<!--:-->';
} ?>
<li>
<p><?php echo PROP_PARK_CSTM;?>:</p><p> <?php _e( $prop_parking_meta ); ?></p>
</li>
<?php endif; ?>
I get back Yesin the set language, yet in this field I don't (I just see Yes or No):
<?php if(get_post_meta($post->ID,'prop_garage',true) && $prop_garage):
$prop_garage_meta = get_post_meta($post->ID,'prop_garage',true);
if ($prop_garage_meta == 'Yes') {
$prop_garage_meta = '<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->';
}
elseif ($prop_garage_meta == 'No') {
$prop_garage_meta = '<!--:en-->No<!--:--><!--:es-->No<!--:--><!--:ru-->нет<!--:-->';
} ?>
<li>
<p><?php echo PROP_GARG_CSTM;?>:</p><p> <?php _e( $prop_garage_meta ); ?></p>
</li>
<?php endif; ?>
Is it something obvious I'm missing? :( Thanks!
I don't know why this issue happens sometimes in qTranslate, but there are two options to deal with it:
using the shortcode notation
$prop_garage_meta = '[:en]Yes[:es]Sí[:ru]да';
applying the_content filter
$prop_garage_meta = apply_filters(
'the_content',
'<!--:en-->Yes<!--:--><!--:es-->Sí<!--:--><!--:ru-->да<!--:-->'
);

WordPress post views from the_views as a variable

I am using the WordPress plugin WP-PostViews to display the number of views a post has. I am using this code and it works great:
<?php if(function_exists('the_views')) { the_views(); }?>
The thing I want to do is to use this number of post views as a variable but I can't get it working. So far I have tried:
<?php if(function_exists('the_views')) { $variable = the_views(); } ?>
and
<?php if(function_exists('the_views')) { $variable = the_views(); } else { $var = 0; } ?>
No success so far. Does anyone have suggestions?
By default the_views() echos instead of returns. You can get a return by setting the first argument to false. Example:
<?php if(function_exists('the_views')) { $variable = the_views(false); } ?>

Categories