I have a php/wordpress code (present inside abc.php file) as shown below which displays images of particular size.
php/wordpress (inside abc.php file):
<ul id="list-thumbs" class="list-grid">
<?php
while ( have_posts() ) : the_post();
?>
<li class="list-grid__thumb shows-grid__item">
<a class="list-grid__img-link" href="<?php echo esc_url( get_the_permalink() ); ?>" tabindex="0">
<?php if ( has_post_thumbnail() ) { ?>
<?php
$image_id = get_post_thumbnail_id( get_the_ID() );
WORLD\Images\the_img_fit_figure( $image_id, 'list-grid__image', '(min-width: 450px) 50vw, 70vw', false ); ?>
<?php } ?>
</a>
</li>
<?php endwhile; ?>
</ul>
The code above displays around 30-40 images from top to bottom with 4 images in one row.
(a) All of those images are featured image in wordpress associated with a post of post_type hello-world
(b) Every post has two fields, Row View (in_row_view) and Column View (in_column_view) coming from Advanced Custom Fields plug in. I am not sure if we can call those two fields as custom fields.
(c) Row View and Column View both are switch which can be enabled to Yes or No. By default, it is set to No.
(d) For Row View, I am using:
if(get_field('in_row_view' ) == '1'){ }
For Column View, I am using:
if( get_field('in_column_view' ) == '1'){ }
What I want to achieve is I want to display those posts which have Row View switch enabled to Yes.
Problem Statement:
I am wondering what changes I need to make in the php/wordpress code above so that it displays only those posts which have Row View switch enabled to Yes.
Probably I need to integrate this code if(get_field('in_row_view' ) == '1'){ } in the php/wordpress code above.
Quick/Sloppy Solution:
<ul id="list-thumbs" class="list-grid">
<?php
while ( have_posts() ) : the_post();
// This will skip the loop
if(get_field('in_row_view' ) != '1') { continue; }
?>
<li class="list-grid__thumb shows-grid__item">
<a class="list-grid__img-link" href="<?php echo esc_url( get_the_permalink() ); ?>" tabindex="0">
<?php if ( has_post_thumbnail() ) { ?>
<?php
$image_id = get_post_thumbnail_id( get_the_ID() );
WORLD\Images\the_img_fit_figure( $image_id, 'list-grid__image', '(min-width: 450px) 50vw, 70vw', false ); ?>
<?php } ?>
</a>
</li>
<?php endwhile; ?>
</ul>
... the issue with the "Quick Solution" is that it will not render the desired "posts per page". The easier way is to include or exclude at the query level with meta_query ... this can be done by grabbing the query "in flight" with the pre_get_posts hook.
The More Elegant Solution
function pw_filter_query( $query ) {
// Your conditions for NOT applying the meta query ... use wp conditionals! :)
if( !$query->get('post_type') != 'your_post_type ) { return $query; }
$query->set('meta_key', 'ecpt_color');
$query->set('meta_value', 'green');
}
}
add_action('pre_get_posts', 'pw_filter_query', 9999);
Source Reference
Related
I am using WCFM plugin. Everything works fine, there is only one problem that I have not solved yet, that is the link of the product tab appearing 2 // characters, and it is wrong with some other functions on my web.
Here is the screenshot
all other tabs are fine, only the product tab appears 2 characters / as below.
ex: mydomain.com/store/storename//#tab_links_area
And I want it to turn into the following
mydomain.com/store/storename/#tab_links_area
or: mydomain.com/store/storename/product/#tab_links_area
And I have checked that the code that affects this place is in store-tabs.php file of WCFM plugin as follows
<?php do_action( 'wcfmmp_store_before_tabs', $store_user->get_id() ); ?>
<div id="tab_links_area" class="tab_links_area">
<ul class="tab_links">
<?php foreach( $store_tabs as $store_tab_key => $store_tab_label ) { ?>
<li class="<?php if( $store_tab_key == $store_tab ) echo 'active'; ?>"><?php echo $store_tab_label; ?></li>
<?php } ?>
</ul>
</div>
<div class="wcfm-clearfix"></div>
<?php do_action( 'wcfmmp_store_after_tabs', $store_user->get_id() ); ?>
I don't know anything about code, can you help me fix this issue using CSS or using code to insert to function.php in my child theme.
I am very grateful and look forward to your feedback
It seems like the store link is register with a "/" at the end in the database whereas the others are not. Maybe there was a change in the permalink structure between the creation of product page and the other pages.
First thing you can do is to go to settings->permalinks and click the "save changes" to make sure all your links will have the same structure.
If this not solve your issue, you can handle it with your code.
You can test if the last caracter of the string that contains your pages link "$store_user->get_store_tabs_url( $store_tab_key )" end up with a "/" and if it the case delete it before it is used in the anchor tag.
So, in those lines =>
<?php foreach( $store_tabs as $store_tab_key => $store_tab_label ) { ?>
<li class="<?php if( $store_tab_key == $store_tab ) echo 'active'; ?>">
<?php echo $store_tab_label; ?>
</li>
<?php } ?>
you should add the test and have something like that :
<?php foreach( $store_tabs as $store_tab_key => $store_tab_label ) { ?>
<li class="<?php if( $store_tab_key == $store_tab ) echo 'active'; ?>">
<?php
/* Get the link */
$tab_link = $store_user->get_store_tabs_url( $store_tab_key );
/* If the link end up with "/" */
if(substr($tab_link, -1) == "/"){
$tab_link = substr_replace($tab_link ,"",-1); // Delete the last caracter.
}
?>
<?php echo $store_tab_label; ?>
</li>
<?php } ?>
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>
I have a page-header that is getting copied over to all of my blog posts that I want to remove completely. I am removing it visually via CSS, but SEO crawlers are still picking it up via the tag.
I am using a standard wordpress them augmented with Elementor. Here is a screenshot of the SEO report.
And here is a screenshot of the actual HTML code
Let me know if any of you have any additional questions! Thank you for your help!
You can make a conditional statement that just outputs the title of the post if on single blog posts, so something like this
<div class="container clr page-header-inner">
<?php
// Return if page header is disabled
if ( oceanwp_has_page_header_heading() ) { ?>
<!-- check to see if single blog posts -->
<?php if (is_single()) : ?>
<h1><?php echo get_the_title(); ?></h1>
<!-- otherwise show the existing title format -->
<?php else: ?>
<<?php echo esc_attr( $heading ); ?> class="page-header-title clr"<?php oceanwp_schema_markup( 'headline' ); ?>><?php echo wp_kses_post( oceanwp_title() ); ?></<?php echo esc_attr( $heading ); ?>>
<?php endif; ?>
<?php get_template_part( 'partials/page-header-subheading' ); ?>
<?php } ?>
<?php if ( function_exists( 'oceanwp_breadcrumb_trail' ) ) {
oceanwp_breadcrumb_trail();
} ?>
</div><!-- .page-header-inner -->
you would have to replace the existing code
I use wordpress CMS and CMB2 METABOXES and Custom fields. Its works perfect but when I'm making loop If data in metabox not exists , this script anyway rendering empty group field. I want to give the command to script, if it is empty ----> show my custom markup code. I have this
<?php $our_advantages_items = get_post_meta( get_the_id(), 'our_advantages_block_box', true );
if( !empty( $our_advantages_items ) ) {
foreach( $our_advantages_items as $our_advantages_item ) { ?>
<div class="cey96zggvcich3">
<div class="cey96zggvcich3_cvz">
<h2><?php echo $our_advantages_item['our_advantages_block_heading']; ?></h2>
<div class="io2ji39349959jeij">
<?php echo wpautop( $our_advantages_item['our_advantages_block_content'], 1 ); ?>
</div>
</div>
</div>
<?php }
} ?>
Will be happy for any help !
Not sure I understand what you're asking, but just add an else to your if statement
<?php if( !empty( $our_advantages_items ) ) : ?>
//If not empty write your html here
<?php else: ?>
//If empty write your html here
<?php endif; ?>
i use a Plug-in on my wordpress site which uses a query to show search results, thats the code of the query:
if ( $query->have_posts() )
{
?>
<?php echo $query->found_posts; ?> Ergebnisse gefunden<br />
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<div>
<?php if ( has_post_thumbnail() ) { echo '<p>'; the_post_thumbnail("small"); echo '</p>'; } ?>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
?>
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
Previous | Next
<?php
}
else
{
echo "No Results Found";
}
The search should only display results from pages who uses a specific page template (mytemplate.php)
I found the Docs http://codex.wordpress.org/Function_Reference/is_page_template who explain this, but i dont get it to work inside the above query.
any help would be nice :) thx
Please do as below.
if ( is_page_template( '{enter the name of page template with extension}' ) ) {
// Enter your if code here
} else {
// Enter your else code here.
}
For this code to work you must select the page template option while creating the page in WordPress Admin Interface.