(WordPress) How to use if OR if post? - php

I want to show some HTML code for specific posts, so I put this on HEADER templete:
<?php if(is_single(24) || is_single(34)) { ?>
MY HTML CODES
<?php } ?>
I tried those too:
<?php if(is_single(24) && is_single(34)) { ?>
<?php if((is_single(24)) && (is_single(34))) { ?>
<?php if((is_single(24)) || (is_single(34))) { ?>
And its not working. If I put this code for single post, like this:
<?php if(is_single(24) { ?>
Its working well.. but I need to do that for many posts.

You could do if (is_single() && in_array($post->ID, array(24, 34)) {}
Depending on context, you may need to make $post global.
I'd probably add some metadata to the posts though and check for that instead... Less messy.

Try using in_array
<?php if(in_array(POST_ID, array(POST_IDS))) { ?>
MY HTML CODES
<?php } ?>

Related

How Can I use a function multiple times on the same page?

Say this piece of code:
<?php while($user=mysqli_fetch_array($resultuser)){ ?>
<?php
function my_function($variable) {
//do something here...
}
?>
<?php };?>
This will obviously return this error
Cannot redeclare my_function() previously declared
So what if someone needs to use the same function multiple times across the same page? Is there a way to generate random function() names? Any idea how to get around this? Thanks.
EDIT WITH ACTUAL CODE
<?php while($deposit26=mysqli_fetch_array($resultdeposit26)){ ?>
<td data-th="Hours">
<?php
$week1hours = $deposit26['hours_worked'];
$week2hours = $deposit26['hours_worked_wk2'];
function time_to_decimal($time) {
$timeArr = explode(':', $time);
$decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
return $decTime;
}
$groupd26hours = time_to_decimal($week1hours) + time_to_decimal($week2hours);
echo round($groupd26hours, 2);
?>
</td>
<?php };?>
<?php
// Earlier in the file or included with include or require
function time_to_decimal($time) {
$timeArr = explode(':', $time);
$decTime = ($timeArr[0] + ($timeArr[1]/60) + ($timeArr[2]/3600));
return $decTime;
} ?>
...
<?php while($deposit26=mysqli_fetch_array($resultdeposit26)) : ?>
<td data-th="Hours">
<?php
$week1hours = $deposit26['hours_worked'];
$week2hours = $deposit26['hours_worked_wk2'];
$groupd26hours = time_to_decimal($week1hours) + time_to_decimal($week2hours);
echo round($groupd26hours, 2); ?>
</td>
<?php endwhile ?>
you want to declare the function outside the loop and call it inside
<?php
function my_function($variable) {
//do something here...
}
while($user=mysqli_fetch_array($resultuser)){
my_function($variable);
}?>
Let me try and explain what I think could help. I think a good starting point would be to look into including a script or including a script once in your files that require your function logic. That way multiple files can take advantage of the same logic without it having to be repeated. For example:
<?php
// File functions.php
function my_function($variable) {
...
}
?>
<?php
// File one
include_once "functions.php"
...
// Use my_function() from file one
my_function($var);
?>
<?php
// File two
include_once "functions.php"
...
// Use my_function() from file two
my_function($var);
?>

exclude sidebar from specific pages

I want to exclude the sidebar from one more specific page
I have this:
<?php if (is_page('Forum')) { } else { ?>
<?php get_sidebars(); ?>
<?php } ?>
But I want to add another page
I tried this:
<?php if (is_page('Forum'), ('Shop') { } else { ?>
<?php if (is_page('Forum','Shop') { } else { ?>
and
But none of those seem to work.
What is the syntax for this?
<?php if (is_page('Forum') || is_page('Shop')) { } else {
You need to use the OR operator to combine two boolean expressions. That is if the page is Forum OR if the page is Shop then...

Make php do_action conditional upon class field

Sorry a noob question. I wish to make this:
<?php do_action('woocommerce_product_thumbnails'); ?>
Conditional to a custom field called 360 with the value yes.
<?php // do_action('woocommerce_product_thumbnails'); // ?>
For the simple reason that my plugin uses 36 images to create a rotating display.
When I dont use the plugin, I want to use the thumbnails. Many thanks Asa.
I have tried
<?php
if (get_post_meta($post->ID, '360', false)) {
do_action('woocommerce_product_thumbnails');
} else {
do_action('woocommerce_product_thumbnails');
}
?>
But I still get to see the thumbnails when 360 is returned as false?
I have tried
<?php
if (get_post_meta($post->ID, '360', true)) {
// do_action('woocommerce_product_thumbnails');
} else {
do_action('woocommerce_product_thumbnails');
}
?>
Works! Many thanks.
I think this is what you mean - if there is a value for the custom meta value of 360 (ie you'd created a custom metabox with a checkbox with id 360 in it) for that page/post then the product thumbnails are used. If not then you put in your rotating display.
<?php
if (get_post_meta($post->ID, '360', true)) {
do_action('woocommerce_product_thumbnails');
} else {
// your rotating display code.
}
?>

Displaying a drupal page without the template around it in Drupal 7

I'd like to render the contents of a "basic page" in drupal. Something like this question: displaying a Drupal view without a page template around it but for Drupal 7.
My attempt almost works:
function mytheme_preprocess_page(&$variables, $hook) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$variables['theme_hook_suggestions'][] = 'page__ajax';
}
}
And have a file named page--ajax.tpl.php in the same directory where template.php lives:
<?php print $page['content']; ?>
The problem is that it still renders the menu and my two custom blocks from the sidebar. I only want the page content. What should I change?
You are almost there. The only thing you need is to add a custom HTML wrapper template.
Add a function to template.php:
function THEMENAME_preprocess_html(&$variables, $hook) {
if ( isset($_GET['ajax']) && $_GET['ajax'] == 1 ) {
$variables['theme_hook_suggestions'][] = 'html__ajax';
}
}
Create a file named html--ajax.tpl.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN"
"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">`
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php print $styles; ?>
<?php print $scripts; ?>
</head>
<body class="<?php print $classes; ?>">
<?php print $page_top; ?>
<?php print $page; ?>
<?php print $page_bottom; ?>
</body>
</html>
Flush all caches. That's all.
Based on the answer of Ufonion Labs I was able to completely remove
all the HTML output around the page content in Drupal 7 by
implementing both hook_preprocess_page and hook_preprocess_html in
my themes template.php, like this:
function MY_THEME_preprocess_page(&$variables) {
if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
$variables['theme_hook_suggestions'][] = 'page__embed';
}
}
function MY_THEME_preprocess_html(&$variables) {
if (isset($_GET['response_type']) && $_GET['response_type'] == 'embed') {
$variables['theme_hook_suggestions'][] = 'html__embed';
}
}
Then I added two templates to my theme: html--embed.tpl.php:
<?php print $page; ?>
and page--embed.tpl.php:
<?php print render($page['content']); ?>
Now when I open a node page, such as http://example.com/node/3, I see
the complete page as usual, but when I add the response_type
parameter, such as http://example.com/node/3?response_type=embed, I
only get the <div> with the page contents so it can be embedded in another page.
Shamelessly taken form here :
displaying a Drupal view without a page template around it (second best answer for drupal 7).
Alexei solution still use the page template wich is in charge of displaying blocks

PHP passing variables into a function

This is really basic PHP. Can someone tell me why this does not work and what I need to do to make it work.
<?php
$test_var=12;
proc_scrn($test_var);
proc_scrn($local_pid)
{
echo "tp12",$local_pid ;
}
?>
Well, you haven't actually created a function there. This would work:
<?php
$test_var=12;
proc_scrn($test_var);
function proc_scrn($local_pid='')
{
echo "tp12: ".$local_pid;
}
?>
function proc_scrn($local_pid)
{
// something
}
PHP- User-defined functions
Pretty simple
<?php
$var=1;
function proc_scrn($var1){
echo "tp12: ".$var1;
}
proc_scrn($var);
?>

Categories