Hide section if it's empty (Wordpress) - wordpress

I can't understand how do not to display my section if its value is empty.
<div class="innerColumn">
<?php
$my_page_id = 142; //your page or post ID
$my_page = get_post($my_page_id); //retrieves the page via the ID
$content = $my_page->post_content; //gets the unfiltered page content
$title = $my_page->post_title; //retrieves page title and sets the variable
?>
<h1 class="titleHead"><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</div>

The simplest way to hide it is to wrap the element in an if statement with an empty check
if(!empty($YOUR_VARIABLE)){ //Code you want to conditionally show}
so in your case I would do (and assuming you want to hide the whole div based on $content):
<?php
$my_page_id = 142; //your page or post ID
$my_page = get_post($my_page_id); //retrieves the page via the ID
$content = $my_page->post_content; //gets the unfiltered page content
$title = $my_page->post_title; //retrieves page title and sets the variable
?>
<?php if (!empty($content)): ?>
<div class="innerColumn">
<h1 class="titleHead"><?= $title ?></h1>
<p><?= $content ?></p>
</div>
<?php endif; ?>
(I took the liberty to clean the code a little, note the shorthand for echo <?=?>

Related

Get the ACF text field value from the category of a custom post type in Wordpress

I am trying to create another class using the value of a text field from ACF PRO.
I have a custom post type named "portfolio" and it has a 4 categories, the ACF field setting is added to "taxonomy is equal to category".
When I edit a category I fill in the name that I want to get and then display like this :
<div class="grid-item-catalog catalog-project-Holder **the name need to be here**">
How do I get the ACF field value from the category?
Other information: my page template is page-portfolio.php and I am using an ACF repeater.
This is my code :
<!-- catalog -->
<div class="grid-catalog catalog-portfolio-wrraper">
<?php if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); ?>
<!-- Repeater -->
<?php if( have_rows('portfolio-projects-repeater') ){ ?>
<?php while( have_rows('portfolio-projects-repeater') ) { the_row();
// vars
$projectDescription = get_sub_field('project-name');
$projectImage = get_sub_field('project-image');
$projectLinkText = get_sub_field('project-link-text');
$projectLink = get_sub_field('project-link');
?>
<div class="grid-item-catalog catalog-project-Holder {the name need to be here}">
<div class="catalog-project-name"><?php the_title(); ?></div>
<div class="catalog-project-content-container">
<div class="catalog-project-image"><img src="<?php echo $projectImage['url']; ?>" alt="<?php echo $projectImage['alt'] ?>" /></div>
<div class="catalog-project-content">
<div class="description-link-container">
<div class="description"><?php echo $projectDescription; ?></div>
<div class="link"><?php echo $projectLinkText; ?></div>
</div>
</div>
</div>
</div>
<?php } ?>
<?php } ?>
<!-- End Repeater -->
<?php } ?>
<?php } ?>
</div>
<!-- end catalog -->
This is a screenshot of the settings for the category's ACF field, I hope it helps:
If I understand correctly, you have a custom post type "portfolio" and you want to get the ACF field value for the categories that the post belongs to. These categories are in the default Wordpress category (i.e. not a custom taxonomy). This is to do with the post and not the values in your repeater.
If that is the case, you can get the value of the ACF field (category-name) for a category like this:
$category_name = get_field('category-name', $category_id);
This means we need to get the category ids for the post in your loop, and you can do this using get_the_category (assuming it is the default categories, for custom taxonomies you need get_the_terms). Also note that a post can have multiple categories so get_the_categories returns an array:
$category_classes = "";
// 1. GET THE CATEGORIES FOR THE CURRENT POST IN THE LOOP
$categories = get_the_category( get_the_ID());
if ( $categories ) {
// 2. A POST CAN HAVE MULTIPLE CATEGORIES SO LOOP THROUGH THEM TO LOOK UP THE ACF FIELD
foreach ( $categories as $category ) {
// 3. ADD ALL ACF VALUE FOR THIS CATEGORY TO OUR $category_classes VARIABLE, SEPARATED BY SPACES
$category_classes .= " ".get_field('category-name', $category->term_id);
}
}
Now $category_classes will have a space-separated list of the ACF values for the categories that you can use directly in your class attribute, i.e.
<div class="grid-item-catalog catalog-project-Holder <?php echo $category_classes; ?>">
Putting this into your code, it should be something like this:
<!-- catalog -->
<div class="grid-catalog catalog-portfolio-wrraper">
<?php if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); ?>
<?php
/* YOUR CODE TO GET THE POST CATEGORIES AND LOOK UP THE ACF FIELD VALUE */
$category_classes = "";
// 1. GET THE CATEGORIES FOR THE CURRENT POST IN THE LOOP
$categories = get_the_category( get_the_ID());
if ( $categories ) {
// 2. A POST CAN HAVE MULTIPLE CATEGORIES SO WE NEED TO LOOP THROUGH THEM ALL
foreach ( $categories as $category ) {
// 3. ADD ALL ACF VALUE FOR THIS CATEGORY TO OUR $category_classes VARIABLE, SEPARATED BY SPACES
$category_classes .= " ".get_field('category-name', $category->term_id);
}
}
?>
<!-- Repeater -->
<?php if( have_rows('portfolio-projects-repeater') ){ ?>
<?php while( have_rows('portfolio-projects-repeater') ) { the_row();
// vars
$projectDescription = get_sub_field('project-name');
$projectImage = get_sub_field('project-image');
$projectLinkText = get_sub_field('project-link-text');
$projectLink = get_sub_field('project-link');
?>
<?php 4. OUTPUT THE CLASSES FOR THE DIV ?>
<div class="grid-item-catalog catalog-project-Holder <?php echo $category_classes; ?>">
<!-- the rest of your code here -->
</div>
<?php } ?>
<?php } ?>
<!-- End Repeater -->
<?php } ?>
<?php } ?>
This code isn't tested, but it should at least point in you in the right direction :)
In your code you are saving a custom field with the name project-name in a variable:
$projectDescription = get_sub_field('project-name');
Maybe you should change this meta key of the subfield to "project-description", it might be confusing, if you also have the name of a project.
To output a value in php you use <?php echo $variableName; ?>
You are accessing the values in a repeater called portfolio-projects-repeater. So your fields are inside of this and need to be accessed using a while, like you did.
Your screenshot shows a text field, which is not inside a repeater field.
So if you want to get the value of that field, you just need to use get_field(). This do not need to be in a while and not be called by using get_sub_field.
Your code therefore should look like:
$variableName = get_field('category-name');
After saving to variable, you can simply output it with:
<div class="grid-item-catalog catalog-project-Holder <?php echo $variableName; ?>">
Yaaaayyy... I succeeded, with this code:
<?php
// load all 'category' terms for the post
$terms = get_the_terms( get_the_ID(), 'category');
// we will use the first term to load ACF data from
if( !empty($terms) ) {
$term = array_pop($terms);
$custom_field = get_field('category-name', $term );
}
?>
In any case i want to thank you for all yours reply's , i am very appreciat this.
Create A Template in the theme and then use the below code, Just change the slug (banner_image)
<?php
//Get the Post ID
$id = get_the_ID();
?>
<img src="<?php echo get_field('banner_image', $id); ?>" />
<div class="col-md-4 text-center"><?php echo get_field( "banner_heading", $id); ?></div>

how to exclude parent pages from get_pages loop Wordpress

I want to display all pages in a one page design.
Now i have this code to do that:
<?php
$pages = get_pages($args);
//start loop
foreach ($pages as $page_data) {
$content = apply_filters('the_content', $page_data->post_content);
$title = $page_data->post_title;
$slug = $page_data->post_name;
?>
<!-- Content section -->
<div class="section" id="<?php echo "$slug" ?>">
<a name="<?php echo "$slug" ?>"></a>
<h2><?php echo "$title" ?></h2>
<?php echo "$content" ?>
</div>
<!-- END Content section -->
<?php } ?>
But now it show's also the empty parent pages. How can i exclude them? i found this on a website:
if($page->post_parent != 2){
But when I insert that into my code i only see the home section.
Is there also a way to apply different templates to the different pages? i know how it is normally done but with a one page website it is difficult.
Thnx
I'm guessing "empty pages" mean they don't have any content inside them.
Try to check if $content has anything inside of it, if not, don't display that page.
Put this inside your foreach statement right after $slug = $page_data->post_name; and before ?>
if($content == ""){
continue;
}

Load page content and blog post index content (WordPress)

I have a question about Wordpress' template structure and querying posts.
I have my templates setup where the (for example) archive-$posttype.php is built like:
get_header();
$args = 'page_id=18'; //Client Uses page
query_posts($args); the_post();
get_template_part( 'sub-header' );
// Reset Query
wp_reset_query();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>...
I do this to set my default $post variable for my sub-header.php file which prints out content from that page:
<div id="subheader">
<h1><?php echo get_post_meta($post->ID, 'header_title', true)?></h1>
<?php echo get_post_meta($post->ID, 'header_description', true)?>...
However, using this method on the home.php template, doesn't work:
get_header();
$temp_query = $wp_query;
$page_id = 119; //Client Uses page
#$post = get_page( $page_id );
$args = array('page_id' => $page_id);
$post = new WP_Query( $args ); the_post();
get_template_part( 'sub-header' );
wp_reset_postdata();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>
<?php get_sidebar( 'news' ); ?>
</div><!--.content -->
<?php get_footer(); ?>
I'm curious why this works on one template and not on the home template. AND, am I going about this the wrong way? What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on.
Thanks!
Not sure I understand your problem exactly but if you are doing what I think, having a chunk of text from a specific page above another loop that pulls in posts of some sort or another I would use the template naming structure.
<?php /* Template Name: My Template */ ?>
This will allow you to use the standard loop to get content from whatever page the clients sets the template to (avoiding the static ids you are using).
Your home page bug could be due to it not being set as the posts page in the reading settings. As I understand it if a page is not set as the posts page it will act like a normal page unless you rewrite the query specifically.
"What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on."
Rather than trying to tie together two unrelated pages/posts, it might be easier to use a custom field tool. My current favorite is Advanced Custom Fields.
With ACF, you can add supplemental fields (image, wysiwyg, file upload, 14 total field types) to your posts and pages, then easily pull the custom data into your template. It's very well documented and extremely easy to use.
So, I changed the way my sub-header.php template works. Basically some basic checking on what type of page/post was being set/called then dynamically pulled the relevant page info.
<?php
if (is_page()) :
$header_title = get_post_meta($post->ID, 'header_title', true);
$video_id = get_post_meta($post->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($post->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($post->ID, 'header_description', true);
elseif (is_home()) :
$page_id = 119; // News page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
elseif (is_archive()) :
$page_id = 18; // Client Uses page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
endif;
?>
<div id="subheader">
<h1><?php echo $header_title; ?></h1>
<?php if ( $video_id ) : ?>
<iframe class="visual-element" width="300" height="200" src="http://www.youtube.com/embed/<?php echo $video_id;?>?rel=0" frameborder="0" allowfullscreen></iframe>
<?php elseif ($thumbnail) : ?>
<?php echo $thumbnail; ?>
<?php endif; ?>
<?php echo $description; ?>
</div><!-- #subheader -->

Wp_list_pages change page title on output

I am using wp_list_pages to create a submenu on a page and child page.
All working fine
My menu looks like
Parent, page title is Hello
Child,
Child,
Child,
etc
I am trying to find a way to dynamically change the Page title on output.
In my example above, I would like my Parent page to display GoodBye instead of Hello.
You might wander why I don't just rename my page to Goodbye.
It is because the Page title , in my design, is displayed in 3 different format
- menu Header Hello displays Welcome (can change this via WP menu
- Page title display the correct title, ie Hello
I need my left menu to display Goodbye....
hope this makes sense for somebody
thx
Use a custom field on your page...let's call it sidebar_title.
Then, you'll need to convert your wp_list_pages code into a custom WordPress loop (there might be a way to use get_pages to do the same if you prefer that.
Here's some sidebar code to list the current page and it's child pages, replacing the_title(); with your sidebar_title if it exists. It's pretty ugly...the main point is to show you how to access custom fields.
<?php
//Get children of current page and display with custom fields.
//You will probably need to adjust this.
$args=array(
'post_parent' => $post->ID,
'post_type' => 'page',
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
?>
<ul>
<?php
// Print parent with sidebar_title, if it exists
$sidebar_title = get_post_meta($post->ID, 'sidebar_title', true);
if ($sidebar_title != ''){ ?>
<li><?php echo $sidebar_title;?></li>
<?php } else { ?>
<li><?php the_title(); ?></li>
<?php } ?>
<?php
// Print each child page with sidebar_title, if it exists
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php
$sidebar_title = get_post_meta($post->ID, 'sidebar_title', true);
echo $sidebar_title;
if ($sidebar_title != ''){ ?>
<li><?php echo $sidebar_title;?></li>
<?php } else { ?>
<li><?php the_title(); ?></li>
<?php } ?>
<?php endwhile; } ?>
</ul>
<?php wp_reset_query();?>

Make WordPress homepage a post category?

I am trying to set my WordPress homepage to a category but it only allows me to set it to either the latest posts or a static page.
Is it possible to set your homepage as a post category?
I am hoping that you know about how to set static page. So first create an empty .php file and name it whatever you like and put it along the other files (index.php, arhive.php etc).
and then enter following code
<?php
/*
* Template Name: Category based Homepage
*/
?>
<?php get_header(); ?>
<div class="main">
<?php
$cat_ID = '1'; //it should be your category ID, you can get the id of the category by going to categories and edit and then in url you can find the tag_ID.
$posts_to_show = '10'; // number of posts from the category you want to show on homepage
//query_posts("cat=$cat_ID&showposts=$posts_to_show");
$category_posts = new WP_Query("cat=$cat_ID&showposts=$posts_to_show");
//if (have_posts())
if ($category_posts->have_posts())
: $first = true;
?>
<ul class="post-list">
<?php
//while (have_posts()) : the_post();
while ($category_posts->have_posts()) : $category_posts->the_post();
if ($first)
$class = "first-in-row";
else
$class = "";
$first = !$first;
?>
<!-- Start: Post -->
<li <?php post_class($class); ?>>
<?php the_post_thumbnail(); ?>
<p class="categories"><?php the_category(", "); ?></p>
<h2><?php the_title(); ?> <?php edit_post_link(__('Edit', 'your_theme_text_domain'), '', ''); ?></h2>
<p class="post-meta"><span class="date"><?php the_time(get_option('date_format')) ?></span> <?php if (comments_open()) : ?>, <span class="comments"><?php comments_popup_link(_x('0', 'comments number', 'your_theme_text_domain'), _x('1', 'comments number', 'your_theme_text_domain'), _x('%', 'comments number', 'your_theme_text_domain')); ?></span> <?php endif; ?> <span class="author"><?php the_author() ?></span></p>
<?php the_excerpt(); ?>
<p class="more"><?php _e('Read More »» ', 'your_theme_text_domain'); ?></p>
<?php if (has_tag()): ?><p class="tags"><span><?php the_tags(""); ?></span></p><?php endif; ?>
</li>
<!-- End: Post -->
<?php endwhile; ?>
</ul>
<?php else : ?>
<h2 class="center"><?php _e('Not found', 'your_theme_text_domain'); ?></h2>
<p class="center"><?php _e('Sorry, but you are looking for something that isn\'t here.', 'your_theme_text_domain'); ?></p>
<?php
endif;
//wp_reset_query();
wp_reset_postdata();
?>
</div>
<?php get_sidebar(); //optional?>
<?php get_footer(); ?>
and replace $cat_ID and $posts_to_show to your liking. And I have used both query methods adjust it to your needs.
Hope it helps somebody who is looking for similar solution.
You can create a custom template that mimics a category page using get_posts and set a page using that template to home, but it won't be perfectly dynamic in the sense that you have to hard code the category slug or ID into that query. Assuming that you don't want to change that category often, that shouldn't be an issue. Alternatively, you could use wp_safe_redirect in a template to redirect to the category page - that would be if you want the user to be put directly on the real category page, URL and all.
I'm not sure what you mean by having your home page as a category, you mean that in your home page posts that will be displayed will be only from a certain category ?
You only need to perform a WP_Query before the loop;
$query = new WP_Query("cat=10, paged=".get_query_var('paged'));
Then use use the WP_Query object to perform the loop;
if($the_query->have_posts()):
while($the_query->have_posts()):
the_title();
the_content();
//Use all the loop function normally
endwhile;
endif;
The paged parameter is used to determine in which page you are, if you need paginantion.
Instead of using the category id, it is good to retrieve the id by the slug.
$home = get_category_by_slug('home-category-slug');
Then your query will be like this
$the_query = new WP_Query("cat=".$home->cat_ID.", paged=".get_query_var('paged'));
Yes this is possible Go to dashboard>>Setting>>Reading>>Static page Choose page from drop down and SAVE. On that page you can create your own stuff...

Resources