Content visibility based on multiple url values with strstr? - php

Using a Wordpress plugin to sort custom post types based on custom taxonomies. Results are returned on the post type's archive page. Sorting is working great but hit a snag trying to display header content for items with multiple taxonomies:
<?php if(strstr($_SERVER['REQUEST_URI'], "level/invigorating")) { ?>
<h3>These poses are invigorating:</h3>
<?php } ?>
<?php if(strstr($_SERVER['REQUEST_URI'], "level/invigorating/position/seated")) { ?>
<h3>These poses are invigorating and seated:</h3>
<p>For positions of this nature please take care of your sacrum.</p>
<?php } ?>
<?php if(strstr($_SERVER['REQUEST_URI'], "position/seated")) { ?>
<h3>These poses are seated:</h3>
<?php } ?>
"level/invigorating/position/seated" returns three h3's because all if's return true.
My PHP skills are awful and trying to understand the manual is like staring into the night sky.
How can this be written so only one value can be returned per page?
Perhaps this screenshot helps.
Using Beautiful Taxonomy Filters.

I believe what you are trying to do is this...
<?php if(strstr($_SERVER['REQUEST_URI'], "level/invigorating/position/seated")) { ?>
<h3>These poses are invigorating and seated:</h3>
<p>For positions of this nature please take care of your sacrum.</p>
<?php } elseif(strstr($_SERVER['REQUEST_URI'], "level/invigorating")) { ?>
<h3>These poses are invigorating:</h3>
<?php } elseif(strstr($_SERVER['REQUEST_URI'], "position/seated")) { ?>
<h3>These poses are seated:</h3>
<?php } ?>

Related

How to only show category description and title if there's a description available?

Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>

Pagination Wordpress in Custom Theme

Hi I hope someone can help with this.
I'm building a web site using wordpress, but I created my custom themes.
I just have basic knowledge of php and i managed to build the home page which show the latest 4 post.
This is the code that i use to show the last and the second-last post. Just using an offset and skipping one post every time to show the previous.
LATEST POST SCRIPT
<?php $posts = get_posts('cat=1,4,5&numberposts=1&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "1") { break; } else { ?>
<?php echo catch_that_image() ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
SECOND LAST POST SCRIPT
</span></td>
<td colspan="8" rowspan="2" valign="top" class="blog_text">
<?php
query_posts('showposts=1'); ?>
<?php $posts = get_posts('cat=1,4,5&numberposts=1&offset=1'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count2 = 0; if ($count2 == "1") { break; } else { ?>
<?php the_content(); ?>
<?php $count2++; } ?>
<?php endforeach; ?>
I hope this part help to understand, because i'm working in local so i can't show the website for now.
Now my problem is how i can dynamic implement the pagination? so if the first page show the post 1,2,3,4
i need to create a dynamic pagination which would allow me on clicking "previous posts" or the number of the page
will show the post number 4,5,6,7.
I've already had a look on the tutorial for dynamic pagination but i can't understand how i can do it, as i'm working on wordpress
and my script are different! As i said i have basic knowledge of PHP so if someone could explain me how to do with my code would be perfect!
Probably i don't need the foreach because this was a code found to show from post 1 to post 4 for example, and i changed to show just one!
I basically can't use a normal loop because the post that i have to show are in diferrent postion so i need separate code!
I hope i could explained myself! Thank you to everyone for any help.

Wordpress PHP within PHP

I am trying to achieve an outcome that combines two plugins in WordPress.
Basically, I am using Easing Slider Pro and Advanced Custom Fields. When the website owner edits a page, I want them to be able to add a slideshow by simply entering the slideshow ID into an Advanced Custom Field called 'slider'.
This is how one would normally add the PHP to display a slideshow:
<?php if ( function_exists('easingsliderpro') ) { easingsliderpro( 5 ); } ?>
The 5 is an example of a slideshow ID that can be changed.
Here is the PHP for the advanced custom field:
<?php if( get_field('slider') ): ?><?php the_field('slider'); ?><?php endif; ?>
Both of these work fine by themselves. But I want a way to combine these two pieces of code so that in the page editor the website manager only has to enter the ID of the slideshow. I don't know a lot about PHP and I am often confused by it, but this was my initial attempt:
<?php if( get_field('slider') ): ?>
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) { easingsliderpro( <?php the_field('slider'); ?> ); } ?></div>
<?php endif; ?>
It didn't work, I am assuming because you're not allowed to have PHP code within PHP code. Is there any workaround that anyone knows that could make this achievable?
Many thanks.
Am I crazy? Can't you just:
AHA!
I think I see the confusion: the_field echoes the value out, so it gets passed to easingsliderpro() as just true, and displays the value.
You need to use a function that returns the value, so you can pass it to the next function.
In this case, it's get_field():
<?php if( get_field('slider') ): ?>
<div id="sliderframe">
<?php
if ( function_exists('easingsliderpro') ) :
easingsliderpro( get_field('slider') );
endif;
?>
</div>
<?php endif; ?>
See more in the documentation:
http://www.advancedcustomfields.com/resources/functions/get_field/
You shouldn't put php open close tags within a php open/close tag.
For your code above, this is valid:
<div id="sliderframe"><?php if ( function_exists('easingsliderpro') ) {
easingsliderpro(the_field('slider'));
} ?></div>

confused to make sitemap using php coding

I have created a CMS website, now I am trying to make site map with php coding, but the code which i have made is not complete. i.e It is not showing all the sub menu data.
Please look at this image . According to this image, I am having many other sub menu under Weight training(sub menu of Fitness Exercises) ,but they are not visible. Why they are not visibile?
Please guide/help me to solve this issue
My php code
foreach ($query_sitemap as $artrow) {
$datefromsql = $artrow->created_date;
$time = strtotime($datefromsql); ?>
<p>
<ul>
<?php echo '<b>'.$artrow->menu_name.'</b>'; ?>
<?php $submenucon=$this->menumodel->fetch_menu_byPid($artrow->id);//i.e select*from menu where parent_id=$mid
if (empty($submenucon)) { ?>
---
<?php
} else {
foreach ($submenucon as $subtrow) {
?>
<?php echo '<li style="color:gray;margin:">'.$subtrow->menu_name.'</li><br/>'; ?>
<?php
}
}
?>
</ul>
</p>
<?php
$i++;
}
} ?>
You're dealing with a tree structure here represented (in a most siplistic/naive way) in a database. So the first thing you should do is to build a tree-like structure based on returned rows and only then traverse it recrusively in order to display.
You can read more on parsing tree-like tables here:
Is it possible to query a tree structure table in MySQL in a single query, to any depth?
Implementing a hierarchical data structure in a database
What is the most efficient/elegant way to parse a flat table into a tree?

Hide and show divs using PHP

So I have this code that requires to be a member. Now the question is what code do I add to hide two different divs called "socialsignup" and "formwrap". Another question is can you have multiple <?php code here ?>s in different places on the same page? Much appreciated with any help.
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
You can wrap multiple time of PHP tags in a page.
That's answer for your fisrt and second question.
You can check like this
<?php if($val=='socialsignup') {?>
html for the social sign up
<?php } else if ($val=='formwrap') {?>
html for formwrap
<?php }else{?>
remaining conditions
<?php } ?>

Categories