I am trying to show all custom posts in Theme options, I am using Theme Options Framework plugin. Here is the code where you can grab all pages but I don't know how to fetch all posts in checkbox so user can select which custom posts show on homepage.
// Pull all the pages into an array
$options_pages = array();
$options_pages_obj = get_pages( 'sort_column=post_parent,menu_order' );
$options_pages[''] = 'Select a page:';
foreach ($options_pages_obj as $page) {
$options_pages[$page->ID] = $page->post_title;
}
for select it is:
<?php
$args = array( 'posts_per_page' => -1 );
$posts = get_posts( $args );
if($posts) {
echo "<select>";
foreach ( $posts as $post ) {
echo "<option value="$post->ID">";
echo $post->post_title;
echo "</option>";
}
echo "</select>";
wp_reset_postdata();
}
?>
For checkbox it is:
<?php
$args = array( 'posts_per_page' => -1 );
$posts = get_posts( $args );
if($posts) {
foreach ( $posts as $post ) {
echo "<div>";
echo "<label>".$post->post_title."</label>";
echo "<input name='posts[]' value='".$post->ID."' type='checkbox' />";
echo "</div>";
}
wp_reset_postdata();
}
?>
Related
How can i get the messages from a certain form?
With this i can get all the inbound messages:
<?php
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish'
);
$query = new WP_Query($args);
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
echo '<article>';
echo get_the_title();
echo '</article>';
}
}
wp_reset_postdata();
?>
But i wanna show only the messages from a specific form for the current month. I search on Google and here but i can not find any example or anything for it so i hope somebody knows.
I have made a solution, maybe not the nicest php writing but it works:
<?php
$currentmonth = date('m');
$args = array(
'post_type' => 'flamingo_inbound',
'post_status' => 'publish',
'monthnum' => $currentmonth
);
$query = new WP_Query($args);
if( $query->have_posts() ){
echo '<ul>';
while( $query->have_posts() ){
$query->the_post();
$results = get_post_meta( get_the_ID() );
foreach($results['_meta'] as $result) {
$normaldata = unserialize($result);
if ($normaldata['post_id'] == '1234') { // The id of the post where the form is send from
$title = get_the_title();
echo '<li>' . $title . '</li>';
} else {
}
}
}
echo '</ul>';
}
wp_reset_postdata();
?>
I know this question has been asked before. I checked through multiple answers on this site,
for example:
Wordpress loop with different bootstrap columns
https://wordpress.stackexchange.com/questions/222278/how-to-separate-posts-loop-in-to-two-columns/222281
... but I cannot work out how to integrate answers with my code (assuming that is possible).
I want to display a list of Categories and their related posts on a page.
The code I'm using works fine BUT displays the results in a single column down the page:
I want to split the display into 2 columns, like in the image below, if possible:
The code I'm using (currently placed in a new page template) is as follows:
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
foreach ( $categories as $category ) {
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
} // End foreach
get_footer();
?>
Wondering if anyone can help me to get this code to display loop results in 2 columns.
Many thanks.
UPDATE TO QUESTION
Karl, thanks for your answer. Your script works, but with a small problem:
The Categories/Related Posts display in 2 columns but a 'gap/space' appears in the middle of the display of data (see image below):
I added to your code slightly so I could display a custom field I inserted into each post. I'm not sure if this has caused the problem.
Altered code (changes are immediately after $query->the_post();):
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
?>
<div style="height:100px"></div>
<?php
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
echo "<div class='new-column'>";
$counter = 0;
foreach ( $categories as $category ) {
if($counter % 4 == 0 && $counter !=0){
echo "<div class='new-column'>";
}
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$customfieldvalue = get_post_meta($post->ID, "PDF", true);
?>
<p><a href="<?php echo $customfieldvalue; ?>" target="_blank"><?php
the_title(); ?></a></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
$counter++;
if($counter % 4 == 0){
echo "</div>";
}
} // End foreach
if($counter % 4 != 0){
echo "</div>";
}
get_footer();
?>
I've used bootstrap classes (row, col-6). Checked the size of categories array and used 2 variables - one as a counter and the other one to check if the column is first or second.
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
//get size of category
$catSize = sizeof($categories);
$j = 1;
$n = 1;
// Loop through categories
foreach ( $categories as $category ) {
if($n == 1){
echo '<div class="row">';
}
echo'<div class="col-6">';
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div></div>';
if($n == 1){
if($j == $catSize){
echo '<div class="col-6"></div>
</div>';
}
else{
$n = 2;
}
}
else{
echo '</div>';
$n =1;
}
$j++;
}
// Restore original Post Data
wp_reset_postdata();
} // End foreach
get_footer();
?>
Try this, I used a modulos operator "%" to group loops into 4, it will create new column every 4 loops. MAKE SURE YOU ADD CSS TO class new-column to arrange it like columns.
<?php
/*
* Template Name: Alphabetical List
*/
get_header();
// Grab all the categories from the database that have posts.
$categories = get_terms( 'category', 'orderby=name&order=ASC');
// Loop through categories
echo "<div class='new-column'">;
$counter = 0;
foreach ( $categories as $category ) {
if($counter % 4 == 0 && $counter !=0){
echo "<div class='new-column'">;
}
// Display category name
echo '<h2 class="post-title">' . $category->name . '</h2>';
echo '<div class="post-list">';
// WP_Query arguments
$args = array(
'cat' => $category->term_id,
'order' => 'ASC',
'orderby' => 'title',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<p><?php the_title(); ?></p>
<?php
} // End while
} // End if
echo '</div>';
// Restore original Post Data
wp_reset_postdata();
$counter++;
if($counter % 4 == 0){
echo "</div>";
}
} // End foreach
if($counter % 4 != 0){
echo "</div>";`enter code here`
}
get_footer();
?>
I'm loading the items straight from the database with php foreach loop, but i want to use wp_query loop, because that gives me more control about pagination, and max items on page.
I tried to transform the code into wp_query loop but not with a good result. What can i do?
<?php
global $wpdb;
$table_name = $wpdb->prefix . "kv_beoordelingen";
$beoordelingen = $wpdb->get_results("SELECT * FROM `{$table_name}` \n". "ORDER BY `{$table_name}`.`dateSince` DESC LIMIT 42");
if( $beoordelingen ) {
foreach ( $beoordelingen as $beoordeling ) {
//vars
$beschrijving = $beoordeling->description;
?>
<?php
$beoordeling_query = new WP_Query(array(
'post_type' => $beoordeling,
'posts_per_page' => -1,
'order' => 'DESC',
'orderby' => 'date',
'paged' => get_query_var('paged'))); ?>
<?php if ( $beoordeling_query->have_posts() ) : ?>
<?php while ( $beoordeling_query->have_posts() ) : $beoordeling_query->the_post();
if( $beschrijving ) {
echo "<p><strong>Ervaring</strong><br/> ".$beschrijving." </p>";
}
endwhile; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif; ?
<?php };
} else {
echo "Geen beoordelingen beschikbaar";
} ?>
Wordpress team members have a select in admin that lets you pick a number to display the team in a certain order on Archive page. The Select returns a value of the number, which i retrieve.
However, I want to display all the members that have the value of 1 first, and any members after that show up below. At the moment, the IF statements only shows the content if they exist (obviously), i would like to however make it work somehow so it shows Members with value 1 first, then members with value 2. Thank you!
$images = get_field('image');
$titles = get_the_title();
$positions = get_field('position');
$link = get_permalink($post->ID);
if ($positions[0] == 1) {
echo "<div class='employee'>";
echo "<img src='$images'>";
echo "<h2>$titles</h2>";
echo "</div>";
}
if ($positions[0] == 2) {
echo "<div class='employee'>";
echo "<img src='$images'>";
echo "<h2>$titles</h2>";
echo "</div>";
}
If you want to sort the $positions array you could use
sort($positions);
You you can easily sort by a meta value with get_posts()...
<?php
$args = array(
'post_type' => 'team_members',
'posts_per_page' => -1,
'meta_key' => 'position',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$posts = get_posts( $args );
?>
<?php if( $posts ): ?>
<ul>
<?php foreach( $posts as $post ): setup_postdata( $post ) ?>
<li class='employee'>
<img src='$images'>
<h2>$titles</h2>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Hi I'm trying to figure out how to put a featured image in wordpress on my custom page.
here is my code:
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
if (has_post_thumbnail() ) {
foreach( $recent_posts as $recent ){
the_post_thumbnail();
echo '<li>' . $recent["post_title"].' </li> ';
}
}
else {
echo "no featured post";
}
?>
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
if( $recent_posts ) {
foreach( $recent_posts as $recent ){
if (has_post_thumbnail($recent['ID']) ) {
echo get_the_post_thumbnail($recent['ID']);
}
echo '<li>' . $recent["post_title"].' </li> ';
}
} else {
echo "no featured post";
}
?>
the_post_thumbnail requires you to be in a loop. If you want to retrieve with your own ID, use get_the_post_thumbnail