How to show only 2 post from post in ACF relationship - relationship

After creating customfield from ACF. I select multiple articles. How can I show only the last 2 posts? Thank!
Show only 2 post from relationship ACF

I would use the post_object field. With this field, you can select which objects you would like to display, when you configure it to "multiple values".
<?php
$featured_posts = get_field('featured_posts');
if( $featured_posts ) { ?>
<ul>
<?php foreach( $featured_posts as $post ) {
setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php } ?>
</ul>
<?php
wp_reset_postdata(); ?>
<?php } ?>

Related

View inside a view using CakePHP 3

Is it possible to display a view inside another view?
I have the following code:
<?php if ($result->type === 'brochure') : ?>
<div>
// massive template block
</div>
<?php elseif ($result->type === 'library') : ?>
<div>
// massive template block different from above
</div>
<?php else : ?>
<div>
// massive template block different from both above
</div>
<?php endif; ?>
I want to replace those with a content block so to speak. I had a look at view blocks but I'm either using it wrong or it doesn't do what I want it to do.
Is this possible in CakePHP 3?
you can use elements for that.
first you should create elements in src/Template/Element directory with .ctp format like this
// in brochure.ctp file in src/Template/Element
<div>
// your massive template block
</div>
then you can call elements like this :
<?php if ($result->type === 'brochure') : ?>
<?= $this->element("brochure") ?>
<?php elseif ($result->type === 'library') : ?>
<?= $this->element("library") ?>
<?php else : ?>
<?= $this->element("default") ?>
<?php endif; ?>

Cakephp working whit Cell

Hi I have a trouble whit Cell in my application I follow the tutorial but did not work for me
here is my code https://github.com/boguda/okpionir.git
I bake cell for News and add display function
I want to use this cell on my static page src/Template/Pages/pocetna.ctp
but it doesn't work
just change
<?php $cell = $this->cell('News'); ?>
<?= $cell ?>
to
<?= $this->cell('News::display'); ?>
Call Cell has to refer to specific method name
EDIT:
Also you have an error in your src/View/Cell/NewsCell
public function display()
{
$this->loadModel('News');
----> $top_news= $this->News->find()
->select('title')
->order(['created'=>'DESC'])
->limit(3)
->toArray();
$this->set('top_news' => $news); <-----
}
should be:
$this->set('top_news', $top_news);
or
$this->set(compact('top_news'));
And your view Template/Cell/News/display.ctp might look like this
<ul class="list-group">
<?php foreach ($top_news as $news): ?>
<li><?= h($news['title']); ?></li>
<?php endforeach; ?>
<li class="list-group-item">Second item</li>
<li class="list-group-item">Third item</li>
</ul>

Advanced Custom Fields: checkbox in repeater field

I can’t seem to get checkboxes working in an Advanced Custom Fields repeater field. The goal is that a different image will be spit out based on what checkbox has been ticked off. The time/title/description part is working great... just need some assistance understanding how to work in the checkbox values.
<?php if( have_rows('agenda') ): ?>
<ul>
<?php while( have_rows('agenda') ): the_row();
$time = get_sub_field('time');
$title = get_sub_field('title');
$description = get_sub_field('description');
$image = get_sub_field('imagepicker');
?>
<li>
<h3><?php echo $time; ?><?php echo $title; ?></h3>
<?php if( $description ): ?>
<?php echo $description; ?>
<?php endif; ?>
<?php if ( 'option1' == get_sub_field('imagepicker') ): ?>
<img src="/option1.jpg" />
<?php elseif ( 'option2' == get_sub_field('imagepicker') ): ?>
<img src="/option2.jpg" />
<?php elseif ( 'option3' == get_sub_field('imagepicker') ): ?>
<img src="/option3.jpg" />
<?php else: ?>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
(and yes I have gone over the other similar S.O. answers but no luck)
finally figured it out... working snippet below in case anyone else runs into the same thing.
<?php if ($image[0] == 'option1') : ?>
<img src="/option1.jpg" />
<?php elseif ($image[0] == 'option2') : ?>
<img src="/option2.jpg" />
<?php elseif ($image[0] == 'option3') : ?>
<img src="/option3.jpg" />
<?php endif; ?>

Category / sub-category loop for drop down menu

I'm trying to dynamically populate a drop down menu with categories and subcategories from a database table. My problem is that I can't figure out a way to loop through the subcategories so they display under the correct category.
In my SQL database I have a table called 'Categories' with 3 columns: id, category, subcategory. The subcategories in the subcategory column are separated by 3 colon marks (Artists:::Childcare:::Classes:::Event).
I'm using unordered lists in html to populate the drop down menu.
<ul id="menu">
<li>button
<ul>
<?php
while($allCategories=mysql_fetch_assoc($resultre1)){ ?>
<li><a href="#"><div class="whatever"<?php print
$relanguage_tags[$allCategories['category']];?>
<?php if(in_array($relanguage_tags[$allCategories['category']],$reCategory))
print "selected='selected'"; ?> >
<?php print $relanguage_tags[$allCategories['category']]; ?></div></a>
<?php } ?>
<ul>
<?php
if($ptype=="showOnMap" || $ptype=="viewFullListing" || $ptype=="home" || $ptype==""){
$reCategoryString=getCommaStringFromArray($reCategory);
$reqr2="select * from $categoryTable where id like '%' ".getRealValue($reCategoryString,"reCategory");
$resultre2=mysql_query($reqr2);
?>
<?php
while($allSubCategories=mysql_fetch_assoc($resultre2)){
$subCatList=explode(":::",$allSubCategories['subcategories']);
$subCatSize=sizeof($subCatList);
for($i=0;$i<$subCatSize;$i++){
?>
<li><a href="#"><div class="whatever"<?php print $relanguage_tags[$subCatList[$i]];?>'
<?php if(in_array($relanguage_tags[$subCatList[$i]],$reSubcategory)) print "selected='selected'"; ?> ><?php print $relanguage_tags[$subCatList[$i]]; ?></div></a></li>
<?php }
} ?>
<?php } ?>
</ul>
</li>
</ul>
</li>
</ul>
$subCatList=explode(":::",$allSubCategories['subcategories']);
$subCatSize=count($subCatList);
for($i=0;$i<$subCatSize;$i++){
echo "<li>".$subCatList[$i]."</li>";
?>
if this isn't the answer means I didn't understood your question well.
EDIT
Make a function that shows all the subcategories from a category table:
function subCategories($category){
$subCategories="";
while($r=mysql_fetch_assoc(mysql_query("select * from ".$category.""))){
$subCategories.="<li>".$r['subCategory']."</li>";
}
return $subCategories;
}
and call this function like this echo subCategories("Housing"); in each category tag.
Hope it helped!

Is it possible to get a different header.php on language switch with qtranslate (Wordpress)?

the problem is that on home page I have a lot of code which is added through header.php and it is not amendable through admin panel. What I want to do is to switch header files on language switch. Whenever a user press on the language icon, lets say on the English language icon the header should also switch from <?php get_header('mylanguage') ?> to <?php get_header('english') ?> or etc. Is it possible to do like this?
Regards,
In your page template instead of calling header through:
get_header();
use this to enable header switching when changing languages;
<?php if (qtrans_getLanguage() == 'de'): ?>
<?php include(TEMPLATEPATH.'/header-home-de.php' ); ?>
<?php else : ?>
<?php include(TEMPLATEPATH.'/header-home-en.php' ); ?>
<?php endif; ?>
In this example there are 2 headers depending on selected language.
Surely, it is possible in qTranslate. Even I've done qTranslate Swicher in bootstrap navigation.
<?php } if (qtranxf_getLanguage() == 'en') { ?>
English
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } elseif (qtranxf_getLanguage() == 'fr') { ?>
Français
<ul class="dropdown-menu">
<?php echo qtranxf_generateLanguageSelectCode('code'); ?>
</ul>
<?php } ?>

Resources