Loop only once on post excerpt - php

I'm displaying an excerpt that is pulled from an advanced custom field in wordpress. The problem I have is that the code generates a lot of spans if the post have different paragraph.
How can I control to just generate one span? Just show the first one I mean.
This is my code:
<?php while(has_sub_field("add_more_content")): ?>
<?php if(get_row_layout() == "new_text"): // layout: Content ?>
<span class="post-excerpt"><?php echo custom_field_excerpt(); ?></span>
<?php endif;?>
<?php endwhile; ?>
Which generates something like:
<span class="post-excerpt">text 1</span>
<span class="post-excerpt">text 2</span>
<span class="post-excerpt">text 3</span>

Create a variable which you'll validate once then set to false so it will only execute on one loop:
<?php
$add_span = true;
while(has_sub_field("add_more_content")): ?>
<?php if(get_row_layout() == "new_text"): // layout: Content ?>
<?php if($add_span) : ?>
<span class="post-excerpt">
<?php endif; ?>
<?php echo custom_field_excerpt(); ?>
<?php if($add_span) : ?>
</span>
<?php
$add_span = false;
endif;
?>
<?php endif;?>
<?php endwhile; ?>

Related

Get parameter of shortcode in Insert PHP Code Snippet plugin using ACF repeater

Everybody hello! I'm sorry for asking such kind easy question, I'm pretty new at this. I have problem with transfering parameter value and echoing it to itself.
PHP code snippet on page:
[xyz-ips snippet="Generating-content" paramSet="1"]
Inside 'generating content':
<?php if( have_rows('information') ): ?>
<?php while( have_rows('information') ): the_row(); ?>
<?php $sub_value = get_sub_field('number');
if($sub_value == 1): ?>
<div class="d-block d-md-flex d-lg-flex">
<img src="<?php the_sub_field('image'); ?>">
<div>
<?php if( have_rows('repeatable_content') ): ?>
<?php while( have_rows('repeatable_content') ): the_row(); ?>
<p>
<?php the_sub_field('text'); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Right now I need to change condition if($sub_value == 1) to if($sub_value == "PARAMETER VALUE")
Please help, guys!
I think that is what you are looking for.Not tested but it should work.
<?php
$paramSet = (!isset($atts['paramSet'])) ? $atts['paramSet'] : 'default';
if (have_rows('information')) : ?>
<?php while (have_rows('information')) : the_row(); ?>
<?php $sub_value = get_sub_field('number');
if ($sub_value == $paramSet) : ?>
<div class="d-block d-md-flex d-lg-flex">
<img src="<?php the_sub_field('image'); ?>">
<div>
<?php if (have_rows('repeatable_content')) : ?>
<?php while (have_rows('repeatable_content')) : the_row(); ?>
<p>
<?php the_sub_field('text'); ?>
</p>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endwhile; ?>
Read More - https://developer.wordpress.org/reference/functions/shortcode_atts/
Thank you guys for helping! The main issue of this problem is that you have to buy premium version of this plugin, your version is working in another plugins :)
Working plugin's name: Shortcodes Blocks Creator Ultimate

splitting the paragraph into 2 sections in wordpress

I'm building a property section in WordPress that leaves me with a long and excruciating list of information when the code draws the data. This list should be divided into 2 sections to save more space on the page when on desktops, etc.
<a name="rooms"></a>
<h3><?php echo __("Property Details", "wppf"); ?></h3>
<?php foreach ($paragraphs as $paragraph) : ?>
<div class="clearfix">
<?php if ($paragraph['name'] != '') : ?>
<h4><?php echo $paragraph['name'] ?></h4>
<?php endif; ?>
<?php if ($paragraph['filesortorder'] != '') : ?>
<?php echo ($paragraph['filesortorder']); ?>
<?php endif; ?>
<p>
<?php if ($paragraph['dimensions'] != "") : ?>
<em><?php echo $paragraph['dimensions'] ?></em>
<br />
<?php endif; ?>
<?php echo $paragraph['description'] ?>
</p>
</div>
<?php endforeach; ?>
<?php foreach ($links as $link) : ?>
<div class="clearfix">
<a href="<?php echo $link['url']; ?>" target="_blank">
<?php echo (empty($link['name'])) ? $link['url'] : $link['name']; ?>
</a>
</div>
<?php endforeach; ?>
There is a maximum of 10 sections that it fills in and I want to split that into 2 groups of 5.
Something like...
if(count($paragraphs) > 5) {
$paragraphs2 = array_splice($paragraphs, 5);
}
Would let you split the paragraphs > 5 into another array.
You'd then just need to output those in your code as you see fit - something like:
<?php foreach ($paragraphs2 as $paragraph2) :?>
<div class="clearfix">
<?php if ($paragraph2['name']!=''):?>
<h4>
<?php echo $paragraph2['name'] ?>
</h4>
<?php endif; ?>
<?php if ($paragraph2['filesortorder']!=''):?>
<?php echo ($paragraph2['filesortorder']); ?>
<?php endif; ?>
<p>
<?php if ($paragraph2['dimensions']!=""):?>
<em>
<?php echo $paragraph2['dimensions'] ?>
</em>
<br />
<?php endif;?>
<?php echo $paragraph2['description'] ?>
</p>
</div>
<?php endforeach;?>
Should get you started to loop through and repeat the relevant display code.

If (is_page_template()) not working in header.php

I have 3 separate header options, all with a banner image:
1. Homepage
2. Sponsor Template
3. All other pages.
I have placed the below code in the header. The homepage and all other pages are working as expected, but I can't seem to make the Sponsor template work (the class="sponsor-title" is not appearing).
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if ( is_front_page()): ?>
<span class="home"><h1><?php echo event_title(); ?></h1></span>
<span class="tag-line"><?php the_field('tag_line'); ?></span>
<span class="date"><?php the_field('date_time_header'); ?></span>
<?php
$ticket = get_field('ticket_url');
if ( $ticket ):
$ticket_url = $ticket['url'];
$ticket_title = $ticket['title'];
?>
<a class="button" href="<?php echo esc_url($ticket_url); ?>"><?php echo esc_html($ticket_title); ?></a>
<?php if (!is_page_template('page-templates/all-sponsor-template.php')); ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php endif; ?>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif;?>
What have I done wrong? I want to ensure that when the template or page is chosen that the correct styling appears, as it is very different from the other pages.
Your problem is the syntax of your if statement. Try this instead:
<?php if (!is_page_template('page-templates/all-sponsor-template.php')) { ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php } else { ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php } ?>
I always use the {..} brackets, as it's easier to follow and understand the logic of the code. However, to rewrite your code using the correct structure of the if statement you've attempted, it would look like this:
<?php if (!is_page_template('page-templates/all-sponsor-template.php')): ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif; ?>
And, of course, the exclamation mark here:
if (!is_page_template('page-templates/all-sponsor-template.php'))
means "If NOT page template all-sponsor-template.php" so remove it if you are checking for TRUE.
Finally, keep in mind that due to certain global variables being overwritten during The Loop, is_page_template() will not work in The Loop. However, if this code is in your header.php, then you should be fine.
<?php if (!is_page_template('page-templates/all-sponsor-template.php')); ?>
// Stuff
<?php endif; ?>
Here you are checking if the page template is NOT page-templates/all-sponsor-template.php, and if it isn't - then you show the sponsor title...
I might be confused, but shouldn't it be like this?
<?php if (is_page_template('page-templates/all-sponsor-template.php')); ?>
// Stuff
<?php endif; ?>
I.e. swapping the if condition so that the sponsor title shows if the page template IS the mentioned file.
Edit:
The if statement that checks if the sponsor page template is being used, is inside the if statement that checks if the current page is the home page.
If you move the template check one level up, it should do the trick:
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if ( is_front_page()): ?>
<span class="home"><h1><?php echo event_title(); ?></h1></span>
<span class="tag-line"><?php the_field('tag_line'); ?></span>
<span class="date"><?php the_field('date_time_header'); ?></span>
<?php
$ticket = get_field('ticket_url');
if ( $ticket ):
$ticket_url = $ticket['url'];
$ticket_title = $ticket['title'];
?>
<a class="button" href="<?php echo esc_url($ticket_url); ?>"><?php echo esc_html($ticket_title); ?></a>
<?php endif; ?>
<?php if (is_page_template('page-templates/all-sponsor-template.php')); ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php endif; ?>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif;?>
This way it will check if the sponsor template is being used on all pages and not only when on the home page.

magento do not display title and logo when manufacturer attribute is set to empty

I am working with the following codes for Magento inside product page view.phtml.
<div class="manufacturer">
<?php if ($_product->getAttributeText('manufacturer') == "Bellazza" || "Della" || "Pet Zone" || "Spyder Auto" ):?>
<div class="dealer">
<p>Authorized Dealer:</p>
</div>
<div class="logoImg">
<?php echo $this->getLayout()->createBlock('manufacturer/manufacturer')->setProduct($_product)->setTemplate('manufacturer/product_manufacturer.phtml')->toHtml();?>
</div>
<?php elseif ?>
<?php endif;?>
</div>
So I create div.manufacturer to display associated brand logo image started by the title "Authorized Dealer". It works. But not all products have a brand so when I set the manufacturer attribute to empty ( refer to the image below ) I don't need the title "Authorized Dealer" anymore. What is the best practice to approach the elseif. Thank you
manufacturer/manufacturer.phtml
<?php
$maufacturers = Mage::getModel('manufacturer/manufacturer')->getCollection()->addFieldToFilter('status',Array('eq'=>1));
$_columnCount=4;
$i=0;
?>
<div class="manufacturer-list">
<?php foreach($maufacturers as $manufacturer): ?>
<?php if(Mage::getModel('eav/entity_attribute_source_table')->setAttribute(Mage::getModel('eav/entity_attribute')->load(Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product',"manufacturer")))->getOptionText($manufacturer->getMenufecturerName())): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul>
<?php endif; ?>
<li>
<a href="<?php echo $this->getBaseUrl()."catalogsearch/advanced/result/?manufacturer[]=".$manufacturer->getMenufecturerName() ?>">
<?php echo $this->getLayout()->createBlock('core/template')->setmanufacturerimage($manufacturer->getFilename())->setlegend($manufacturer->getLegend())->setListPageFlag(1)->setTemplate('manufacturer/manufacturer_resize.phtml')->toHtml(); ?>
</a>
<div class="manufacturer-name">
<?php echo Mage::getModel('eav/entity_attribute_source_table')->setAttribute(Mage::getModel('eav/entity_attribute')->load(Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product',"manufacturer")))->getOptionText($manufacturer->getMenufecturerName()) ?>
</div>
</li>
<?php if ($i%$_columnCount==0 && $i!=count($maufacturers)): ?>
</ul>
<?php endif; ?>
<?php endif; ?>
<?php endforeach; ?>
</div>
product_manufacturer
<?php
$product=$this->getProduct();
if(Mage::getModel('eav/entity_attribute_source_table')->setAttribute(Mage::getModel('eav/entity_attribute')->load(Mage::getModel('eav/entity_attribute')->getIdByCode('catalog_product',"manufacturer")))->getOptionText($product->getData('manufacturer'))):
$manufacturers=Mage::getModel('manufacturer/manufacturer')->getCollection()->addFieldToFilter('menufecturer_name',$product->getData('manufacturer'));
foreach($manufacturers as $manufacturer){
$status=$manufacturer->getStatus();
if($status==1){
?>
<div class="manufacturer-img-box">
<a href="<?php echo $this->getBaseUrl()."catalogsearch/advanced/result/?manufacturer[]=".$manufacturer->getMenufecturerName() ?>">
<?php echo $this->getLayout()->createBlock('core/template')->setmanufacturerimage($manufacturer->getFilename())->setlegend($manufacturer->getLegend())->setTemplate('manufacturer/manufacturer_resize.phtml')->toHtml(); ?>
</a>
</div>
<?php }
}
endif;
?>
You need refactoring the code
<div class="manufacturer">
<div class="logoImg">
<?php echo $this->getLayout()->createBlock('manufacturer/manufacturer')->setProduct($_product)->setTemplate('manufacturer/product_manufacturer.phtml')->toHtml();?>
</div>
</div>
and check variable
<?php if(!is_null($manufacturer->getMenufecturerName())): ?>
<div class="manufacturer-name">
<?php
echo Mage::getModel('eav/entity_attribute_source_table')
->setAttribute(Mage::getModel('eav/entity_attribute')
->load(Mage::getModel('eav/entity_attribute')
->getIdByCode('catalog_product',"manufacturer")))
->getOptionText($manufacturer->getMenufecturerName())
?>
</div>
<?php endif; ?>
And you need refactoring your code. Because I loock much excess code in your template. You need get data in you block and in phtml you need render this data by API blocks. getImageSrc(); getManufactures() and more over methods for get processed information.

Im trying to kill the sidebars but i cant - wordpress

I am trying to setup a custom page, you can take a look here:
http://dageniusmarketer.com/download-the-book-now
and im using this code for my custom page. The sidebars as far as i can tell have IDs of "navigation", and "sidebar", however, the rules i am applying to them are not working. I tried a test rule with the ID "content", and it works as expected. Why am i not able to hide the sidebars? Something isn't making sense.
<?php
/*
* Template Name: My Custom Page
* Description: A Page Template with No Sidebars.
*/
?>
<?php get_header(); ?>
<!--single.php-->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="post">
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php echo get_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<p class="metadata">
<?php printf(__('%1$s %2$s', 'mtsgossip'), $time_since, get_the_time(__('l, F jS, Y', 'mtsjourney')), get_the_time()); ?>,
<?php _e('by ','mtsjourney');?> <?php the_author();?>
<?php _e('and is filed under','mtsjourney'); ?> "<?php the_category(', ') ?> ".
<?php if (('open' == $post-> comment_status) && ('open' == $post->ping_status)) {
// Kommentare und Pingen erlaubt ?>
<?php _e('You can','mtsjourney'); ?> <?php _e('leave a response here','mtsjourney'); ?>,
<?php _e('or send a','mtsjourney'); ?>
<a href="<?php trackback_url(true); ?>" ><?php _e('Trackback','mtsjourney'); ?></a>
<?php _e('from your own site','mtsjourney'); ?>.
<?php } elseif (!('open' == $post-> comment_status) && ('open' == $post->ping_status)) {
// Nur Pingen erlaubt ?>
<?php _e('Responses are currently closed, but you can send a','mtsjourney'); ?>
<a href="<?php trackback_url(true); ?>" >
<?php _e('Trackback','mtsjourney'); ?></a>
<?php _e('from your own site','mtsjourney'); ?>.
<?php } elseif (('open' == $post-> comment_status) && !('open' == $post->ping_status)) {
// Kommentare geschlossen, Pingen erlaubt ?>
<?php _e('You can','mtsjourney'); ?>
<?php _e('leave a response here','mtsjourney'); ?>.
<?php _e('Pinging is currently not allowed','mtsjourney'); ?>.
<?php } elseif (!('open' == $post-> comment_status) && !('open' == $post->ping_status)) {
// Kommentare und Pings sind nicht erlaubt. ?>
<?php _e('Both comments and pings are currently closed','mtsjourney'); ?>.
<?php } edit_post_link('E','',''); ?>
</p>
</div>
<div id="navigation" style="margin-left:200px;"></div>
<div id="sidebar" style="display:none"></div>
<div id="content" style="width:900px"></div>
<!--postend-->
<p class="navigate">
<?php previous_post_link('« %link') ?>
||
<?php next_post_link(' %link »') ?>
</p>
<?php endwhile; endif;?>
<!--post end-->
<!--single.php end-->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Looking at the page structure, I tried to remove left and right sidebar by adding following styles.
Put the following styles as inline css in the desired template, so that it don't affect the layout of any other page. Hope this helps.
div#navigation
{
display:none;
}
div#sidebar
{
display:none;
}
div#content
{
margin: 5px 0 75px 0;
}
Your sidebar is being called with this line:
<?php get_sidebar(); ?>
You can try commenting it out to hide the sidebar:
<?php //get_sidebar(); ?>
You have two divs with the "sidebar" id, the one we can see in the code you posted (Which has the styles) and the one that eventually gets created by <?php get_sidebar(); ?>
These are not the same elements, so to fix your problem just remove that superfluous sidebar and apply the styles in your stylesheet.
Why dont you delete all the content of sidebar.php? so, all other files that call it will show nothing.
your sidebar is being called with this line
<?php get_sidebar(); ?>
try to comment it

Categories