WordPress wpAlchemy sort repeating fields values - wordpress

I don't even know where to begin with this, so I'm just going to throw it out there. Say there is a custom post type and in it there is a wpAlchemy powered meta box with repeating fields, where in a person can add multiple attachments.Say you don't expect or trust the user to sort the attachments themselves so you want to return the array of attachments in alphabetical order by the value title. How would you do this with the following code? (This code is within the loop of a single page)
<?php
global $attachment_repeat;
$meta = $attachment_repeat->the_meta();
?>
<?php if($attachment_repeat->have_value('docs')):?>
<ul class="attachments">
<?php while( $attachment_repeat->have_fields('docs') ) : ?>
<li>
<!--url --><!--title --><?php $attachment_repeat->the_value('title'); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

First you should get the main array ... of "docs" $arr = $attachment_repeat->the_value('docs'); if you var_dump($arr) you will see an associative array .. you can then sort using PHP array sort functions .. maybe something like this:
usort( $arr, "custom_sort" );
function custom_sort($a,$b) {
return strcmp( $a['title'], $b['title'] );
}

Related

ACF: Display values outside the main loop

I am coding a theme that uses an archive on a page with a special template. Above the results of the archive (that also means outside of the loop) I want to display the values of some custom fields. (e.g. the value of the field “intro”. I tried to create an additional loop and have the values displayed, but there are no results. Does anybody see a mistake?
Thanks!
Raphael
Here is what I tried last:
<div class="newsearch_intro">
<?php global $post;
$args = array('numberposts'=>'1');
$custom_posts = get_posts($args);
foreach($custom_posts as $post) : setup_postdata($post);
the_field('intro');
endforeach;
?>
</div>
Try using the_field() / get_field() with the post ID as a second parameter:
<div class="newsearch_intro">
<?php
global $post;
the_field('intro', $post->ID);
?>
</div>

Assign an image to a taxonomy in wordpress?

I am trying to get a different icon to appear next to an event in Wordpress. I am use the "The Event Calendar" plugin and the events are stored as taxonomies. I have three taxonomies. "Tax1", "Tax2" and "Tax3".
I cannot seem to figure out how to call to check what the taxonomies are and assign an icon to each one. Here is what I have so far.
" title="" rel="bookmark">
<?php $taxonomy_exist = taxonomy_exists( 'Tax2' ); ?>
<?php if(is_tax($taxonomy_exist)) { ?>
<p>working!!!!!!!!!!</p>
<?php } else { ?>
<img class="cat-icon" src="/wp-content/uploads/2017/11/Heart.png" />
<?php } ?>
<?php $title = get_the_title($event); ?>
<?php if (strlen($title) > 30) { ?>
<?php echo mb_strimwidth($title, 0, 30, '...'); ?>
<?php } else { ?>
<?php echo $title ?>
<?php }?></a></span><!--.event-title-->
So it is getting to the if statement and failing, showing the heart icon. How can I see if it is part of the "Tax2" taxonomy?
Problem 1:
is_tax() accepts taxonomy names as a first argument. But you put $taxonomy_exist parameter which is boolean. (true/false). Of course it should not work and always return zero. Because there is not any taxonomy called "true" or "false".
Problem 2: Are you sure that tax1, tax2 and tax3 are taxnomies? may be they are terms? if they are terms then i will add a solution code as an update to this answer. If they are taxonomies, then i don't see any logic there. Because taxonomies are not objects to be related to posts. Taxonomy is name of categorization, terms are member elements of those taxonomies.

How to list posts from categories with custom post type? (WP)

Hi I am having an issues with custom post type.
So what I am trying to do, list all posts from subcategories while calling a main category. Usualy this worked with no problem with normal post type from Wordpress, but since I tried to use custom post type it's not working...
My category structure is like this:
Category
Sub category
( Posts inside )
Sub category
( Posts inside )
Any help or tips are appreciated. Thanks
<?php
$categories = get_categories('title_li=&hide_empty=1&parent=1430');
foreach($categories as $category) {
echo "<div class='col-12' style='border-bottom: 0'><h1 class=''>".$category->name."</h1></div>";
$args = array('cat'=> $category->term_id);
if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<!-- article -->
<article class="col-3">
<div class="image">
<span class="helper"></span><?php the_post_thumbnail('full');?>
</div>
<h1><?php the_title(); ?></h1>
<?php the_content();?>
</article>
<!-- /article -->
<?php endwhile; endif; }?>
</main>
There are a couple of problems going on here:
First, you're not declaring a loop, or calling get_posts.
Second, if you check out the documentation for WP_Query (which is the "backbone" behind get_posts, so the arguments are essentially the same), you'll see that if you do NOT pass in an argument for post type, the default is post.
So, since you've not shared with us the post type, you'll have to adjust the below as needed:
// .. your code above ....
$args = array(
'cat'=> $category->term_id,
// Include the post_type in the query arguments
'post_type' => 'custom-post-type' // Change this as needed
);
// Now we need to actually query for the posts...
$custom_posts = new WP_Query( $args );
// These are modified to use our custom loop...
if ($custom_posts->have_posts() ) : while ($custom_posts->have_posts() ) : $custom_posts->the_post(); ?>
// .. your code below ... the_title(), etc will work here...

Get a specific page's excerpt within the loop Wordpress

I am trying to create a loop that loads a random image from any posts, whilst also retrieving the excerpt of a specific page. I have done the random post part, but cannot get it to retrieve the page excerpt... I think I may need to query the pages in their own loop but I'm not sure how to do this. I have installed the function to get page excerpt support etc. but I think I am doing something wrong within the loop, any help would be appreciated.
<div class="postimage">
<?php if (have_posts()) :
query_posts('showposts=1&orderby=rand');
while (have_posts()) : the_post(); ?>
<?php the_post_thumbnail('blog-post-image'); ?>
<div class="borderimage"></div>
<div class="tagline"><h1><?php the_excerpt('$page_id=8'); ?> </h1>
</div>
</div>
</div>
<?php endwhile; else : endif; ?>
query_posts replaces the global $wp_query, which you don't want to do since you want to keep that query for your page. Try this instead...
if (have_posts()){
while(have_posts()){
the_post(); //global $post now has the page in it
$args = array("posts_per_page"=>1,"orderby"=>"rand");
$random_posts = get_posts($args); //returns an array
$random_post = $random_posts[0];
//do your stuff...
//$post contains the original page
//$random_post contains the random post
}
}

Wordpress - Add comma to all but last item, remove underscore

I'm using the advanced custom field plugin for Wordpress to show the results of checkboxes. I have what I want working, I just want to tidy up the code and add the following:
Remove the underscore from the social media tag (some kind of stripping out???).
If possible I'd like to show a comma after each "tag" but not if it's the last one.
Here's my test page, they're the blue "tags" under the discipline section.
Here's my code:
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){
echo ''.strtoupper($name).'';
}
}
?>
Well it is pretty basic, you just have to do a loop. I could have write something better with more information... anyway this should do exactly what your code did but in a loop.
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
if(in_array($name, get_field('categories') )){ //I don't know what this is suppose to do
echo ''.strtoupper($name).'';
}
}
?>
Try this out:
<?php foreach( get_field('categories') as $category ): ?>
<?php echo ucwords($category) ?>
<?php endforeach; ?>
Ok this should be better
<?php
$catNames = array( 'branding','creative','development','exhibition','packaging','print','seo','social_media','usability','web','advertising','campaign','content','feasibility','publishing','research','strategy');
foreach($catNames as $name){
$theID = get_cat_ID($name); // get the ID of each category
echo ''.$theID->name.'';
}
?>

Resources