Pass Advanced Custom Fields Through Blog Loop - php

I'm hoping that someone can help me out here. I am trying to get ACF integrated into a Theme a client purchased.
The basic functionality is that I have built a custom field on every post to choose where to place the post. Thus, if they choose “Option A” in the post, then the code needs to check for that option to be true before passing all of the content related to the post (Title, Content, Images, etc.)
A basic code example of how this would work in a standard php document would be as follows:
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<?php if(get_field('pick_your_theme') == "Theme1") { ?>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
<?php } ?><?php endwhile; ?>
<?php endif; ?>
The issue here is that the theme author has created the page in one giant open PHP function rather than breaking it up (as in example code 1 above) where I can easily insert the selector get_field.
Below is a link for the PHP code for the page:
http://pastebin.com/wyWQiQvx

Normally you should already be able to access ACM fields within the loop using the following code:
<?php the_field('NAME_OF_FIELD'); ?>

Building on #ChrisHerbert's comment, it should be a simple matter of sneaking your conditional in there, so the loop section of your code looks something like this:
if ( have_posts() ){ while (have_posts()){ the_post();
if(get_field('pick_your_theme') == "Theme1") {
if( $gdl_show_title != "No" ){
echo '<div class="sixteen columns mb0">';
echo '<div class="page-header-wrapper">';
echo '<h1 class="page-header-title title-color gdl-title">' . get_the_title() . '</h1>';
echo '<div class="header-gimmick mr0"></div>';
echo '<div class="clear"></div>';
echo '</div>';
echo '</div>'; // sixteen columns
}
if( $page_background != 'No' ){
echo "<div class='sixteen columns'>";
echo '<div class="page-bkp-frame-wrapper">';
echo '<div class="page-bkp-frame">';
}
$content = get_the_content();
// Show content
if( $gdl_show_content != 'No' && !empty($content) ){
echo '<div class="sixteen columns">';
echo '<div class="gdl-page-content">';
echo '<div class="bkp-frame-wrapper">';
echo '<div class="bkp-frame p20">';
the_content();
echo '</div>';
echo '</div>';
echo '</div>'; // page-content
echo '</div>'; // sixteen columns
}
} //pick_your_theme conditional
} // while loop
} //if have posts
Clearly you can customize the code within the 'pick_your_theme' conditional to meet you needs and add a } elseif(get_field('pick_your_theme') == "Some_Other_Theme") { or a default for when 'pick_your_theme' isn't set } else { block in there as needed.

Related

While Loop displays arguments inside the first argument

I have a while loop created to display all my wordpress posts. The issue is that all the posts (except the first) appear inside the first post.
I've check my div structures and I couldn't find any issues.
The loop is working, but the div isn't showing in the right place.
<div class="slider-vertical">
<!-- This is the main loop for the posts -->
<?php
$count = 0;
while ( $all_posts->have_posts() ) : $all_posts->the_post();
$count++;
// Variabled for the specific fields (color, 3D objects and so on...)
$fond_couleur = get_field('fond_de_couleur_hex');
$visuel_3D = get_field('source_iframe_3d');
$visuel_3D_2 = get_field('source_iframe_3d_2');
// get_template_part( 'template-parts/content', 'portfolio' );
$content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = str_replace( ']]>', ']]>', $content );
$gallerieArray = get_field('image_galerie');
$title = get_the_title($post);
?>
<!-- HTML structure for the posts - this is displayed, but only the first one is at the right place -->
<?php
echo '<div class="post-numero';
if($count == 1){
echo ' actif';
}
echo '">';
?>
<!-- DIV that contains the 3D object -->
<div class="visuel-3d" style="background:<?php echo $fond_couleur;?>;">
<!-- Injecting the 3D Object via an iframe -->
<div class="slider-horizontal">
<?php echo '<span class="post-horizontal actif">'. $visuel_3D .'</span>';?>
<?php if($visuel_3D_2){
echo '<span class="post-horizontal">'. $visuel_3D_2 .'</span>';
} ?>
</div>
<h2 class="titrePost"><?php echo $title; ?></h2>
</div>
<!-- Cross used to close the rightside menu -->
<span class="arrow-right-sidebar">x</span>
<!-- HTML structure for the rightside menu -->
<div class="right-sidebar-content foo">
<div class="right-top">
<h2 style="text-align: center;"><?php echo $title; ?></h2>
</div>
<div class="right-bottom">
<div><?php echo $content ?></div><!-- That is where the next posts of the while loop get printed -->
<div class="gallerie-sidebar-full">
<?php // loop for the image gallery at the bottom of the rightside menu
if ($gallerieArray == true){
foreach($gallerieArray as $key => $val){
$gallerieImages[] = $val["url"];
}
$idg = 0;
foreach ($gallerieImages as $val){
echo '<img id="id-'. $title .'-'. $idg .'" class="gallerie-sidebar" src="'. $val .'">';
$idg += 1;
}
}
?>
</div>
</div>
</div>
</div>
<?php
endwhile;
?>
</div>
What is expected is 4 different posts inside the "slider-vertical" div displayed at the same level in the HTML structure.
The actual result is the first post at the correct level and then the next posts 2 levels deeper inside the first post.
this kind of problem occurs when there is a not properly closed HTML tag on the first execution of the loop, try to use a short statement syntax
echo '<div class="post-numero'. (($count == 1)? ' actif' : '').'">;
check also the content printed by <?php echo $content ?> of the first post, may contains an open HTML tag.
You can easily check the generated code using the souce view (CTRL+U) in Firefox that highlights in red the wrong HTML closing tags

Insert ads within category, archive, tag & search page for WordPress

I like to insert adsense in the middle of category, archive, tag & search page listing on a WordPress website. Here is the closest solution I can find online:
<?php $postcounter = 1;
if (have_posts()) : ?>
<?php while (have_posts()) : $postcounter = $postcounter + 1;
the_post(); ?>
<?php if(4 == $postcounter)
{ echo ' <div id="adsbetween">
<center> YOUR_ADSENSE_CODE </center>
</div> ' ;
} ?>
I need help on applying the same with what I have in my theme below:
if (($cat_layout == 'masonry-3') || ($cat_layout == 'masonry-2')) {
echo '<div class="module-masonry-wrapper clear-fix">';
echo '<div class="masonry-content-container">';
while (have_posts()): the_post();
echo kid_masonry_render(get_the_ID());
endwhile;
echo '</div></div>';
Here is another instance:
echo '<div class="classic-blog-content-container">';
while (have_posts()): the_post();
echo kid_classic_blog_render(get_the_ID(), 35);
endwhile;
echo '</div>';
The code should ad ADSENSE_CODE after the 4th listing.
Any help?
This should inject the adsense and let the loop continue (I think that is what you want).
if($cat_layout=='masonry-3' || $cat_layout=='masonry-2'){
$postcounter=1;
echo '<div class="module-masonry-wrapper clear-fix">';
echo '<div class="masonry-content-container">';
while(have_posts()){
if($postcounter==4){
echo '<div id="adsbetween"><center>YOUR_ADSENSE_CODE</center></div>';
}
the_post();
echo kid_masonry_render(get_the_ID());
++$postcounter;
}
echo '</div>';
echo '</div>';
}
The extra case:
$postcounter=1;
echo '<div class="classic-blog-content-container">';
while(have_posts()){
if($postcounter==4){
echo '<div id="adsbetween"><center>YOUR_ADSENSE_CODE</center></div>';
}
the_post();
echo kid_classic_blog_render(get_the_ID(),35);
++$postcounter;
}
echo '</div>';'

Adding Anchor to every module in Joomla 3.1

I am trying to modify the module template code to add an anchor to the title of each module. I have extremely limited PHP knowledge, so any guidance would be very helpful.
I've found posts for doing this with articles, but the code appears to be very different for modules.
This is the code that is in my module template.
function modChrome_basic($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<?php echo $module->content; ?>
<?php endif;
}
function modChrome_standard($module, &$params, &$attribs)
{
if (!empty ($module->content)) : ?>
<div class="rt-block <?php if ($params->get('moduleclass_sfx')!='') : ?><?php echo $params->get('moduleclass_sfx'); ?><?php endif; ?>">
<div class="module-surround">
<?php if ($module->showtitle != 0) : ?>
<div class="module-title">
<?php
echo '<h2 class="title">';
if (preg_match("/icon[-]{1,}/i", $params->get('moduleclass_sfx'))) :
echo '<span class="title-icon ' .getIconClass($params->get('moduleclass_sfx')). '"></span>';
endif;
echo $module->title;
echo '</h2>';
?>
</div>
<?php endif; ?>
<div class="module-content">
<?php echo $module->content; ?>
</div>
</div>
</div>
<?php endif;
}
This is the code I tried
if( strlen($this->item->params->get('title')) > 0 ) {
echo '<a name="'.$this->item->params->get('title').'"></a>';
}
I also tried
if( strlen($module->item->params->get('title')) > 0 ) {
echo '<a name="'.$module->item->params->get('title').'"></a>';
}
Joomla has an easy way to customize the module appearance.
You have to use a Custom module chrome.
You have to create a modules.php file under /templates/TEMPLATE_NAME/html/modules.php.
Inside this file you have to put:
<?php
function modChrome_custom( $module, &$params, &$attribs ) {
echo '<div id="' .$params->get( 'moduleclass_sfx' ) .'" >';
if ($module->showtitle)
{
echo '<h' .$headerLevel .'>' .$module->title .'</h' .$headerLevel .'>';
}
echo '<div class="module_content">';
echo $module->content;
echo '</div>';
echo '</div>';
}
?>
In the code above the module id getting moduleclass_sfx value. You could change it and set another module variable.
In order to use this appearence you have to set the appropriate style to the template module calls like: <jdoc:include type="modules" name="user1" style="custom" />
If you want to use another style name you have to change the function from modChrome_custom to modChrome_NEWSTYLE.
Good Luck!

How can I combine these two if/else statements for different results?

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;
}

Magicfields foreach not stopping, iterating too many

Using the magicfields 2.0 plugin for Wordpress 3.1.
Here's the broken page:
http://sseko.wecreativeagency.com/style/
and here's a page with it working:
http://sseko.wecreativeagency.com/university-bound/
Note the footer on the first page is receiving the id info from the magicfields
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>">
<?php the_content(); ?>
<div id="note" class="grid_12"><p class="note grid_6 alpha"><span class="special">Style your Ssekos!</span> There are so many ways to tie your Ssekos. Watch the videos and learn how! Then, come up with your own!</p><h1 class="grid_6 omega"><?php wp_title(' ','true','right'); ?></h1></div>
<?php $styles = getFieldOrder('image');
if(is_array($styles))
{foreach($styles as $style)
{
echo "<div class='grid_3'>";
echo "<a rel='styles' href='#info$style'class='inlineimg grid_3'>";
echo "<img src='";
echo get_image('image',1,$style,$tag_img=0);
echo "'class='grid_3' title='";
echo get('name',1,$style);
echo "'alt='";
echo get('name',1,$style);
echo "'";
echo "</a>";
echo "<h2 class='grid_3'>";
echo get('name',1,$style);
echo "</h2></div>";
echo "<div style='display:none'><div id='info$style' class='grid_8 lightbox'>";
echo get('link',1,$style);
echo "</div></div>";
}
}
?>
<div class="clear"></div>
</article>
<?php endwhile; endif; ?>
I've check the database for extra entries but I can't find the reason for it continuing to iterate out into the elements below.
For some reason I could not check your site .. but I believe this is hapenning because you have 2 nested loops ..
One is the Wordpress loop if--> while , and then your Foreach loop.
When the Wordpress loop encounters posts , it will iterate your second loop FOR EACH ONE of the posts ...
for example, on a page where there are 10 $post, it will iterate 10 times for each $style ...
When you have 1 post , it will iterate once .
Like I said, for some reason your site was not available to me , but seeing your URL construction, My guess is that it does not work on the first example, because it is some kind of category (which returns multiple posts ) and the second url is a SINGLE post ...

Categories