I am trying to change a CSS class, based on the contents of a variable called $project_cats which contains a category name. While $project_cats appears to be valid throughout my script, it is not valid within one of my if statements, at least that is my assumption.
How can I access $project_cats from within my if statement?
<?php
if ( $query->have_posts() )
{
?>
<div id="myresource">
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<?php
$terms = get_the_terms($post->id,"project-type");
$project_cats = NULL;
if ( !empty($terms) ){
foreach ( $terms as $term ) {
$project_cats .= strtolower($term->name) . ' ';
}
}
//$project_cats does not appear to have content in the below if statement
if ($project_cats == "webinars") {
$iconclass = "iconsmind-Video-4";
}
?>
<div class="nectar-icon-list" data-icon-color="default" data-icon-style="border" data-icon-size="small" data-animate="">
<div class="nectar-icon-list-item">
<div class="list-icon-holder" data-icon_type="icon" style="background-color:#1963af;">
<i class="icon-default-style <?php echo $iconclass; ?>" data-color="default"></i>
</div>
<div class="content">
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php echo $project_cats;?>
</div>
</div>
</div>
<?php
}
?>
</div>
<?php
}
else
{
echo "No Results Found.";
}
?>
I just realized what's happening, in the if statement the name of the category is having a space ' ' appended to it, hence my if statement was never TRUE. Fixed.
Related
There are 2 dynamic changing requiers thing in my html.they are "active" and "numbers (for example 1,2,3...)".I want write "active" into class if it is first element in query, else write only one space.Also I want write "1" into class of first element and for other elements,write like this "2,3,...".
So, for this scenerio, I wrote below code.But my plugin doesnt work like what I want.You can see how the numbers are created for this script on that link:
http://www.hekim.deniz-tasarim.site/elementor-685/
may it loads slowly, so I write echos for numbers here:
0 1 0 2 0 3 0 4 for first query
1 0 1 0 1 0 1 0 for second query
So, how can I solve this?
<?php
$activeornot = 0;
$teamnumber = 1;
$query = new \WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
if ($activeornot = 0):
$echo_active_or_not = "active";
else:
$echo_active_or_not = " ";
endif;
?>
<li role="presentation" class="<?php echo $echo_active_or_not; ?>"><img src="<?php the_post_thumbnail_url(); ?>" alt="s1.jpg"></li>
<?php echo $activeornot++; ?>
<?php echo $teamnumber++; ?>
<?php } }
else {
echo "<div style=background:red> There is no available team member with current settings. </div>";
}
?>
</ul>
</div>
</div>
<div class="row">
<!-- Tab panes -->
<div class="tab-content">
<?php
$query = new \WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$activeornot = 0;
if ($activeornot = 0):
$echo_active_or_not = "active";
else:
$echo_active_or_not = " ";
endif;
$teamnumber = 1;
?>
<div role="tabpanel" class="tab-pane <?php echo $echo_active_or_not; ?> team<?php echo $teamnumber; ?>" id="team<?php echo $teamnumber; ?>">
<div class="col-xxs-12 col-xs-6 col-sm-6 col-md-4">
//something
</div>
</div>
</div>
</div>
<?php echo $teamnumber++; ?>
<?php echo $activeornot++; ?>
<?php } } ?>
I'm using a PHP if else statement to ignore a custom field 'thingstodo' if it's empty. I have got the following code to work but it's putting unwanted line-breaks after every paragraph tag in my content. Where am I going wrong?
<?php if (get_field('gettingthere')): ?>
<div class="inprofile3col">
<h2>Getting there</h2>
<span class="content">
<?php echo get_post_meta($post->ID, 'gettingthere', true); ?>
</span>
<?php endif; ?>
<!-- ends Getting there -->
<!-- Things to do begins -->
<?php if (get_field('thingstodo')): ?>
<h2>Things to do</h2>
<?php endif; ?>
<span class="content">
<?php
$value = get_field('thingstodo');
if ($value) {
echo $value;
} else {
echo '';
}
?>
</span>
</div>
Here
<span class="content">
<?php
$value = get_field('thingstodo');
if ($value ) {echo $value ;} else {echo '';}
?>
</span>
you still have an output if the field is empty: the empty span-tag:
<span class="content">
echo '';
</span>
You should remove line breaks from thingstodo and avoid empty html tags. Here is the updated mark up:
<?php if( get_field('gettingthere') ): ?>
<div class="inprofile3col">
<h2>Getting there</h2>
<span class="content">
<?php echo get_post_meta( $post->ID, 'gettingthere', true ) ; ?>
</span>
<?php endif; ?>
<!-- ends Getting there -->
<!-- Things to do begins -->
<?php if( get_field('thingstodo') && !empty(trim(get_field('thingstodo')))) { ?>
<h2>Things to do</h2>
<span class="content">
<?php
$value = preg_replace('/\s\s+/',' ',trim(get_field('thingstodo')));
if ($value) {echo $value ;} else {echo '';}
?>
</span>
<?php } ?>
</div>
I currently have these two blocks of php that are pulling results:
<?php
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
?>
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
</div>
<?php
endif;
?>
And
<?php
$url = article_custom_field('site-url');
elseif ( !empty($url) ) :
?>
<div class="site-url">Visit</div>
<?php
endif;
?>
I want to combine them to output one single block, like:
<div class="tech-list">
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<div class="site-url">Visit</div>
</div>
It needs to meet the following:
If web-tech exists, output it. Don't output site-url if it doesn't exist.
If web-tech exists, output it. If site-url exists, output it.
If site-url exists, output it. Don't output web-tech if it doesn't exist.
The containing div should not be output at all if neither variable does not exist.
Am I missing an obvious way to do it? It seems trivial, but I can't get the if/else/elseif statements to line up.
You could store your outputs in a variable, like so:
<?php
$output = '';
$webtech = article_custom_field('web-tech');
if ( !empty($webtech) ) :
$output .= '<div class="tech-title">Technologies:</div>'
. '<ul class="tech-ul">' . $webtech . '</ul>';
endif;
$url = article_custom_field('site-url');
if(!empty($url)) :
$output .= '<div class="site-url">Visit</div>';
endif;
if($output != ''):
echo '<div class="tech-list">';
echo $output;
echo '</div>';
endif;
?>
This way, only when there is something set in your output variable, will it show anything.
Does this solve your problem?
It sounds like you need to check both variables before you output the container that they exist in.
<?php
$webtech = article_custom_field('web-tech');
$url = article_custom_field('site-url');
if ( !empty($webtech) || !empty($url))
{
?>
<div class="tech-list">
<?php
if ( !empty($webtech) )
{
?>
<div class="tech-title">Technologies:</div>
<ul class="tech-ul"><?php echo $webtech; ?></ul>
<?php
}
if ( !empty($url) )
{
?>
<div class="site-url">Visit</div>
<?php
}
?>
</div>
<?php
}
?>
if (a or b) then {
OpenTechTitle;
if (a) SetAtext;
if (b) SetBtext;
CloseTechTitle;
}
I have this code, and I need to close a row every 4 post. Every post is inside a div. I tried some things but I coudn't implement to my code.
<?php
echo "<div class='row'>";
global $post;
$all_events = tribe_get_events(
array(
'eventDisplay'=>'upcoming',
//'posts_per_page'=>10,
)
);
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="col-sm-3">
<span class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'j M'); ?></span>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) { ?>
<div class="event-thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="event-excerpt">
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="event-content">
<?php the_content(); ?>
</div>
<?php } ?>
</div>
<?php } //endforeach ?>
<?php wp_reset_query(); ?>
<?php
echo "<div class='row'>";
global $post;
$all_events = tribe_get_events(
array(
'eventDisplay'=>'upcoming',
//'posts_per_page'=>10,
)
);
$count = 1;
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="col-sm-3">
<span class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'j M'); ?></span>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) { ?>
<div class="event-thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="event-excerpt">
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="event-content">
<?php the_content(); ?>
</div>
<?php } ?>
</div>
<?php
if($count == 4){
echo "<div class='seperator'></div>";
$count =1;
}
?>
<?php $count++; } //endforeach ?>
<?php wp_reset_query(); ?>
I'd actually solve this 100% in CSS, so you don't need any counting or handling inside your PHP code.
Have a look at this JSFiddle.
float: left will cause the single elements to all follow each other (left aligned).
clear: left on every 4 * n + 1-th element (nth-child(4n+1)) will clear this, essentially forcing a line break.
There is one caveat to this: If there's no room for all 4 entries in one row, you'll end up with additional wrapping, which can be avoided by defining a fixed width for the container.
A simplified in-code version for PHP would just count the fields written and add a line break as necessary:
$i = 1; // counter
foreach ($events as $event) { // iterate over all events
if ($i++ % 4 == 0) // a % b will be 0 for 4, 8, etc.
echo '<br />'; // print the line break using whatever HTML you see fit.
print_event($event); // print the actual event
}
You might ask whether I check for the line break before actually printing an event: That's to prevent additional line breaks if the number of entries is a multiple of 4, i.e. I avoid having an empty trailing line.
You need the modulo operator. It works like this:
$i == 0;
foreach($some_array as $some_value){
if ($i % $number_to_divide_by == 0) {
// do something here every nth time
}
$i++;
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
i want to exclude some categories from this wp template page, but having problem .. please help me to finish this. codes are following..
<?php
/*
Template Name: Menu card
*/
?>
<?php global $more, $post, $wpdb, $pageid;
get_header();
if (!$pageid) {
$pageid = $post->ID;
}
if (is_category() ) {
$cat_ID = get_query_var('cat');
}
$pagetitle = get_the_title($pageid);
$categories = get_post_meta($pageid, "categories", true);
?>
<div id="content-top"></div>
<div id="content-border">
<div id="content" class="menucard">
<div class="ribbon-container">
<div class="title-container">
<div class="title">
<div class="bar-left"></div>
<div class="bar-right"></div>
<h1 class="post-title"><?php echo $pagetitle; ?></h1>
</div>
</div>
</div>
<a href="" id="card-prev">
<div class="arrow_bit_bottom"></div>
<div class="arrow_bit_top"></div>
<div class="arrow_bit_left"></div>
<div class="arrow_bit_right"></div>
<div class="arrow_bit_middle"></div>
</a>
<a href="" id="card-next">
<div class="arrow_bit_bottom"></div>
<div class="arrow_bit_top"></div>
<div class="arrow_bit_left"></div>
<div class="arrow_bit_right"></div>
<div class="arrow_bit_middle"></div>
</a>
<?php
if ($cat_ID > 0) {
$card_cats = explode(',',$categories);
$count = 1;
for ($x=0; $x < count($card_cats); $x=$x+2) {
for ($i=0;$i<2;$i++) {
$array_location = $x+$i;
if ($cat_ID == $card_cats[$array_location]) {
$activepage = $count;
}
}
$count++;
}
} else {
$activepage = 1;
} ?>
<div id="card-container" activepage="<?php echo $activepage; ?>">
<div id="card-slider">
<?php $card_cats = explode(',',$categories);
$count = 1;
for ($x=0; $x < count($card_cats); $x=$x+2) { ?>
<div id="cardpageid-<?php echo $count; ?>" class="card-page">
<div class="menucard-devider"></div>
<?php for ($i=0;$i<2;$i++) {
$array_location = $x+$i;
if (isset($card_cats[$array_location])) { ?>
<div class="card-cat" id="cardcatid-<?php echo $card_cats[$array_location]; ?>" catid="<?php echo $card_cats[$array_location]; ?>">
<h2><?php echo get_cat_name($card_cats[$array_location]); ?></h2>
<?php $cat_desc = category_description( $card_cats[$array_location] );
if ($cat_desc) { ?>
<div class="cat-desc">
<?php echo $cat_desc; ?>
</div>
<?php } ?>
<?php $child_cats = get_categories('child_of='.$card_cats[$array_location]);
$cat_array = '';
foreach ($child_cats as $child_cat) {
if ($cat_array != '') {
$cat_array .= ',';
}
$cat_array .= '-'.$child_cat->term_id;
}
query_posts('cat='.$card_cats[$array_location].','.$cat_array.'&showposts=-1');
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$more = 0;
include('menuitem.php');
}
}
wp_reset_query();
if ($child_cats) {
foreach ($child_cats as $child_cat) { ?>
<h3><?php echo __($child_cat->cat_name); ?></h3>
<div class="devider"></div>
<?php $cat_desc = category_description( $child_cat->term_id );
if ($cat_desc) { ?>
<div class="cat-desc">
<?php echo $cat_desc; ?>
</div>
<?php }
query_posts('cat='.$child_cat->term_id.'&showposts=-1');
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$more = 0;
include('menuitem.php');
}
}
wp_reset_query();
}
} ?>
</div>
<?php }
} ?>
</div>
<?php $count++;
} ?>
</div>
</div>
</div><!-- #content -->
</div>
<div id="content-bottom"></div>
<?php get_footer(); ?>
For filtering out the child_categories in a "hacky way":
Add below:
foreach ($child_cats as $child_cat) { ?>
The following code (where 1,2,3 is the category id you want to exclude):
<?php if(in_array($child_cat->term_id, array(1,2,3))) continue;
You can do the same for filtering out the "parent" categories.
PS: This is definitely not the best way to do this, just giving him a quick solution.
AFAIK the categories can be removed via the database and not by the code that you showed..
A more elegant way is provided by the admin section of your wordpress. try logging it to /wp-admin and remove it from there. Its easy.
and if you want to skip a few in this then :
unset($child_cats[2]);
$child_cats= array_values($child_cats);
where 2 is the index of item you want to skip